I’m creating an app where I want to poll a database every 10 seconds to see if new records are available and dynamically update a dropdown. However, the way I have it set up now, the callback updates the options
value AND resets the value
the user has chosen:
@app.callback(Output('reflapmenu', 'options'),
[Input('leaderboard-interval', 'n_intervals')])
def build_session_dropdown(notused):
#placing connection inside to avoid having stale connection
conn = <database connection omitted>
df = pd.read_sql("""select
sessionuid,
lapnumber,
laptime,
playercarindex
from v_leaderboard_melbourne
where laptime >= 60
order by laptime
limit 50
""", conn)
#formatting for session column to make table width smaller
df["session"] = [f"""S{x[-4:]}""" for x in df["sessionuid"]]
options = [{'label': f"""{uid} - Lap {lnum} - {lapt}""", 'value': uid} for uid, lnum, lapt in
zip(df["session"], df["lapnumber"], df["laptime"])]
return options
#### reference lap: build list dynamically
reflapmenu = dcc.Dropdown(
id='reflapmenu',
searchable=False,
clearable=False,
style=dict(width = '275px')
)
As it stands now, on page load the dropdown does not have a value chosen (the dropdown shows blank, waiting for the user to choose). The user can then choose a value, but when the callback fires, it updates the options
argument and resets the state of the value
to blank…I would like only the options
field to update (i.e. give the user more choices when they become available), but don’t change their current selection.
Is this possible? Do I have to stash their current choice somehow?