Hi community,
I’m building a simple dash app which retrieves data from an API (time series data) and displays it in a scatter plot. Here is a reduced example of what I’m trying to do:
# App layout
######################################################################################
app.layout = html.Div([
html.Div([
html.Div([
# first dropdown
html.Div([
dcc.Dropdown(
id='id-dropdown1',
options=[{'label': k, 'value': v} for k,v in dict1.items()],
placeholder="Select a value",
value="all"
)
],
style={'width': '25%', 'display': 'inline-block', 'marginTop': 25}),
# second dropdown
html.Div([
dcc.Dropdown(
id='id-dropdown2',
placeholder="Select a value",
value="all"
)
],
style={'width': '25%', 'display': 'inline-block', 'marginTop': 25}),
# button
html.Div([
html.Button(id='submit-button', n_clicks=0, children='Submit')],
style={'width':'25%', 'display':'inline-block', 'marginTop':25, 'float':'right'})
]),
]),
# graph to update
dcc.Graph(id='main-graph',style={'height':'550px'}),
])
######################################################################################
# Reactive interactions between dropdown selections
######################################################################################
@app.callback(
dash.dependencies.Output('id-dropdown2', 'options'),
[dash.dependencies.Input('id-dropdown1', 'value')])
def dropdown2_update_1(dd1_value):
# update options of dropdown2 depending on value of dropdown1
return [{"label":label, "value":value} for label,value in somedict.items()]
@app.callback(
dash.dependencies.Output('id-dropdown2', 'value'),
[dash.dependencies.Input('id-dropdown1', 'value')])
def dropdown2_update_2(segment_nbr):
# updated value of dropdown2 depending on value of dropdown1
return "all"
######################################################################################
# Reactive function to update TS graph
######################################################################################
@app.callback(
dash.dependencies.Output('main-graph', 'figure'),
[dash.dependencies.Input('submit-button', 'n_clicks')],
[dash.dependencies.State('id-dropdown1', 'value'),
dash.dependencies.State('id-dropdown2', 'value')])
def update_graph(n_clicks, dd1_value, dd2_value):
# retrieve data from API
data = retrieve_data(dd1_value, dd2_value)
print(data.info())
# filter the dataframes
trace0 = go.Scatter(x=data.ds,
y=data.y,
mode="lines+markers",
name="TS data",
opacity = 0.8,
marker = dict(color='rgb(0, 0, 0)'))
layout = dict(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=3,
label='3m',
step='month',
stepmode='backward'),
dict(step='all')
])
),
rangeslider=dict(
visible = True
),
type='date'
),
shapes=[{
'type':'line',
'line': {
'color': 'rgb(255, 0, 0)',
'width': 3,
},
}]
)
fig = dict(data=[trace0], layout=layout)
return fig
if __name__ == '__main__':
app.run_server(host='0.0.0.0')
In the above example, there are two dropdown, and the input of the first updates the options of the second. Also, the callback to update the Graph component is done by taking the States of the two Dropdowns, and it is fired through a button component.
The data variable is a pandas data frame and is correctly retrieved from the API (I can check that by printing it). However the Graph is not updating. Retrieving the data from the API takes between 20s to 100s.
Here is a sample output displayed by the server:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3169 entries, 0 to 3168
Data columns (total 2 columns):
ds 3169 non-null object
y 3169 non-null int64
dtypes: int64(1), object(1)
memory usage: 49.6+ KB
None
172.19.227.81 - - [07/Sep/2018 14:37:05] "POST /_dash-update-component HTTP/1.1" 200 -
It seems that the components is being updated, but no.
This is my current setup: Anaconda python 3. Dash installed from PyPI: dash-core-components (0.28.2), dash-html-components (0.12.0), dash (0.26.4) and dash_renderer (0.13.2).
Any help will be appreciated!