How To Create A Dashboard in Python With Jupyter Notebook
How To Create A Dashboard in Python With Jupyter Notebook
The demo of the financial dashboard created in Python with Jupyter Notebook and published
with Mercury :
I choose djenv as a name for virtual environment. I keep all required packages in
the requirements.txt file:
mljar-mercury
mplfinance
yfinance
# add kernel
python -m ipykernel install --user --name djenv
We have environment ready, the next step is to start a Jupyter Notebook and create a new
notebook. Please remember to select kernel with djenv name when creating a new notebook:
import yfinance as yf
import mplfinance as mpf
The stock data will be displayed for selected ticker. Let’s create a variable for that:
ticker = "TSLA"
The market data for selected ticker can be fetched with 1 line of code, thanks to
amazing yfinance package:
history = yf.Ticker(ticker).history(period="3mo")
history.tail()
The history variable is a Pandas DataFrame. The financial data can be easily plotted
with mplfinance package:
The whole notebook is preseted below, it has only 9 lines of Python code:
Share dashboard with non-technical users
OK, the notebooks is ready. You can change the ticker variable (for example set to "MSFT" )
and execute the notebook to get new data. However, it is not convenient to share the notebook
with code, it is sometime impossible, if your users are non-technical and can’t install Python and
required packages. Please don’t worry! 😊 There is an open-source framework
called Mercury for converting Python notebooks into interactive web applications. You can hide
the code in the notebook and show only outputs (table and chart). The web app can be deployed
to the cloud. You will share the link to the web application. The ticker variable can be
converted into interactive widget. Sounds interesting?
What is more, you don’t need to change any code in your notebook. Just add the YAML header
in the first raw cell:
---
title: Finance dashboard
description: Dashboard with financial data
show-code: False
params:
ticker:
input: select
value: TSLA
label: Select ticker
choices: [TSLA, COIN, MSFT, PLTR, SNOW, NFLX]
---
The YAML will tell the Mercury framework how to display the notebook. It defines
the title and app’s description . The code is hidden with show-code paramter.
The ticker variable is defined as a select widget. The whole notebook is presented below:
OK, but how to run the web app? It is very simple, just run the command:
mercury run
That’s all! The Mercury will detect the notebooks in the directory and will serve them as web
applications.
We are ready for deployment. The first step is to create the app in the Heroku. I’m using Heroku
CLI tool for app creation:
Really?! Yes, that’s all. Just wait a minute and your server will be running 😊 My dashboard is
running at dashboard-jupyter-notebook.herokuapp.com/. The demo of the dashboard is below:
If you update the notebook and would like to deploy a newer version of the notebook, please just
use the same command:
You need to add a Heroku buildpack to make it work. It can be done in the Heroku dashboard:
https://github.com/jontewks/puppeteer-heroku-buildpack
After adding a buildpack with puppeteer you need to re-deploy the app. Please add do
changes in the code or add welcome.md file ( welcome.md message docs) and deploy with:
If you have any questions or need help please subscribe to the newsletter below. In the form, you
can leave feedback, comment, or question. We do our best to respond quickly :