4stra commited on
Commit
46b8217
1 Parent(s): 56b66d5

App.py final. exporting files works.

Browse files
Files changed (1) hide show
  1. app.py +444 -54
app.py CHANGED
@@ -1,23 +1,51 @@
1
- import gradio as gr
 
2
  from textclassifier import TextClassifier as tc
3
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
4
  from functions import functions as f
5
  import time
6
 
 
 
 
 
 
 
7
  USER_LIST = ['jimmieakesson', 'BuschEbba', 'annieloof', 'JohanPehrson', 'bolund', 'martastenevi', 'SwedishPM',
8
  'dadgostarnooshi']
9
 
 
 
 
 
 
 
 
 
 
10
  UserNameDict = dict(zip(['Jimmie Åkesson', 'Ebba Busch', 'Annie Lööf', 'Johan Pehrson', 'Per Bolund',
11
  'Märta Stenevi', 'Magdalena Andersson', 'Nooshi Dadgostar'], USER_LIST))
12
 
13
  Columns = ['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic', 'sub_topic', 'sentiment', 'target', 'tweet',
14
  'date', 'urls', 'id', 'class_tuple', 'user_id']
 
 
15
 
16
 
17
- def show_all_stats(ListChoices, SeeFullStats):
18
  dataframe = pd.read_csv("{}/data/twitterdata.csv".format(tc.ROOT_PATH))
19
  if SeeFullStats:
20
- return dataframe[ListChoices]
21
  else:
22
  return pd.DataFrame()
23
 
@@ -27,71 +55,433 @@ def fixChoicesCorrectOrder(Choices):
27
  return ListChoices
28
 
29
 
30
- def MatchNameToUser(Name):
31
- return UserNameDict[Name]
 
 
 
 
 
 
 
 
 
 
 
32
 
33
 
34
  def main(From,
35
  To,
36
- Username,
37
  UserNameChoices,
38
- Nbr_Of_Tweets_To_Classify,
39
- ListChoices,
40
- SeeFullStats
 
 
 
41
  ):
42
- def WhoToScrape():
43
- if Username == "":
44
- return MatchNameToUser(UserNameChoices[0])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  else:
46
- return Username
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- text_classifier = tc.TextClassifier(
49
- from_date=From,
50
- to_date=To,
51
- user_list=WhoToScrape(),
52
- num_tweets=int(Nbr_Of_Tweets_To_Classify))
 
 
 
 
 
 
 
 
 
53
 
 
 
54
  text_classifier.run_main_pipeline()
55
  dataframe = text_classifier.get_dataframe()
56
- dataframe = dataframe[
57
- ['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic', 'sub_topic', 'sentiment', 'target', 'tweet',
58
- 'date', 'urls', 'id', 'class_tuple', 'user_id']]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- return dataframe[fixChoicesCorrectOrder(ListChoices)], show_all_stats(fixChoicesCorrectOrder(ListChoices), SeeFullStats)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
 
63
  if __name__ == "__main__":
64
- from datetime import date
65
-
66
- demo = gr.Interface(
67
- article="</a><br>From = The date from which you want to start the analysis.</a><br> To = The date to which "
68
- "you want to end "
69
- "the analysis.</a><br> Username = The username of the user you want to analyze.</a><br> How many "
70
- "tweets to classify = "
71
- "The number of tweets you want to analyze.</a><br>",
72
- analytics_enabled=False,
73
- theme="default",
74
- title="Twitter data analysis",
75
- fn=main,
76
- inputs=[gr.components.Textbox(label="From", value='2022-01-01'),
77
- gr.components.Textbox(label="To", value='2022-01-25'),
78
- gr.components.Textbox(label="Username", value="BuschEbba"),
79
- gr.components.Checkboxgroup(
80
- choices=['Jimmie Åkesson', 'Ebba Busch', 'Annie Lööf', 'Johan Pehrson', 'Per Bolund',
81
- 'Märta Stenevi',
82
- 'Magdalena Andersson', 'Nooshi Dadgostar'], label=""),
83
- gr.components.Textbox(label="How many Tweets to Classify", value="20"),
84
- gr.components.Checkboxgroup(label="Options",
85
- choices=['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic',
86
- 'sub_topic', 'sentiment', 'target', 'tweet', 'date', 'urls', 'id',
87
- 'class_tuple', 'user_id'],
88
- value=['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic',
89
- 'sub_topic', 'sentiment', 'target', 'tweet', 'date']
90
- ),
91
- gr.components.Checkbox(label="Show full statistics")
92
- ],
93
- outputs=[
94
- gr.components.DataFrame(label="Summary statistics of the intervall you selected", max_rows=None),
95
- gr.components.DataFrame(label="Summary statistics of the total database", max_rows=None, )])
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  demo.launch(share=False)
 
 
 
 
 
1
+ import numpy as np
2
+
3
  from textclassifier import TextClassifier as tc
4
  import pandas as pd
5
+ import regex as re
6
+
7
+ from pathlib import Path
8
+ import glob
9
+ from math import sqrt
10
+ import os
11
+ import matplotlib
12
+
13
+ matplotlib.use('Agg')
14
+ import matplotlib.pyplot as plt
15
  from functions import functions as f
16
  import time
17
 
18
+
19
+ SELECTED_COLUMN_DICT = { 'merged_topic': ['tweet', 'main_topic' , 'sub_topic' ,'synonym_topic' , 'cos_sim_topic', 'merged_topic' ],
20
+ 'sentiment':['tweet', 'sentiment'],
21
+ 'merged_target': ['tweet', 'target','synonym_target', 'cos_sim_target' , 'merged_target']
22
+ }
23
+
24
  USER_LIST = ['jimmieakesson', 'BuschEbba', 'annieloof', 'JohanPehrson', 'bolund', 'martastenevi', 'SwedishPM',
25
  'dadgostarnooshi']
26
 
27
+ USER_NAMES = ['Jimmie Åkesson', 'Ebba Busch', 'Annie Lööf', 'Johan Pehrson', 'Per Bolund', 'Märta Stenevi',
28
+ 'Magdalena Andersson', 'Nooshi Dadgostar']
29
+
30
+ CHOICE_LIST = ['Topic', 'Sentiment', 'Target']
31
+
32
+ # PLOT_CHOICES_DICT = {'Topic': 'sub_topic', 'Sentiment': 'sentiment', 'Target': 'target'} I just changed its pavue to merged target and merged topic
33
+ PLOT_CHOICES_DICT = {'Topic': 'merged_topic', 'Sentiment': 'sentiment', 'Target': 'merged_target'}
34
+ PLOT_CHOICES_REVERSE_DICT = {'merged_topic': 'Topic', 'sentiment': 'Sentiment', 'merged_target': 'Target'}
35
+ # PLOT_CHOICES_REVERSE_DICT= {'sub_topic':'Topic', 'sentiment':'Sentiment' , 'target':'Target'}
36
  UserNameDict = dict(zip(['Jimmie Åkesson', 'Ebba Busch', 'Annie Lööf', 'Johan Pehrson', 'Per Bolund',
37
  'Märta Stenevi', 'Magdalena Andersson', 'Nooshi Dadgostar'], USER_LIST))
38
 
39
  Columns = ['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic', 'sub_topic', 'sentiment', 'target', 'tweet',
40
  'date', 'urls', 'id', 'class_tuple', 'user_id']
41
+ num_tweet = 1000
42
+ LIMIT = 0.05
43
 
44
 
45
+ def show_all_stats(SeeFullStats):
46
  dataframe = pd.read_csv("{}/data/twitterdata.csv".format(tc.ROOT_PATH))
47
  if SeeFullStats:
48
+ return dataframe
49
  else:
50
  return pd.DataFrame()
51
 
55
  return ListChoices
56
 
57
 
58
+ def MatchNameToUser(user_names):
59
+ users = []
60
+ for N in user_names:
61
+ users.append(UserNameDict[N])
62
+ return users
63
+
64
+
65
+ def convert_plot_choices(plot_choices):
66
+ return [PLOT_CHOICES_DICT[choice] for choice in plot_choices]
67
+
68
+
69
+ def convert_back_plot_choices(plot_choices_raw):
70
+ return [PLOT_CHOICES_REVERSE_DICT[choice] for choice in plot_choices_raw]
71
 
72
 
73
  def main(From,
74
  To,
 
75
  UserNameChoices,
76
+ plot_choice,
77
+ save_selected ,
78
+ rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8,
79
+ v1, v2, v3, v4, v5, v6, v7, v8 ,
80
+ s1, s2, s3, s4, s5, s6, s7, s8
81
+
82
  ):
83
+
84
+
85
+ save_file_bool = s1, s2, s3, s4, s5, s6, s7, s8
86
+
87
+ def Add_Pychart(df, leaders, plot_choices):
88
+ df_list = []
89
+ pie_charts = []
90
+ return_list = []
91
+ leader_bool_list, plot_bool_list = convert_to_boolean(leaders, convert_back_plot_choices(plot_choices))
92
+ bool_list = []
93
+ for leader in leader_bool_list:
94
+ if leader:
95
+ for choice in plot_bool_list:
96
+ bool_list.append(choice)
97
+ else:
98
+ for i in range(len(plot_bool_list)):
99
+ bool_list.append(False)
100
+ for user in USER_NAMES: # leaders:
101
+ df_list.append((df.loc[df["username"] == UserNameDict[user]], user))
102
+
103
+ for db in df_list:
104
+ for col in PLOT_CHOICES_REVERSE_DICT: # plot_choices:
105
+ if col=='merged_target':
106
+ pie_charts.append(bar(db[0], col + ": " + db[1]))
107
+ else:
108
+ pie_charts.append(pie_chart(db[0], col, col + ": " + db[1]))
109
+ return pie_charts
110
+
111
+ def bar(db: pd.DataFrame, title):
112
+ '''This method adds a stacked bar diagram for each target and each sentiment
113
+ NOTE: The tweets without any target are not shown in the plot, we just show distribution of tweets that have a
114
+ target.
115
+ '''
116
+ if db.empty:
117
+ return None
118
  else:
119
+ db['merged_target']= db["merged_target"].apply(lambda x: "other" if x == "ERROR_9000" or x == "ERROR_496" else x) # replacing Different Error type with string "other"
120
+ db['sentiment'] = db['sentiment'].apply(lambda x: re.sub('\s+', "", x)) # removing extra spaces in at the end and beginning of the sentiments.
121
+ # This can be removed after we remove all unnessary spaces from twitter data
122
+ all_targets= ['v', 'mp', 's', 'c', 'l', 'kd', 'm', 'sd', 'Red-Greens', 'The opposition']
123
+ db_new = db.loc[db["merged_target"] != "other"] # dataframe with other category removed
124
+ percent_target = (len(db_new) / len(db))*100
125
+ targets= db_new["merged_target"].value_counts().keys().to_list()
126
+ positive=[0]*len(all_targets)
127
+ negative=[0]*len(all_targets)
128
+ neutral=[0]*len(all_targets)
129
+ other =[0]*len(all_targets)
130
+ for i,target in enumerate(all_targets):
131
+ temp_db= db_new.loc[db_new["merged_target"] == target]
132
+ if temp_db.empty:
133
+ pass
134
+ else:
135
+ sent = temp_db['sentiment'].to_list()
136
+ positive[i] +=sent.count('positive')
137
+ negative[i] += sent.count('negative')
138
+ neutral[i] += sent.count('neutral')
139
+ other[i] += sent.count('other')
140
+ font1 = {'family': 'serif', 'color': 'blue', 'size': 10}
141
+ fig = plt.figure()
142
+ y1 = np.array(positive)/len(db_new)
143
+ y2 = np.array(negative)/len(db_new)
144
+ y3 = np.array(neutral)/len(db_new)
145
+ y4 = np.array(other)/len(db_new)
146
+ plt.bar(all_targets, y1 , color='g')
147
+ plt.bar(all_targets, y2 , bottom=y1, color='r')
148
+ plt.bar(all_targets, y3 , bottom=(y1+y2), color ='yellow')
149
+ plt.bar(all_targets, y4 , bottom=(y1+y2+y3) , color= 'b')
150
+ plt.xticks(rotation=15)
151
+ plt.ylim(0, 1)
152
+ plt.title( str(percent_target)[0:4] + "% "+ " of tweets have target. "+ "Number of tweets with target:" +str(len(db_new)),loc='right',fontdict=font1)
153
+ #plt.xlabel("Targets")
154
+ plt.ylabel("Procent")
155
+ plt.legend(["positive","negative", "neutral","other"])
156
+ return fig
157
 
158
+ def pie_chart(db, col_name, title):
159
+ if db.empty:
160
+ return None
161
+ else:
162
+ # db = db[col_name].value_counts()[:5] # Lägg till "Others sedan"
163
+ db = piechart_input(db, col_name, LIMIT)
164
+ labels = db[col_name].to_list()
165
+ sizes = db['frequency'].values
166
+ # explode = (0, 0.1, 0, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
167
+ font1 = {'family': 'serif', 'color': 'blue', 'size': 20}
168
+ fig = plt.figure()
169
+ plt.pie(sizes, labels=labels, radius=1, autopct='%1.1f%%')
170
+ plt.title(title, fontdict=font1)
171
+ return fig
172
 
173
+ text_classifier = tc.TextClassifier(from_date=From, to_date=To, user_list=MatchNameToUser(UserNameChoices),
174
+ num_tweets=num_tweet)
175
  text_classifier.run_main_pipeline()
176
  dataframe = text_classifier.get_dataframe()
177
+ # dataframe= pd.read_csv(os.path.dirname(
178
+ # os.path.dirname(os.path.abspath(__file__))) + "/politweet/data/twitterdata.csv") #
179
+ df = dataframe
180
+
181
+
182
+ if save_selected:
183
+ user_list = MatchNameToUser(UserNameChoices)
184
+ df_l=[]
185
+ for user in user_list:
186
+ df_l.append( pd.DataFrame(df.loc[df['username']== user]) )
187
+
188
+ selected_df= pd.concat(df_l).reset_index(drop=True)
189
+ export_to_download(selected_df,"selected_leaders")
190
+ save_selected_checkbox= [gr.Checkbox.update(interactive=False)]
191
+
192
+
193
+ pycharts = Add_Pychart(df, UserNameChoices, convert_plot_choices(plot_choice))
194
+
195
+ rb_components = [rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8] #radio_buttons
196
+ df_visibility_check = [v1,v2,v3,v4,v5,v6,v7,v8]
197
+ def get_selected_df_list(d_frame,save_or_no ,selected_users, radio, visiblity):
198
+
199
+ leader_bool_list = [True if leader in selected_users else False for leader in USER_NAMES]
200
+ df_list=[]
201
+ number_tweets = []
202
+ save_file_components_list =[]
203
+ for i , u_bool in enumerate(leader_bool_list):
204
+ user_df = d_frame.loc[d_frame['username'] == USER_LIST[i]]
205
+ number_tweets.append(gr.Number.update(value=len(user_df),visible=u_bool))
206
+
207
+ if save_or_no[i]:
208
+ export_to_download(pd.DataFrame(user_df) ,"one_leader" )
209
+ save_file_components_list.append( gr.Checkbox.update(visible=u_bool , interactive=False) )
210
+ else:
211
+ save_file_components_list.append( gr.Checkbox.update(visible=u_bool) )
212
+
213
+ if u_bool and visiblity[i]:
214
+ df_list.append( get_exemple_df(user_df,PLOT_CHOICES_DICT[radio[i]]) )
215
+ else:
216
+ df_list.append(None)
217
+
218
+ return df_list +number_tweets+save_file_components_list
219
+
220
+ return pycharts + save_selected_checkbox +get_selected_df_list(df,save_file_bool,list(UserNameChoices), rb_components, df_visibility_check)
221
+
222
+ ''' END OF MAIN
223
+ ####
224
+ #####
225
+ ####
226
+ ####
227
+ '''
228
+
229
+
230
+
231
+
232
+
233
+ def get_exemple_df(df:pd.DataFrame, column:str):
234
+ print(column)
235
+ df=df[SELECTED_COLUMN_DICT[column] ]
236
+ unique_labels = df[column].value_counts().keys()
237
+ stat=[]
238
+ for label in unique_labels:
239
+ df_temp = df.loc[df[column] == label]
240
+ if len(df_temp) > 5:
241
+ df_temp =df_temp[0:5]
242
+ stat.append(df_temp)
243
+
244
+ exemple_df= pd.concat(stat)
245
+
246
+ #stat =stat.reset_index(drop=True) just in case u want to reset indexing
247
+
248
+ return exemple_df
249
+
250
+
251
+ def export_to_download(_data_frame,_type:str ):
252
+
253
+ downloads_path = str(Path.home()) + "/Downloads/"
254
+ if _type == "one_leader":
255
+ file_name = _data_frame['username'].to_list()[0] #df['username'][0] + "_data"
256
+ else:
257
+ file_name = "selected_leaders"
258
+ full_path = downloads_path + file_name+".csv"
259
+
260
+ while full_path in glob.glob(downloads_path + "*"):
261
+ search_list = re.findall('\p{N}+', full_path)
262
+ if search_list:
263
+ index = search_list[0]
264
+ full_path = re.sub(index, str(int(index) + 1), full_path)
265
+ else:
266
+ suffix = " (1).csv"
267
+ full_path = re.sub('\.csv', suffix, full_path)
268
+
269
+ _data_frame.to_csv(full_path, index=False)
270
+
271
+
272
+
273
+
274
+
275
+
276
+ # , pie_chart(df, "main_topic"), pie_chart("target")
277
+ def piechart_input(df, column, limit):
278
+ df_len = len(df)
279
+ df_v = df[column].value_counts()
280
+ df_len = len(df)
281
+ if column == "sentiment":
282
+ ds_sentiment = df[column].apply(lambda x: re.sub("\s+", "", str(x)))
283
+ df_v = ds_sentiment.apply(lambda x: x if str(x).lower() == "positive" or str(x).lower() == "negative" or str(x).lower() == "neutral" else "other").value_counts()
284
+ elif column == "merged_target":
285
+ ds_target = df[column].apply(lambda x: "other" if x == "ERROR_9000" or x == "ERROR_496" else x)
286
+ df_v = ds_target.value_counts()
287
+ freq = df_v.to_list()
288
+ labels= df_v.keys().to_list
289
+ freq_dict = {}
290
+ freq_dict[column] = labels
291
+ freq_dict["frequency"] = freq
292
+ return pd.DataFrame.from_dict(freq_dict)
293
+
294
+ else:
295
+ df_v = df[column].value_counts()
296
+ freq = df_v.to_list()
297
+ labels = df_v.keys().to_list()
298
+ freq_other = 0
299
+ freq_dict = {column: [], "frequency": []}
300
+ for i in range(len(df_v)):
301
+ if freq[i] / df_len < limit:
302
+ freq_other += freq[i]
303
+ else:
304
+ freq_dict[column].append(labels[i])
305
+ freq_dict["frequency"].append(freq[i])
306
+
307
+ if "other" not in freq_dict[column]:
308
+ freq_dict[column].append("other")
309
+ freq_dict["frequency"].append(freq_other)
310
+ else:
311
+ ind_other = freq_dict[column].index("other")
312
+ freq_dict["frequency"][ind_other] += freq_other
313
+
314
+ return pd.DataFrame.from_dict(freq_dict)
315
+
316
+
317
+ def convert_to_boolean(leaders, plot_choices):
318
+ leaders_converted = [True if leader in leaders else False for leader in USER_NAMES]
319
+ plot_converted = [True if choice in plot_choices else False for choice in CHOICE_LIST]
320
+
321
+ return leaders_converted, plot_converted
322
+
323
 
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+
336
+ def update_window(leaders: list, plot_choices: list,
337
+ v1, v2, v3, v4, v5, v6, v7, v8
338
+ ):
339
+
340
+
341
+ leader_bool_list, plot_bool_list = convert_to_boolean(leaders, plot_choices)
342
+
343
+ bool_list = []
344
+ df_visiblity_bool = [v1, v2, v3, v4, v5, v6, v7, v8]
345
+
346
+
347
+ #this loop sets boolean for plots
348
+ for leader in leader_bool_list:
349
+ if leader:
350
+ for choice in plot_bool_list:
351
+ bool_list.append(choice)
352
+ #bool_list.append(True) ## this is for radio component
353
+ else:
354
+ for i in range(len(plot_bool_list)):
355
+ bool_list.append(False)
356
+ #bool_list.append(False)
357
+
358
+ update_blocks = []
359
+ update_plots = []
360
+ update_radio = []
361
+ update_nr_tweet =[]
362
+ update_checkbox = []
363
+ update_save_file_checkboxes =[]
364
+ update_df = []
365
+
366
+ #all_visual = block_list + plots + radio_list + nr_tweet_list + checkbox_list + saving_file_checkboxes + df_list
367
+
368
+
369
+ for i, vis_or_not in enumerate(leader_bool_list):
370
+ update_blocks.append(gr.Row.update(visible=vis_or_not))
371
+ update_blocks.append(gr.Row.update(visible=vis_or_not))
372
+ if vis_or_not:
373
+ update_blocks.append(gr.Row.update(visible=df_visiblity_bool[i]))
374
+ update_df.append(gr.DataFrame.update(visible=df_visiblity_bool[i]))
375
+ else:
376
+
377
+ update_blocks.append(gr.Row.update(visible=False ))
378
+ update_df.append(gr.DataFrame.update(visible= False ))
379
+
380
+ update_nr_tweet.append( gr.Number.update(visible = vis_or_not) )
381
+ update_radio.append(gr.Radio.update(visible=vis_or_not))
382
+ update_checkbox.append(gr.Checkbox.update(visible=vis_or_not))
383
+ update_save_file_checkboxes.append(gr.Checkbox.update(visible=vis_or_not))
384
+ for choice in bool_list:
385
+ update_plots.append(gr.Plot.update(visible=choice))
386
+
387
+ return update_blocks + update_plots + update_radio + update_nr_tweet + update_checkbox + update_save_file_checkboxes + update_df
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+ def add_plots(user):
403
+ plot_list = []
404
+ for plot_type in PLOT_CHOICES_DICT:
405
+ plot_list.append(gr.Plot(label=plot_type+ " for " + user, visible=False))
406
+ return plot_list
407
+
408
+
409
+
410
+
411
+
412
+ def add_nbr_boxes():
413
+ return [gr.Number(value=0, label='Tweets by ' + user, visible=False) for user in USER_NAMES]
414
 
415
 
416
  if __name__ == "__main__":
417
+ import gradio as gr
418
+ demo = gr.Blocks(title='Politweet')
419
+ with demo:
420
+ with gr.Column():
421
+ with gr.Row():
422
+ with gr.Column():
423
+ with gr.Row():
424
+ date1 = gr.Textbox(label="From", value='2022-05-10')
425
+ date2 = gr.Textbox(label="To", value='2022-05-30')
426
+ leaders = gr.Checkboxgroup(choices=USER_NAMES,
427
+ label="")
428
+ plot_choices = gr.CheckboxGroup(choices=CHOICE_LIST, label='Choose what to show')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429
 
430
+ save_selected_data_checkbox= gr.Checkbox(label="Export selected data")
431
+ with gr.Row():
432
+ update = gr.Button('Apply')
433
+ btn = gr.Button("Run")
434
+
435
+ # show_stat = gr.Checkbox(label="Show full statistics", value=True)
436
+ # show_plots = gr.components.Checkbox(label='Show topics', value=True)
437
+ with gr.Column():
438
+ selected = gr.DataFrame(label="Summary statistics for the selected choices",
439
+ max_rows=None, visible=False)
440
+ # all_data = gr.components.DataFrame(label="Summary statistics of the total database", max_rows=None)
441
+
442
+ plots = []
443
+ radio_list = []
444
+ checkbox_list = []
445
+ df_list = []
446
+ block_list = []
447
+ saving_file_checkboxes =[]
448
+ nr_tweet_list = []
449
+ with gr.Column():
450
+ for i in range(len(USER_NAMES)):
451
+ block_list +=[gr.Row()] * 3
452
+ for i, leader in enumerate(USER_NAMES):
453
+ with gr.Row():
454
+ plots += add_plots(leader)
455
+ with gr.Row():
456
+ radio_list.append(gr.Radio(list(PLOT_CHOICES_DICT.keys()), visible=False ,interactive=True))
457
+ nr_tweet_list.append( gr.Number(visible=False) )
458
+ checkbox_list.append(gr.Checkbox(label="Show stats ",value=False,visible=False))
459
+ saving_file_checkboxes.append( gr.Checkbox(label= "Export file" , value=False , visible= False) )
460
+
461
+ with gr.Row():
462
+ df_list.append(gr.DataFrame(visible=False))
463
+
464
+
465
+ inp = [date1,
466
+ date2,
467
+ leaders,
468
+ plot_choices , save_selected_data_checkbox] + radio_list + checkbox_list + saving_file_checkboxes
469
+
470
+ output = plots + [save_selected_data_checkbox]+ df_list + nr_tweet_list + saving_file_checkboxes
471
+
472
+
473
+ all_visual = block_list + plots + radio_list + nr_tweet_list +checkbox_list + saving_file_checkboxes +df_list #+ df_list # df_comps
474
+
475
+ update_inp = [leaders, plot_choices] + checkbox_list
476
+
477
+
478
+ update.click(fn=update_window, inputs=update_inp, outputs=all_visual)
479
+
480
+ btn.click(fn=main, inputs=inp, outputs=output)
481
+ # input.change(fn=main, inputs=input, outputs=output)
482
  demo.launch(share=False)
483
+
484
+
485
+ #df= pd.read_csv(os.getcwd()+"/data/twitterdata.csv")
486
+
487
+ #https://51285.gradio.app