-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_callbacks.py
53 lines (46 loc) · 1.64 KB
/
dash_callbacks.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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from pandas._config.config import options
import plotly.graph_objs as go
import plotly.express as px
import pandas as pd
df = pd.read_csv('gapminderDataFiveYear.csv')
app = dash.Dash()
year_option = []
for year in df['year'].unique():
year_option.append({'label':str(year), 'value':year})
app.layout = html.Div([
dcc.Graph(id='graph'),
dcc.Dropdown(id='year-picker', options=year_option,
value=df['year'].min() )
])
@app.callback(Output('graph', 'figure'),
[Input('year-picker', 'value')])
def update_figure(selected_year):
df = pd.read_csv('gapminderDataFiveYear.csv')
filtered_df = df[df['year'] == selected_year]
fig = go.Figure()
traces = []
for continent_name in filtered_df['continent'].unique():
df_by_continent = filtered_df[filtered_df['continent'] == continent_name]
traces.append(go.Scatter(
x = df_by_continent['gdpPercap'],
y = df_by_continent['lifeExp'],
mode = 'markers',
opacity = 0.7,
marker = {'size': 15},
name = continent_name
)
)
return {'data':traces,
'layout':go.Layout(title = 'My Plot',
xaxis={'title':'GDP Per Cap','type':'log'},
yaxis = {'title': 'Life Expectancy'})}
'''
fig = px.line(df, x=df_by_continent['gdpPercap'], y=df_by_continent['lifeExp'], title='Life expectancy in Canada')
return [fig]
'''
if __name__ == '__main__':
app.run_server()