Python web application framework for building reactive analytical web apps without JavaScript
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Dash provides special values and utilities for controlling callback execution, preventing component updates, and making partial modifications to data structures.
Prevent component updates in callback outputs.
class NoUpdate:
"""Sentinel value to prevent component property updates in callbacks."""
pass
no_update: NoUpdate # Singleton instance, alias for NoUpdate()Stop callback execution and prevent any updates.
class PreventUpdate(Exception):
"""
Exception to prevent callback execution and component updates.
Raise this exception in a callback to stop execution without
updating any outputs or triggering errors.
"""
passPerform partial updates on lists and dictionaries without replacing entire structures.
class Patch:
"""
Enable partial updates to lists and dictionaries in callback outputs.
Instead of replacing entire data structures, Patch allows you to
modify specific elements, append items, or make targeted changes.
"""
def __init__(self): ...
def append(self, item: Any) -> 'Patch':
"""Append item to end of list."""
def prepend(self, item: Any) -> 'Patch':
"""Add item to beginning of list."""
def insert(self, index: int, item: Any) -> 'Patch':
"""Insert item at specific index in list."""
def remove(self, item: Any) -> 'Patch':
"""Remove first occurrence of item from list."""
def clear(self) -> 'Patch':
"""Remove all items from list or dictionary."""
def __setitem__(self, key: Union[int, str], value: Any):
"""Set item at key/index."""
def __delitem__(self, key: Union[int, str]):
"""Delete item at key/index."""from dash import Dash, html, dcc, callback, Input, Output, no_update
app = Dash(__name__)
app.layout = html.Div([
dcc.Input(id='input1', placeholder='Type here...'),
dcc.Input(id='input2', placeholder='Or here...'),
html.Div(id='output1'),
html.Div(id='output2')
])
@callback(
[Output('output1', 'children'),
Output('output2', 'children')],
[Input('input1', 'value'),
Input('input2', 'value')]
)
def update_outputs(input1, input2):
# Only update output1 if input1 has a value
if input1:
output1_value = f'Input 1: {input1}'
else:
output1_value = no_update
# Only update output2 if input2 has a value
if input2:
output2_value = f'Input 2: {input2}'
else:
output2_value = no_update
return output1_value, output2_valuefrom dash import Dash, html, dcc, callback, Input, Output, PreventUpdate
app = Dash(__name__)
app.layout = html.Div([
dcc.Input(id='password', type='password', placeholder='Enter password'),
html.Div(id='secure-content')
])
@callback(
Output('secure-content', 'children'),
Input('password', 'value')
)
def show_secure_content(password):
if not password:
# Don't update anything if no password entered
raise PreventUpdate
if password != 'secret123':
return html.Div('Incorrect password', style={'color': 'red'})
return html.Div([
html.H2('Secure Content'),
html.P('This is confidential information.')
])from dash import Dash, html, dcc, callback, Input, Output, State, Patch
app = Dash(__name__)
app.layout = html.Div([
dcc.Store(id='list-store', data=['Item 1', 'Item 2']),
dcc.Input(id='new-item', placeholder='Add new item'),
html.Button('Add', id='add-btn'),
html.Button('Clear All', id='clear-btn'),
html.Div(id='item-list')
])
@callback(
Output('list-store', 'data'),
[Input('add-btn', 'n_clicks'),
Input('clear-btn', 'n_clicks')],
[State('new-item', 'value'),
State('list-store', 'data')],
prevent_initial_call=True
)
def update_list(add_clicks, clear_clicks, new_item, current_list):
from dash import callback_context
if not callback_context.triggered:
raise PreventUpdate
trigger = callback_context.triggered[0]['prop_id']
patched_list = Patch()
if 'add-btn' in trigger and new_item:
patched_list.append(new_item)
return patched_list
elif 'clear-btn' in trigger:
patched_list.clear()
return patched_list
raise PreventUpdate
@callback(
Output('item-list', 'children'),
Input('list-store', 'data')
)
def display_list(items):
if not items:
return html.P('No items in list')
return html.Ul([
html.Li(item) for item in items
])from dash import Dash, html, dcc, callback, Input, Output, State, Patch
app = Dash(__name__)
app.layout = html.Div([
dcc.Store(id='dict-store', data={'count': 0, 'name': 'App'}),
html.Button('Increment Count', id='increment-btn'),
dcc.Input(id='name-input', placeholder='Update name'),
html.Button('Update Name', id='name-btn'),
html.Div(id='dict-display')
])
@callback(
Output('dict-store', 'data'),
[Input('increment-btn', 'n_clicks'),
Input('name-btn', 'n_clicks')],
[State('name-input', 'value'),
State('dict-store', 'data')],
prevent_initial_call=True
)
def update_dict(increment_clicks, name_clicks, new_name, current_dict):
from dash import callback_context
if not callback_context.triggered:
raise PreventUpdate
trigger = callback_context.triggered[0]['prop_id']
patched_dict = Patch()
if 'increment-btn' in trigger:
patched_dict['count'] = current_dict['count'] + 1
return patched_dict
elif 'name-btn' in trigger and new_name:
patched_dict['name'] = new_name
return patched_dict
raise PreventUpdate
@callback(
Output('dict-display', 'children'),
Input('dict-store', 'data')
)
def display_dict(data):
return html.Div([
html.P(f"Name: {data.get('name', 'Unknown')}"),
html.P(f"Count: {data.get('count', 0)}")
])@callback(
[Output('output1', 'children'),
Output('output2', 'style'),
Output('output3', 'className')],
Input('trigger', 'value')
)
def conditional_updates(value):
# Update all outputs
if value == 'update_all':
return 'Updated!', {'color': 'green'}, 'success'
# Update only first output
elif value == 'update_first':
return 'First updated!', no_update, no_update
# Update only styling
elif value == 'update_style':
return no_update, {'color': 'blue'}, 'info'
# No updates
else:
raise PreventUpdateNoUpdate = Any # Sentinel type for preventing updates
PreventUpdate = Exception # Exception type for preventing callback execution
Patch = Any # Partial update utility typeInstall with Tessl CLI
npx tessl i tessl/pypi-dash