...

/

Modify the App by Parsing URLs and Using Their Components

Modify the App by Parsing URLs and Using Their Components

Get familiar with altering the components and parameters of a Dash application to achieve the desired functionality and layout.

Having established some working knowledge about how Location and Link components work, we want to use this in our app. The plan is to do the following:

  • Add 169 new pages using three callbacks and adding a few new layout elements.
  • The user will have a drop-down menu of countries to choose from.
  • Selecting a country modifies the URL, which will render the country layout.

This layout will include a heading, a chart, and a table about the country in the URL. The illustration below shows a sample of a country page that we’ll build:

Press + to interact
 A sample country page showing a chart including other countries as well
A sample country page showing a chart including other countries as well

We now have a template for a country page. This was triggered because the URL contained a country that is one of the available countries in the dataset. Otherwise, it will display the main app, containing all the components that we have built so far. Users can also click on the “Home” link to go there.

First let’s see how to restructure our app.

Restructuring our app to cater to multiple layouts

At this stage, we haven’t moved from the basic structure that we discussed earlier. The table below shows a simplified representation of the current structure:

The Structure of a Dash App

App parts

app.py

imports (boilerplate)

import dash

import dash_html_components as html

import dash_core_components as dcc

app instantiation

app= dash.Dash(__name__)

app layout: a list of HTML and/or interactive components

app.layout= html.Div([

dcc.Dropdown()

dcc.Graph()

...

])

callback functions

@app.callback()

...

@app.callback()

...

running the app

if __name__ == `__main__`:

app.run_server()

Everything will remain the same with the exception of the layout part. Right now, we only have one layout attribute, and everything was added to the main div. We used tabs to efficiently utilize space in some cases, and ...