Simple Dashboards Just with Jupyter | by Michael Keith | Towards Data Science

There is such a thing as the right tool for the job. When you or your organization have access to expensive visualization software, such as Tableau, Power BI, or Domo, every problem looks like a nail and your program is the hammer. If you are a data analyst for such an organization, you will inevitably spend countless hours on one of these dashboards and imbed every conceivable answer to any question a senior decision-maker of the company could have, just to be greeted with an email a week later containing some request that makes it obvious that no one is even using the dashboard’s most basic functionality. I cannot pretend to know why some people (especially upper management) don’t enjoy the beauty of an interactive Tableau application with the bar charts, leaflets, and line graphs all synchronized together in perfect harmony, but it might be that people who don’t deal with data all day don’t naturally know how to navigate such a thing and don’t want to spend any valuable time trying to figure it out.
Instead of dealing with all that hassle, consider using Jupyter Lab, together with basic Python and markdown, to create simple visualizations, exported to an HTML or PDF document that get straight to the point, require basically no cost to maintain, and can be cobbled together in a couple of hours. I’m not contesting that such a thing is appropriate for all situations, but it can be an arrow in the quiver for when whoever is needing the dashboard would be fine with a quick overview of the data rather than a perfect mosaic of visual aids. I’ve found that many higher-ups can more readily access something so simple and appreciate how few resources have to be dedicated to setting up and maintaining it.
github.com
To get started, I suggest using Anaconda to run Jupyter Lab. There are many resources that describe how to do this — I particularly like the guide from pandas. I personally use the 64-Bit Graphical Installer Anaconda distribution (click here to download) for Windows to run most of my Python applications, but understand there are other ways to go about this. Use whatever makes most sense for your situation.
After Anaconda is installed, you should be able to run Jupyter Lab by opening the Anaconda Navigator and selecting Launch in the JupyterLab application box.
You can also start it by typing “jupyter lab” in Anaconda Prompt.
Next, you will need to install Nbconvert. I recommend reading the entire documentation in case you run into trouble with my summary. To install it, use this command in Anaconda Prompt:
conda install nbconvert
To export your final product to an HTML document, you need to install Pandoc. For Windows, the installer is recommended, but there are other installation methods. You can find all of that information here.
I prefer exporting results to HTML only — it is fast and the end product looks great. But there are other export options you can explore, including PDF and Latex. The installation for these tools is more complicated. Again, all of that can be found in the Nbconvert documentation.
Finally, let’s import data. I would imagine this means setting up a SQL connection to a data warehouse for most users (it’s what it usually means for me), but this could also mean importing the data from a shared drive, API, etc. The important part here is that the import be completed without warnings in the code. Warnings won’t stop your code from working, but they will export to the final HTML document unless you clear them from your notebook manually. This is true for every coding block you run.
For this example, I will be using COVID-19 vaccination data available through Our World in Data on GitHub.
Here is the list of needed imports:
import datetimeimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport matplotlib.ticker as tickerimport seaborn as snsfrom IPython.core.display import display, HTMLpd.options.display.max_rows = None # to print tablespd.options.display.max_columns = None # to print tablesdata_state = pd.read_csv('us_state_vaccinations.csv')data_manufac = pd.read_csv('vaccinations-by-manufacturer.csv')
Now that we have Jupyter Lab installed with all of the necessary add-ons and we’ve read in the data, we can begin making data visualizations.
For this example, I will build three graphics and include two tables in the output. I will also use Markdown on Jupyter for easy navigation between sections.
For this graphic, I use a line chart with three statistics represented: total vaccines distributed, people who have received one does, and people who are fully vaccinated over time from 12/15/2020 to present (4/15/2021). This is a very simple visualization with the only potentially challenging part being placing the last data point at the end of each line to give the total figures for the respective groups using the matplotlib text() function. The Python code is given below.
First, to prepare the data:
Then, to build the chart:
The output looks like this:
Next, a chart to represent the performance by U.S. state/territory. The first thought for many to represent these statistics would be a leaflet chart, which is basically a regional map of the area you are charting with different areas shaded to represent different densities. However, those can be difficult to produce in this scenario and also are not as straightforward to obtain the pertinent comparisons as a simple heat map. Let’s see how the heat map looks.
We can use the data prepared for the previous chart for this one. The code to create the visual aid:
And the output:
We can also add in a table with this information, as well as total vaccination numbers for each of these states. The code for that is included in the notebook linked in GitHub above.
Finally, let’s breakdown the data by manufacturer. For this chart, we can try something more advanced: a stacked bar chart overlaid with a shaded line representing the 7-day rolling average. It can be tricky to get the dates to align between the bar graph and line plot as the underlying matplotlib functions default to different date representations for the two charts.
The code to prepare the data is given below:
The code to make the visualization:
The output:
One thing that surprised me was to see how few Johnson & Johnson vaccines are being administered. Considering that is a one-dose vaccine, I expected it to be more popular. Even with simple visualizations, interesting insights from data can be revealed.
Now that we have our three visualizations, we can export the notebook to an HTML document. There is a way to theoretically make these charts all interactive by using the `%matplotlib widget` command in the code, but I haven’t been able to get that to work on my system. Any tips in the comments would be welcome. Regardless, the export command involves using Anaconda Prompt to run two commands:
cd local/path/to/jupyter/notebookjupyter nbconvert --to html --no-input dashboard.ipynb
This will place an HTML document in the same local directory as your notebook. For the complete code used in this example, see here.
To refresh the analysis with new data, you will just have to make sure you’ve established a live feed to the data (in this example, I used flat files that would have to be redownloaded). It is then as easy as selecting ‘kernel →Restart Kernel and Run All Cells’ in your browser with the Jupyter Notebook open. Confirm that you want to restart the kernel, then rerun the commands given in the prior section on Anaconda Prompt, and you will have fresh output that you can email to whoever needs it. This process is much simpler than building state-of-the-art dashboards and can be an effective solution when you need something simple and you need it quickly.