Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created March 5, 2020 16:14
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 randyzwitch/69881ac0f555c74654e5b598701e462b to your computer and use it in GitHub Desktop.
Save randyzwitch/69881ac0f555c74654e5b598701e462b to your computer and use it in GitHub Desktop.
Callback example in Dash
#### THIS IS A SUMMARY, THIS FILE WILL NOT RUN AS-IS ####
#### INPUTS to build_telemetry_chart ####
#https://github.com/omnisci/F1-demo/blob/726c56ba4e8c878abeef81b7cf680bd4d36a4bd2/f1dash/controls.py#L9-L32
#### reference lap: build list dynamically with callback
reflapmenu = dcc.Dropdown(
id='reflapmenu',
searchable=False,
clearable=False,
style=dict(minWidth = '250px', paddingRight = '15px', borderRadius = 0)
)
#### telemetry metrics: these are the reasonable values from the table
metricmenu = dcc.Dropdown(
id='metricmenu',
options=[
{'label': 'speed', 'value': 'speed'},
{'label': 'enginerpm', 'value': 'enginerpm'},
{'label': 'brake', 'value': 'brake'},
{'label': 'gear', 'value': 'gear'},
{'label': 'steer', 'value': 'steer'},
{'label': 'throttle', 'value': 'throttle'}
],
value="speed",
searchable=False,
clearable=False,
style=dict(minWidth = '250px', paddingRight = '15px', borderRadius = 0, color="#FFFFFF")
)
#### Listens for the 'value' field of 'reflapmenu' and 'value' field of 'metricmenu' to change
#### Runs build_telemetry_chart function, which sends its output to 'telemetry-graph' id
#https://github.com/omnisci/F1-demo/blob/726c56ba4e8c878abeef81b7cf680bd4d36a4bd2/f1dash/app.py#L182-L185
#callback decorator
@app.callback(Output('telemetry-graph', 'figure'),
[Input('track-interval', 'n_intervals'), Input('reflapmenu', 'value'), Input('metricmenu', 'value')])
def build_telemetry_chart(notused, reflapvalue, metric):
...
#### This code has 'telemetry-graph' as its id, so the results of build_telemetry_chart modifies the 'figure' field of 'telgraph'
#https://github.com/omnisci/F1-demo/blob/726c56ba4e8c878abeef81b7cf680bd4d36a4bd2/f1dash/telemetry.py#L31-L35
telgraph = dcc.Graph(id='telemetry-graph',
config={
'displayModeBar': True
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment