-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_side.py
88 lines (74 loc) · 2.31 KB
/
client_side.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
from dash import Dash, dcc, html, Input, Output
import pandas as pd
import json
import plotly.express as px
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
available_countries = df['country'].unique()
app.layout = html.Div([
dcc.Graph(
id='clientside-graph-px'
),
dcc.Store(
id='clientside-figure-store-px'
),
'Indicator',
dcc.Dropdown(
{'pop' : 'Population', 'lifeExp': 'Life Expectancy', 'gdpPercap': 'GDP per Capita'},
'pop',
id='clientside-graph-indicator-px'
),
'Country',
dcc.Dropdown(available_countries, 'Canada', id='clientside-graph-country-px'),
'Graph scale',
dcc.RadioItems(
['linear', 'log'],
'linear',
id='clientside-graph-scale-px'
),
html.Hr(),
html.Details([
html.Summary('Contents of figure storage'),
dcc.Markdown(
id='clientside-figure-json-px'
)
])
])
@app.callback(
Output('clientside-figure-store-px', 'data'),
Input('clientside-graph-indicator-px', 'value'),
Input('clientside-graph-country-px', 'value')
)
def update_store_data(indicator, country):
dff = df[df['country'] == country]
return px.scatter(dff, x='year', y=str(indicator))
app.clientside_callback(
"""
function(figure, scale) {
if(figure === undefined) {
return {'data': [], 'layout': {}};
}
const fig = Object.assign({}, figure, {
'layout': {
...figure.layout,
'yaxis': {
...figure.layout.yaxis, type: scale
}
}
});
return fig;
}
""",
Output('clientside-graph-px', 'figure'),
Input('clientside-figure-store-px', 'data'),
Input('clientside-graph-scale-px', 'value')
)
@app.callback(
Output('clientside-figure-json-px', 'children'),
Input('clientside-figure-store-px', 'data')
)
def generated_px_figure_json(data):
return '```\n'+json.dumps(data, indent=2)+'\n```'
if __name__ == '__main__':
app.run_server(debug=True)