# Import packages from dash import Dash, dash_table, dcc, html, Input, Output, State import dash_bootstrap_components as dbc import plotly.express as px # Setup data df = px.data.gapminder() dropdown_list = df['country'].unique() # Initialise the App app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) # Create app components markdown = dcc.Markdown(id='our-markdown', children='# My first app') dropdown = dcc.Dropdown(id='our-dropdown', options=dropdown_list, value=dropdown_list[0]) radio = dcc.RadioItems(id='our-radio', options=['line', 'scatter'], value='line') button = html.Button(id='our-button', children='Update data', n_clicks=0) data_table = dash_table.DataTable(id='our-data-table', page_size=10) # App Layout app.layout = dbc.Container( [ dbc.Row(dbc.Col(markdown)), dbc.Row( [ dbc.Col(dropdown, width=3), dbc.Col(radio, width=3), dbc.Col(button, width=3) ] ), dbc.Row(dbc.Col(dcc.Graph(id='our-figure'))), dbc.Row(dbc.Col(data_table)) ] ) # Configure callbacks @app.callback( Output(component_id='our-figure', component_property='figure'), Output(component_id='our-data-table', component_property='data'), Input(component_id='our-button', component_property='n_clicks'), State(component_id='our-dropdown', component_property='value'), State(component_id='our-radio', component_property='value'), ) def update_graph(n_clicks, value_dropdown, value_radio): if n_clicks >= 0: df_sub = df[df['country'].isin([value_dropdown])] data = df_sub.to_dict('records') if value_radio == 'scatter': fig = px.scatter( df_sub, x='year', y='lifeExp', color='country', symbol='continent', title='PX {} plot'.format(value_radio), template='plotly_white' ) else: fig = px.line( df_sub, x='year', y='lifeExp', color='country', symbol='continent', title='PX {} plot'.format(value_radio), template='plotly_white' ) return fig, data # Run the App if __name__ == '__main__': app.run_server()