I have a dashboard in which 3 Data Tables are updated when a new date range is selected from the DateRangePicker. While the data is refreshing for each of the 3 data tables, the ‘default’ loading animation for dcc.Loading is displayed.
I recently added a Button which can update the DateRangePicker with a specific “start_date” and “end_date”. When the Button is clicked, the DateRangePicker updates properly, and the Data Tables refresh.
However, the loading animations do not play when I click the Button to update the DateRangePicker, which then automatically updates the Data Tables.
To be clear, if I use the DateRangePicker, the loading animations still play properly. But if I update the date range by clicking the Button (“Update 5.0”), then no loading animation is played, even though the data refreshes.
Relevant snippets from my code:
DateRangePicker
dcc.DatePickerRange(
id='my-date-picker-range',
min_date_allowed=date(2018, 12, 14),
max_date_allowed=date.today(),
start_date=date.today() - timedelta(days=7),
end_date=date.today(),
display_format='D/M/Y'
)
Data Table
dbc.Col(
html.Div(
children=[
dash_table.DataTable(
id='datatable-weapons',
columns=[{"name": 'Rank', "id": "ranking"},
{"name": 'Weapons', "id": 'weapon'},
{"name": 'Times Used', "id": 'times_used'},
{"name": '% Used', "id": 'pct_used'}
],
style_header={
'fontWeight': 'bold',
},
style_cell={
'textAlign':'center',
},
style_cell_conditional=[
{
'if': {'column_id': 'weapon'},
'textAlign': 'left',
}
],
style_as_list_view=True,
pagination_settings={
'current_page': 0,
'page_size': PAGE_SIZE
},
pagination_mode='be'
),
dcc.Loading(id="loading-1", children=[html.Div(id="loading-output-2")], type="default")
]
)
)
Callback for Button click to update DateRangePicker
@app.callback(
[Output('my-date-picker-range', 'start_date'), Output('my-date-picker-range', 'end_date')],
[Input('button-update-5', 'n_clicks')])
def update_output(n_clicks):
if n_clicks > 0:
start_date = date(2019, 2, 12)
end_date = date.today()
return start_date, end_date
Callback for one of the data tables
@app.callback(
[Output('datatable-weapons', 'data'), Output('weapon-download-link', 'href'), Output('loading-output-1', 'children')],
[Input('my-date-picker-range', 'start_date'), Input('my-date-picker-range', 'end_date'),
Input('range-slider', 'value'), Input('prestige-range-slider', 'value'), Input('datatable-weapons', 'pagination_settings')])
def update_graph(begin_dt, end_dt, rank_range, prestige_range, pagination_settings):
df = pd.read_sql(
create_sql('weapon', begin_dt, end_dt, rank_range[0], rank_range[1], prestige_range[0], prestige_range[1]), conn)
df["weapon"] = df["weapon"].astype(str)
df["times_used"] = df["times_used"].apply(lambda x: "{:,}".format(x))
df["pct_used"] = df["pct_used"].map('{:,.2f}%'.format)
csv_string = df.to_csv(index=False, encoding='utf-8')
csv_string = "data:text/csv;charset=utf-8," + quote(csv_string)
return df.iloc[
pagination_settings['current_page']*pagination_settings['page_size']:
(pagination_settings['current_page'] + 1)*pagination_settings['page_size']
].to_dict('rows'), csv_string, ''