# Import packages from dash import Dash, dcc, Input, Output, html import dash_bootstrap_components as dbc import pandas as pd # Import data url = 'https://raw.githubusercontent.com/open-resources/dash_curriculum/main/tutorial/part2/ch6_files/data_03.txt' df3 = pd.read_csv(url, sep=';') y=2007 df3 = df3.loc[(df3['year']==y), :] # Initialise the App app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) # Create app components _header = html.H1(children = 'Population by country in 2007', style = {'textAlign' : 'center'}) continent_dropdown = dcc.Dropdown(id = 'continent-dropdown', placeholder = 'Select a continent', options = [c for c in df3.continent.unique()]) country_dropdown = dcc.Dropdown(id = 'country-dropdown', placeholder = 'Select a country') _output = html.Div(id = 'final-output') # App Layout app.layout = dbc.Container( [ dbc.Row([dbc.Col([_header], width=8)]), dbc.Row([dbc.Col([continent_dropdown], width=8)]), dbc.Row([dbc.Col([country_dropdown], width=6)]), dbc.Row([dbc.Col([_output], width=6)]) ] ) # Configure callbacks @app.callback( Output(component_id='country-dropdown', component_property='options'), Input(component_id='continent-dropdown', component_property='value') ) def country_list(continent_selection): country_options = [c for c in df3.loc[df3['continent']==continent_selection, 'country'].unique()] return country_options @app.callback( Output(component_id='final-output', component_property='children'), Input(component_id='country-dropdown', component_property='value'), prevent_initial_call=True ) def pop_calculator(country_selection): pop_value = df3.loc[df3['country']==country_selection] pop_value = pop_value.loc[:, 'pop'].values[0] # select only first value in pop column output = ('The population in '+country_selection+' was: '+pop_value.astype(str)) return output # Run the App if __name__ == '__main__': app.run_server()