How to Create a Simple Dashboard With Plotly

Eduardo Sarmento
Nov 24, 2020 · 5 min read
Image for post
Image for post
Photo by Negative Space from Pexels

Introduction

In order to have a better visualization of your data, you may want to gather everything in one place, creating a dashboard. Plotly Express is a tool that will let you to create awesome interactive graphs easily and, with Plotly Dash, you can use them to compose a dashboard.

In this article, I’ll show you how to create two graphs and put them together in a simple dashboard. We’ll be using COVID-19 data around the world as an example.

Method

The first graph we’ll create is a scatter plot on a world map showing the evolution of cases through the weeks of 2020. The data on COVID-19 worldwide is from Our World in Data COVID-19 dataset. We’ll also need coordinates of each country, which I got from this dataset on Kaggle.

Now that we imported both datasets, let’s merge them. After merging, I also converted the date column to datetime format and filled null values with 0.

The last preparation we need to do with our data is to organize them by week. I achieved this by selecting only the rows with data from Sundays and then created a new column with the corresponding week number using the isocalendar method.

Now, we can create our first graph using the Plotly’s scatter_geo function.

The result is a world map with points with size proportional to the total amount of cases in each country. The color also changes according to that number and you can see historical data using the slide at the bottom. If you hover over a point, it shows some extra infos.

Image for post
Image for post

The next graph we’ll create is a line plot showing the evolution of cases in the current 10 countries with highest amount of confirmed cases. For this, we need to know what countries are these. Therefore, we’ll select the rows of the last week, sort them by total_cases and extract only the first 10 as a list.

Now, we can create the graph comparing COVID-19 evolution in these countries.

The result is a simple line plot, on which you can select lines to hide and zoom in sengments of the graph.

Image for post
Image for post

The last thing we’ll do is creating a dashboard web page with these two graphs using Plotly Dash and Ngrok. Plotly Dash is a framework for building machine learning anddata science web apps. Ngrok is a tool that gives us a public URL for our localhost (you’ll need this if you’re running your code using Google Colab like me).

First, we’ll need to install Plotly Dash on our Colab machine.

Now, we can import everything we’ll need.

You can define the following function to download Ngrok.

We’ll also use a function to set up Ngrok’s tunneling.

The last step is to create a script that will create and serve the dashboard. We’ll use %%writefile on top of the code so it’ll be written on disk.

Now, run the function to download Ngrok if you haven’t done it yet and run the server with the following commands.

You should see something like this.

Image for post
Image for post

Click on the first link to open your dashboard and there it is!

Image for post
Image for post
Final result

Notice that the link will work only as long as you keep the server running. You can use other hosting services for a permanent link, like Heroku.

References

Coronavirus Source Data
https://ourworldindata.org/coronavirus-source-data

Countries Geographic Coordinates
https://www.kaggle.com/eidanch/counties-geographic-coordinates

IPython Notebook
https://github.com/edusrmt/one-figure-projects

Plotly Express
https://plotly.com/python/plotly-express/

The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store