I have developed a Dash application that I’m generally pleased with. At this point, I am looking to optimize its performance. While the documentation advises against using client-side callbacks with database queries, I am curious about their most effective application scenarios.
For instance, I have several features, such as modal toggles, dynamic column definition updates in AG-Grid, and dcc.Store
updates, that are currently managed through server-side callbacks.
Here’s how I’m implementing these features:
- Modal Openings:
@callback(
Output("p-modal", "is_open"),
Input("p-modal-button", "n_clicks"),
State("p-modal", "is_open"),
)
def toggle_modal_pp(n1, is_open):
if n1:
return not is_open
return is_open
- Updating Column Definitions in AG-Grid:
@callback(
Output("m_data_table", "columnDefs"),
Output("m_data_table", "columnSize"),
Input("m-toggle", "value"),
prevent_initial_call=True,
)
def toggle_cols(value):
if 2 in value:
return columns_methodCDefs_wide, "autoSize"
return columns_methodCDefs_narrow, "SizeToFit"
- Updating
dcc.Store
, with two examples:
@callback(
Output('dropdown-store', 'data'),
Input('dropdown', 'value'),
prevent_initial_call=True
)
def on_api_choice(selected_value):
if not selected_value:
raise PreventUpdate
return selected_value
@callback(
Output('current-url', 'data'),
Input('dropdown1', 'value'),
Input('dropdown1', 'value'),
prevent_initial_call=True,
)
def update_upper_filters(dd1, dd2):
if dd1 is None:
return None
params = [
('dd1', quote(concept) if dd1 else []),
*((('dd2', quote(v)) for v in dd2) if strength else []),
]
params = [item for item in params if item is not None and item[1] is not None]
dds = "&".join(f"{k}={v}" for k, v in params)
return dds
I’m interested in understanding when it’s most appropriate to migrate these to client-side callbacks. Are there specific conditions or types of interactions that are better suited for client-side logic? Also, are there performance benchmarks or case studies illustrating the impact of using client-side callbacks for similar operations?