Skip to content

Instantly share code, notes, and snippets.

@willgdjones
Created June 6, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willgdjones/48eb304bf603ec3e08f8245326a159e9 to your computer and use it in GitHub Desktop.
Save willgdjones/48eb304bf603ec3e08f8245326a159e9 to your computer and use it in GitHub Desktop.
A small, reproducible example that demonstrates the behaviour described in: https://community.plot.ly/t/multiple-callbacks-executed-by-a-single-input-change/10673
import numpy as np
import pandas as pd
from datetime import datetime as dt
import pytz
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from plotly import tools
app = dash.Dash('auth', url_base_pathname='/')
CHOICES = [
{'label': 1, 'value': 'Option 1'},
{'label': 2, 'value': 'Option 2'}
]
app.layout = html.Div(children=[
html.Div(
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=dt(2017, 10, 1),
max_date_allowed=dt(2018, 8, 1),
initial_visible_month=dt(2018, 3, 1),
display_format='YYYY/MM/DD',
start_date='2018-03-04',
end_date='2018-03-06'
), style={'text-align': 'center'}
),
html.Div(
dcc.Dropdown(
id='extruder-choice',
options=CHOICES,
value='h74_1_extruder_2_massflow'
)
),
html.Div(
dcc.Graph(
id='time-series-figure',
config={'modeBarButtonsToRemove': ['sendDataToCloud']}
),
style={
'height': '1000px',
}
),
])
@app.callback(
dash.dependencies.Output('time-series-figure', 'figure'),
[dash.dependencies.Input('my-date-picker-range', 'start_date'),
dash.dependencies.Input('my-date-picker-range', 'end_date'),
dash.dependencies.Input('extruder-choice', 'value')]
)
def update_daterange(startdate, enddate, extruder):
date_range = [startdate, enddate]
import pdb; pdb.set_trace()
return refresh(date_range, extruder)
app.config.supress_callback_exceptions = True
app.config.update({
# remove the default of '/'
'routes_pathname_prefix': '',
# remove the default of '/'
'requests_pathname_prefix': ''
})
x = pd.date_range(start='2018-01-01', end='2018-06-04', freq='H')
y = 8 + 5*np.random.random(len(x))
# import pdb; pdb.set_trace()
def refresh(date_range, extruder):
startdate, enddate = date_range
time_series_fig = tools.make_subplots(
rows=1, cols=1,
specs=[
[{}],
]
)
selection_idx = np.bitwise_and(
x > dt.strptime(startdate, '%Y-%m-%d'),
x < dt.strptime(enddate, '%Y-%m-%d')
)
x_selection = x[selection_idx]
y_selection = y[selection_idx]
trace = go.Scatter(
x=x_selection,
y=y_selection,
marker=dict(
symbol='x',
color='black',
size=3,
)
)
time_series_fig.append_trace(trace, 1, 1)
return time_series_fig
if __name__ == '__main__':
app.run_server(debug=True, port=8051)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment