import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output app = dash.Dash(__name__) app.layout = html.Div([ dcc.Graph(id='graph'), html.Label('Select a city:'), dcc.Dropdown( id='city-dropdown', options=[ {'label': 'New York City', 'value': 'nyc'}, {'label': 'Los Angeles', 'value': 'la'}, {'label': 'Chicago', 'value': 'chi'} ], value='nyc' ) ]) @app.callback( Output('graph', 'figure'), [Input('city-dropdown', 'value')] ) def update_graph(selected_city): # Get data for selected city data = get_data(selected_city) # Create a new figure with updated data fig = create_fig(data) return fig if __name__ == '__main__': app.run_server()
import dash import dash_html_components as html from dash.dependencies import Input, Output import datetime app = dash.Dash(__name__) app.layout = html.Div([ html.H1(id='time-container', children='Current Time'), html.Button('Start', id='start-button', n_clicks=0) ]) @app.callback( Output('time-container', 'children'), [Input('start-button', 'n_clicks')] ) def update_time(n_clicks): if n_clicks == 0: return 'Current Time' else: return datetime.datetime.now().strftime('%H:%M:%S') if __name__ == '__main__': app.run_server()In this example, we have a button component that starts a timer when clicked. The `update_time` function is called every time the button is clicked, and it returns the current time as a string. This updates the content of an H1 component in the web application with the current time. Package library: `dash, dash_html_components`