-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
466 lines (385 loc) · 25.8 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
from h2o_wave import main, app, Q, ui, on, run_on, data
from typing import Optional, List
import pandas as pd
import plotly.express as px
import io
import base64
import matplotlib.pyplot as plt
from plotly import io as pio
df = pd.read_excel('static/all_news.xlsx')
df['Industry sector'] = df['Industry sector'].str.split(', ')
df = df.explode('Industry sector').reset_index(drop=True)
df['News source'] = df['News source'].str.split(', ')
df = df.explode('News source').reset_index(drop=True)
lins = []
for item in df['News source']:
if item == 'Economic Times' or item == 'The Economic Times' or item == 'Economic Times (ET)' or item =='Economic Times.' or item =='ET (Economic Times)':
lins.append('Economic Times')
elif item == 'Not specified' or item == 'Not Mentioned' or item == 'Not mentioned.':
lins.append('Not Specified')
elif item == 'Financial news outlet.':
lins.append('Financial news outlet')
else:
lins.append(item)
df['News source'] = lins
df['Target audience'] = df['Target audience'].str.split(', ')
df_expanded = df.explode('Target audience').reset_index(drop=True)
target_audience_distribution = df_expanded['Target audience'].value_counts().reset_index()
target_audience_distribution.columns = ['Target Audience', 'Count']
df['date'] = pd.to_datetime(df['date'], format='mixed')
industry_distribution = df['Industry sector'].value_counts().reset_index()
industry_distribution.columns = ['Industry', 'Count']
# Subsector Distribution
subsector_distribution = df['Subsector'].value_counts().reset_index()
subsector_distribution.columns = ['Subsector', 'Count']
# Use for page cards that should be removed when navigating away.
# For pages that should be always present on screen use q.page[key] = ...
def add_card(q, name, card) -> None:
q.client.cards.add(name)
q.page[name] = card
# Remove all the cards related to navigation.
def clear_cards(q, ignore: Optional[List[str]] = []) -> None:
if not q.client.cards:
return
for name in q.client.cards.copy():
if name not in ignore:
del q.page[name]
q.client.cards.remove(name)
@on('#intro')
async def page_intro(q: Q):
q.page['sidebar'].value = '#intro'
clear_cards(q) # When routing, drop all the cards except of the main ones (header, sidebar, meta).
add_card(q, 'article', ui.tall_article_preview_card(
box=ui.box('vertical', height='650px'), title='News Analysis',
image='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaMAAAB4CAMAAABoxW2eAAAAxlBMVEX////G0P9eR4y8vu2ajLe/xfTI0/98creCd7iEertaQorK1f9YP4iHf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FglAOE9j/ZpcOyBl0NhWo1Sc92rr555XgetGp64nlO/ryClxRnL9WYLQfD/iL7mzU+n1tU8ScsRpApJ+3l19+Pub+h+AdKpWru54bT1bm3aWdF8t0KZallPKN/S8D0qmXFGVKHth2h0Lcr1pB+chThhuTEGNIg78oVQjI69x2WvvmFN4UYsp76TH3vy73+Rfo//dIJu32BooAAAAAASUVORK5CYII=',
content='''
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac sodales felis. Duis orci enim, iaculis at augue vel, mattis imperdiet ligula. Sed a placerat lacus, vitae viverra ante. Duis laoreet purus sit amet orci lacinia, non facilisis ipsum venenatis. Duis bibendum malesuada urna. Praesent vehicula tempor volutpat. In sem augue, blandit a tempus sit amet, tristique vehicula nisl. Duis molestie vel nisl a blandit. Nunc mollis ullamcorper elementum.
Donec in erat augue. Nullam mollis ligula nec massa semper, laoreet pellentesque nulla ullamcorper. In ante ex, tristique et mollis id, facilisis non metus. Aliquam neque eros, semper id finibus eu, pellentesque ac magna. Aliquam convallis eros ut erat mollis, sit amet scelerisque ex pretium. Nulla sodales lacus a tellus molestie blandit. Praesent molestie elit viverra, congue purus vel, cursus sem. Donec malesuada libero ut nulla bibendum, in condimentum massa pretium. Aliquam erat volutpat. Interdum et malesuada fames ac ante ipsum primis in faucibus. Integer vel tincidunt purus, congue suscipit neque. Fusce eget lacus nibh. Sed vestibulum neque id erat accumsan, a faucibus leo malesuada. Curabitur varius ligula a velit aliquet tincidunt. Donec vehicula ligula sit amet nunc tempus, non fermentum odio rhoncus.
Vestibulum condimentum consectetur aliquet. Phasellus mollis at nulla vel blandit. Praesent at ligula nulla. Curabitur enim tellus, congue id tempor at, malesuada sed augue. Nulla in justo in libero condimentum euismod. Integer aliquet, velit id convallis maximus, nisl dui porta velit, et pellentesque ligula lorem non nunc. Sed tincidunt purus non elit ultrices egestas quis eu mauris. Sed molestie vulputate enim, a vehicula nibh pulvinar sit amet. Nullam auctor sapien est, et aliquet dui congue ornare. Donec pulvinar scelerisque justo, nec scelerisque velit maximus eget. Ut ac lectus velit. Pellentesque bibendum ex sit amet cursus commodo. Fusce congue metus at elementum ultricies. Suspendisse non rhoncus risus. In hac habitasse platea dictumst.
'''
))
@on('#data-frame-analysis')
async def page_df(q: Q):
q.page['sidebar'].value = '#data-frame-analysis'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q)
table_rows = []
for index, row in df.iterrows():
table_rows.append(ui.table_row(
name=row['title'],
cells=[row['title'], row['News source'],] # Adjust these indices based on your CSV columns
))
add_card(q, 'table', ui.form_card(box='vertical', items=[ui.table(
name='table',
downloadable=True,
resettable=True,
groupable=True,
columns=[
ui.table_column(name='Title', label='Title', searchable=True,min_width='500'),
ui.table_column(name='News Source', label='News Source', filterable=True, min_width='1000',cell_type=ui.tag_table_cell_type(name='tags', tags=[
ui.tag(label='RUNNING', color='#D2E3F8'),
ui.tag(label='DONE', color='$red'),
ui.tag(label='SUCCESS', color='$mint'),
]
))
],
events = ['click'],
rows=table_rows)
]))
@on('table')
async def handle_table_click(q: Q):
table_rows = []
for index, row in df.iterrows():
table_rows.append(ui.table_row(
name=row['title'],
cells=[row['title'], row['News source'],] # Adjust these indices based on your CSV columns
))
print(q.args.table)
if q.args.table:
q.client.selected_actor = q.args.table[0]
q.args['#'] = 'data-frame-analysis'
await page_df(q)
@on('#column-analysis')
async def pageca(q: Q):
print('Handling page4')
q.page['sidebar'].value = '#column-analysis'
clear_cards(q)
column_names = df.columns.tolist()
if q.args.plot_button:
print('Plot button clicked')
selected_column = q.args.selected_column
print(f'Plotting graph for {selected_column}')
plot_fig = plot_categorical_graph(df, selected_column)
# Add a card to display the plot
config = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html = pio.to_html(plot_fig, validate=False, include_plotlyjs='cdn', config=config)
add_card(q, 'col1', ui.form_card(box=ui.box('zone1'), title='', items=[
ui.frame(content=html, height='1000px', width='1500px')]))
else:
print('Rendering dropdown menu')
# Add a dropdown to select a column
dropdown_card = ui.form_card(box=ui.box('horizontal'), items=[
ui.dropdown(name='selected_column', label='Select Column', choices=[ui.choice(name=col, label=col) for col in column_names]),
ui.button(name='plot_button', label='Submit', primary=True),
])
add_card(q, 'dropdown_card', dropdown_card)
@on('#industry-sector-sentiment-analysis')
async def page_ind(q: Q):
q.page['sidebar'].value = '#industry-sector-sentiment-analysis'
clear_cards(q) # When routting, drop all the cards except of the main ones (header, sidebar, meta).
'''
add_card( q, 'dataframe', ui.form_card(box='zone2', items=[
# modify heading here (content)
ui.text_xl(content='Data Frame Head'),
ui.table(
name='table',
columns=[ui.table_column(name=i, label=i, min_width='200',cell_type=ui.markdown_table_cell_type(target='_blank')) for i in df.columns],
height='400px',
rows=[ui.table_row(name=f'row{i}', cells=list(str(i) for i in df.values[i])) for i in range(100)],
)
]))
'''
# Identify the top 10 industry sectors with positive sentiment
# Assuming you have a 'Sentiment' column in your DataFrame
positive_rows = df[df['Sentiment'].str.lower().str.contains('positive')]
# Extract the top 10 industry sectors with positive sentiment
# Assuming you already have 'positive_rows' DataFrame
# Extract the top 10 industry sectors with positive sentiment
positive_industries = positive_rows['Industry sector'].value_counts().head(10)
positive_industries = positive_industries[1:]
# Create a pie chart using plotly express
fig = px.pie(positive_industries,
names=positive_industries.index,
values=positive_industries.values,
title='Top 10 Industry Sectors with Positive Sentiment')
config = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html = pio.to_html(fig, validate=False, include_plotlyjs='cdn', config=config)
add_card(q, 'piechart1', ui.frame_card(box='horizontal', title='', content=html))
# Identify the top 10 industry sectors with negative sentiment
# Assuming you have a 'Sentiment' column in your DataFrame
negative_rows = df[df['Sentiment'].str.lower().str.contains('negative')]
# Extract the top 10 industry sectors with negative sentiment
negative_industries = negative_rows['Industry sector'].value_counts().head(10)
add_card(q, 'dataframe3', ui.form_card(box='horizontal', items=[
ui.text_xl(content='Top 10 Industry Sectors with Most Negative Sentiment'),
ui.table(
name='negative_table',
columns=[
ui.table_column(name='Industry Sector', label='Industry Sector', min_width='200'),
ui.table_column(name='Count', label='Negative Sentimental NewsCount', min_width='200')
],
rows=[ui.table_row(name = f'count{count}',cells=[sector , str(count)]) for sector, count in negative_industries.items() if sector != '0'],
height='400px',
)
]))
positive_news_source = positive_rows['News source'].value_counts().head(13)
del positive_news_source['0']
del positive_news_source['Not mentioned']
del positive_news_source['Not specified.']
fig = px.pie(positive_news_source,
names = positive_news_source.index,
values = positive_news_source.values,
title = 'Top 10 Positive News Sources')
config = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html = pio.to_html(fig, validate=False, include_plotlyjs='cdn', config=config)
add_card(q, 'piechart2', ui.frame_card(box='zone1', title='', content=html))
negative_news_source = negative_rows['News source'].value_counts().head(10)
add_card(q, 'datafram2', ui.form_card(box='zone1', items=[
ui.text_xl(content='Top 10 Negative News Sources'),
ui.table(
name='negative_table_News_source',
columns=[
ui.table_column(name='Industry Sector', label='Industry Sector', min_width='200'),
ui.table_column(name='Count', label='Negative Sentimental NewsCount', min_width='200')
],
rows=[ui.table_row(name = f'count{count}',cells=[sector , str(count)]) for sector, count in negative_news_source.items() if sector != '0'],
height='400px'
)
]))
@on('#temporal-analysis')
async def page_temporal(q: Q):
q.page['sidebar'].value = '#temporal-analysis'
clear_cards(q) # When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Create a histogram using Plotly for temporal distribution
fig_temporal = px.histogram(df, x='date', nbins=30, labels={'date': 'Date', 'count': 'Number of Articles'})
fig_temporal.update_layout(title='Temporal Distribution of Articles', xaxis_title='Date', yaxis_title='Number of Articles')
config_temporal = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html_temporal = pio.to_html(fig_temporal, validate=False, include_plotlyjs='cdn', config=config_temporal)
add_card(q, 'temporal1', ui.form_card(box=ui.box('horizontal', width='750px'), title='', items=[
ui.frame(content=html_temporal, height='650px', width='650px')]))
# Create a grouped bar chart for industry representation
fig_industry = px.histogram(df, x='date', color='Industry sector', nbins=30,
labels={'date': 'Date', 'count': 'Number of Articles', 'Industry sector': 'Industry'})
fig_industry.update_layout(title='Industry Representation Over Time', xaxis_title='Date', yaxis_title='Number of Articles')
config_industry = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html_industry = pio.to_html(fig_industry, validate=False, include_plotlyjs='cdn', config=config_industry)
add_card(q, 'industry1', ui.form_card(box=ui.box('vertical', width='1500px'), title='', items=[
ui.frame(content=html_industry, height='650px', width='1500px')]))
@on('#ind-sub-analysis')
@on('page4_reset')
async def page4(q: Q):
q.page['sidebar'].value = '#ind-sub-analysis'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q, ['form'])
# Now df_expanded has each industry on a separate row
# Plot Industry Distribution
fig_industry = px.bar(industry_distribution, x='Industry', y='Count', title='Distribution of News Across Industries')
fig_industry.update_layout(xaxis_title='Industry', yaxis_title='Number of Articles')
fig_industry.update_traces(width=2)
config = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html = pio.to_html(fig_industry, validate=False, include_plotlyjs='cdn', config=config)
add_card(q, 'ind1', ui.form_card(box=ui.box('horizontal',width='1500px'), title='', items=[
ui.frame(content=html, height='1000px',width='1500px')]))
@on('#target-audience-analysis')
async def page_target_aud(q: Q):
q.page['sidebar'].value = '#target-audience-analysis'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q, ['form'])
# Plot Target Audience Distribution
fig_target_audience = px.bar(target_audience_distribution, x='Target Audience', y='Count', title='Target Audience Analysis')
# Increase bar width if needed
fig_target_audience.update_layout(yaxis_range=[0, 100])
fig_target_audience.update_traces(width=3)
config = {
'scrollZoom': False,
'showLink': False,
'displayModeBar': False
}
html = pio.to_html(fig_target_audience, validate=False, include_plotlyjs='cdn', config=config)
add_card(q, 'aud1', ui.form_card(box=ui.box('horizontal',width='1500px'), title='', items=[
ui.frame(content=html, height='1000px',width='1500px')]))
@on('#competitor-analysis')
async def page_comp(q: Q):
q.page['sidebar'].value = '#competitor-analysis'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q)
@on('#salary-analysis')
async def page_salary(q: Q):
q.page['sidebar'].value = '#salary-analysis'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q)
@on('#cross-industry-analysis')
async def page_cross(q: Q):
q.page['sidebar'].value = '#cross-industry-analysis'
# When routing, drop all the cards except of the main ones (header, sidebar, meta).
# Since this page is interactive, we want to update its card
# instead of recreating it every time, so ignore 'form' card on drop.
clear_cards(q)
def plot_categorical_graph(df, selected_column):
# Assuming data is present in a column named 'data'
fig = px.histogram(df, x=selected_column, title=f'Distribution of {selected_column}')
return fig
@on()
async def page4_step2(q: Q):
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2'),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox2', label='Textbox 2'),
ui.buttons(justify='end', items=[
ui.button(name='page4_step3', label='Next', primary=True),
])
]
@on()
async def page4_step3(q: Q):
# Just update the existing card, do not recreate.
q.page['form'].items = [
ui.stepper(name='stepper', items=[
ui.step(label='Step 1', done=True),
ui.step(label='Step 2', done=True),
ui.step(label='Step 3'),
]),
ui.textbox(name='textbox3', label='Textbox 3'),
ui.buttons(justify='end', items=[
ui.button(name='page4_reset', label='Finish', primary=True),
])
]
async def init(q: Q) -> None:
q.page['meta'] = ui.meta_card(box='', layouts=[ui.layout(breakpoint='xs', min_height='100vh', zones=[
ui.zone('main', size='1', direction=ui.ZoneDirection.ROW, zones=[
ui.zone('sidebar', size='300px'),
ui.zone('body', zones=[
ui.zone('header'),
ui.zone('content', zones=[
# Specify various zones and use the one that is currently needed. Empty zones are ignored.
ui.zone('horizontal', direction=ui.ZoneDirection.ROW,),
ui.zone('zone2',direction=ui.ZoneDirection.ROW ),
ui.zone('vertical'),
ui.zone('grid', direction=ui.ZoneDirection.ROW, wrap='stretch', justify='center'),
ui.zone(name='zone1', direction=ui.ZoneDirection.ROW),
ui.zone(name='zone3',direction=ui.ZoneDirection.COLUMN)
]),
]),
])
])])
q.page['sidebar'] = ui.nav_card(
box='sidebar', color='primary', title = 'News Data Analyzer', subtitle="",
value=f'#{q.args["#"]}' if q.args['#'] else '#intro',
image='', items=[
ui.nav_group('Menu', items=[
ui.nav_item(name='#intro', label='Home'),
ui.nav_item(name='#data-frame-analysis', label='Data Frame Analysis'),
ui.nav_item(name='#column-analysis', label='Column Analysis'),
ui.nav_item(name='#industry-sector-sentiment-analysis', label='Industry Sector Sentiment Analysis'),
ui.nav_item(name='#ind-sub-analysis', label='Industry and sub sector comparision'),
ui.nav_item(name='#temporal-analysis', label='Temporal Analysis'),
ui.nav_item(name='#target-audience-analysis', label='Target Audience Analysis'),
ui.nav_item(name='#competitor-analysis', label='Competitor Analysis'),
ui.nav_item(name='#salary-analysis',label='Salary Analysis'),
ui.nav_item(name='#cross-industry-analysis',label='Cross Industry Analysis'),
]),
],
)
q.page['header'] = ui.header_card(
box='header', title='', subtitle='',
)
# If no active hash present, render page1.
if q.args['#'] is None:
await page_intro(q)
@app('/')
async def serve(q: Q):
# Run only once per client connection.
if not q.client.initialized:
q.client.cards = set()
await init(q)
q.client.initialized = True
# Handle routing.
await run_on(q)
await q.page.save()