...

/

Incorporating the Function into the App

Incorporating the Function into the App

Learn how to add a stand-alone function to a Dash application.

Final goal

Here’s the plan for the functionality that we are going to introduce:

  1. Create a drop-down list using the countries and regions available in our dataset.
  2. Create a callback function that takes the selected country, filters the dataset, and finds the population of that country in the year 2010.
  3. Return a small report about the found data. The illustration below shows the desired end result:
Press + to interact
 A drop-down list used to display the selected country's population
A drop-down list used to display the selected country's population

The illustration below shows what this folder structure might look like:

Press + to interact
The assumed folder structure for the app
The assumed folder structure for the app

We’ll run a minimal app in JupyterLab, make sure it is running properly, keep a copy, and then add it to the app.

We first need to take a look at the dataset, explore it a little bit, and learn how to implement the new functionality.

Visualizing our DataFrame

To view what files we have in the dataset, we can run the following code:

Press + to interact
import os
print(os.listdir('../data'))

For now, we’ll be working with the PovStatsData.csv file. For a quick overview of its structure, we can run the following:

Press + to interact
import pandas as pd
poverty_data = pd.read_csv('../data/PovStatsData.csv')
print(poverty_data.head(3))

It seems that we have two fixed variable columns (“Country Name” and “Indicator Name”). Measured variables in the form of numeric data (or missing NaN) values are available under their respective year column. Here, the years span from 1974 to 2019 (note that not all of the years are shown for better readability). The countries and indicators also have codes, which can be useful later when we want to merge different DataFrames.

Note: ...