-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
85 lines (72 loc) · 2.73 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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
# data from crawler module
import news_crawler
# call to create a list with all available fetchers
articles = news_crawler.FetchList()
# app call
app = dash.Dash()
# app layout
#
colors = {'background':'#111111','text':'#ffffff'} # nice way of org. colors
article_style = dict(width = '20%', verticalAlign='top',
color = colors['text'], display='inline-block')
def create_provider_layout(providers):
html_out = {}
for news_provider in providers:
html_out[news_provider] = html.Div([html.H1(providers[news_provider]),
html.Details([html.Summary('See all:'),
dcc.Markdown(id=news_provider)], open = True )],
style = article_style)
return html_out
# layout
# this maybe confusing but useful, if more news providers are added to the news feed
# the individual layout off each provider is created by the 'create_provider_layout()'
# this returns a dictonary of created html, this way it could be added to the app layout
# by passing the key like: provider_layout['ny']
providers = {'cnn':'CNN', 'rt':'Russia Today', 'bbc':'BBC', 'aj':'Aljazeera'}
provider_layout = create_provider_layout(providers)
app.layout = html.Div([
html.Button(id='submit_bttn',
n_clicks=0,
children='Update',
style=dict(fontSize='24')), # add a submmit button
provider_layout['cnn'],
provider_layout['rt'],
provider_layout['bbc'],
provider_layout['aj'],
], style = dict(backgroundColor = colors['background']))
# callbacks for interactivity
@app.callback(Output('cnn', 'children'),
[Input('submit_bttn', 'n_clicks')])
def output(n):
cnn_text = ''
for article in articles.fetch_all(keys =['cnn']):
cnn_text = cnn_text + "\n" + article.to_markdown().content
return cnn_text
@app.callback(Output('rt', 'children'),
[Input('submit_bttn', 'n_clicks')])
def output_rt(n):
rt_text = ''
for article in articles.fetch_all(keys =['rt']):
rt_text = rt_text + "\n" + article.to_markdown().content
return rt_text
@app.callback(Output('bbc', 'children'),
[Input('submit_bttn', 'n_clicks')])
def output_bbc(n):
bbc_text = ''
for article in articles.fetch_all(keys =['bbc']):
bbc_text = bbc_text + "\n" + article.to_markdown().content
return bbc_text
@app.callback(Output('aj', 'children'),
[Input('submit_bttn', 'n_clicks')])
def output_aj(n):
aj_text = ''
for article in articles.fetch_all(keys =['aj']):
aj_text = aj_text + "\n" + article.to_markdown().content
return aj_text
# run
if __name__ == '__main__':
app.run_server()