Open In App

Creating Interactive Dashboard from Jupyter Notebooks

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Creating interactive dashboards is a required and powerful way to communicate insights in the field of Data Analysis and Data Visualization. In this article, we are going to dive into the process of creating an interactive dynamic dashboard using Python, Jupyter Notebook, and Dash framework complete with interactivity through user input.

Prerequisites

Before we dig into actually making a dashboard in Jupyter Notebook, make sure you have the required libraries installed. You can use the following command to install the libraries.

pip install dash jupyter-dash
pip install yfinance

This will install the Dash and Jupyter Dash and also the yfinance package.

Creating Dashboard from Jupyter Notebook

Step 1: Import the Necessary Libraries

Here, dash imports the Dash library which is necessary for building interactive dashboards.

Python3




from jupyter_dash import JupyterDash
from dash import dcc, html, Input, Output
import yfinance as yf 
import plotly.express as px


Here is the explanation of the code:

  • dcc, html import the specific components from Dash which include graphs, dropdowns, etc. from dcc and html imports html components for structuring the layout of the web page.
  • JupyterDash is a class provided by the ‘jupyter_dash’ library which is an extension of dash which helps to seamlessly work within Jupyter Notebook.
  • Input, Output are the classes from the dash.dependencies module which used to specify the inputs and outputs for callback functions in Dash.
  • yfinance is a library used to fetch stock data.
  • plotly.express is a data visualization library which provides a simple and concise syntax for creating a variety of interactive plots.

Step 2: Initialization

Next, we will initialize our Dash application within the Jupyter Notebook,

Python3




app = JupyterDash(__name__)


Step 3 : Building the Layout

We will define a simple layout which will include heading, a label, dropdown menu for selecting a stock symbol and an empty graph area.

Python3




app.layout = html.Div([
    html.H1("Stock Price Analysis Dashboard"),
     
    html.Label("Select Stock Symbol:"),
    dcc.Dropdown(
        id='stock-selector',
        options=[
            {'label': 'AAPL', 'value': 'AAPL'},
            {'label': 'GOOGL', 'value': 'GOOGL'},
            {'label': 'MSFT', 'value': 'MSFT'},
        ],
        value='AAPL'
    ),
     
    dcc.Graph(id='stock-price-chart'),
])


This section uses HTML and Dash components to define a layout. This layout will work as the initial main appearance of the dashboard.

Step 4: Adding Interactivity with Callback

To make our dashboard interactive, we will create a callback which will then dynamically update the line chart based on the selected stock symbol.

Python3




@app.callback(
    Output('stock-price-chart', 'figure'),
    [Input('stock-selector', 'value')]
)
def update_chart(selected_stock):
    stock_data = yf.download(selected_stock, start='2022-01-01',
                             end='2023-01-01')
    fig = px.line(stock_data, x=stock_data.index, y='Close',
                  title=f'{selected_stock} Stock Price Analysis')
    return fig


When the user selects a different stock symbol, this callback function is triggered. It fetches the previous stock data for the selected stock and updates the content of the line chart with the new data, providing an interactive experience for analyzing stock prices.

Step 5: Running the Application

To run the above application, run the below line which will start the Dash server and display the Dashboard within the Jupyter Notebook and also in a separate web browser.

Python3




if __name__ == '__main__':
    app.run_server(port=8050, mode='external')


Output:

Creating Dashboard from Jupyter Notebooks

This will display the dashboard on Jupyter Notebook and will provide a link for the external web browser where the dashboard is displayed.

You will see an output like,

Dash app running on http://127.0.0.1:8050/

You can access the web browser dashboard by simply navigating to the provided link.

Video Output:

The dashboard on web browser will be like,

ezgif-4-d2232ac2cd

Deployment using Binder

Binder is a free and open platform that allows you to create interactive, shareable, and reproducible computational environments. You can deploy your Jupyter Notebook on Binder which can be accessible to anyone on internet.

Here’s how you can deploy the dashboard using binder:

Step 1: Create a file named ‘requirements.txt’ in the same directory as your Jupyter Notebook. In the ‘requirements.txt’ list the necessary libraries ; for this dashboard the ‘requirements.txt’ file will look like this:

jupyter-dash
dash
yfinance
plotly

Step 2: Create a file named ‘Procfile'(without any file extension) with the following content:

web: jupyter-dash your_notebook_name.ipynb

Here, ‘your_notebook_name’ will be replaced by your actual Jupyter Notebook file name.

Step 3: Commit both ‘requirements.txt’ and ‘Procfile’ along with the dashboard Jupyter Notebook file to Github repository.

Step 4: Now, go to Binder(https://mybinder.org/) and enter the URL of your Github repository in the “GitHub repository name or URL” section.

Next, click on the “Launch” button. This will generate a binder link that you can share with others.

Step 5: Share the generated Binder link with others, and they will be able to interact with your Jupyter Notebook in an online environment.

Now, anyone can access your Jupyter Notebook through the Binder link without having to download the dependencies locally. This makes your analysis and visualization easily reproducible in an interactive environment.

This is how a binder link looks like:

Screenshot-2023-11-19-235312

Conclusion

Dash enables the creation of powerful yet interactive dashboards directly within Jupyter Notebook. When you create a dashboard using Python in Jupyter Notebook, it shows a wonderful integration of Data Analysis with Data Visualization. By adding user inputs and dynamic components, you can make dashboards which are adept to changing data on the basis of user preferences. Start experimenting and discover the endless possibilities for your data storytelling!



Similar Reads

Creating Interactive Slideshows in Jupyter Notebooks
We all have been very well acquainted with the creation of slideshows by using Microsoft PowerPoint for our schools, colleges, or offices in our day-to-day lives. But, have we ever wondered how would it be to create a slideshow through the Jupyter Notebook? The advantages of creating slideshows with Python and Jupyter are its version control capabi
11 min read
Interactive Controls in Jupyter Notebooks
This article explains the significance of interactive controls in Jupyter Notebooks and presents a few different methods of adding them to the notebooks for Python programming language. A list of basic controls/widgets and finally examples are provided to demonstrate all that is presented throughout this article. This article expects basic familiar
12 min read
Interactive Dashboard from Jupyter with Voila
Jupyter Notebook is a powerful tool for data visualization, exploration, and analysis. However, when you are going to share your findings in a more interactive and user-friendly way, changing your Jupyter Notebook into an interactive dashboard is a valuable privilege. Voila is a Jupyter extension that provides an interactive Dashboard. In this arti
6 min read
How to Run Jupyter Notebooks from a Remote Server?
Jupyter Notebook is an open-source, interactive web application that allows you to write and run computer code in over 40 programming languages, including Python, R, Julia, and Scala. In this article, we will see how we can Jupyter Notebook with a configured server instead of localhost. Run Jupyter Notebooks from a Remote ServerBelow, is the step-b
2 min read
Interactive Graphs in Jupyter Notebook
When working in a Jupyter Notebook environment, you can produce interactive Matplotlib plots that allow you to explore data and interact with the charts dynamically. In this article, we'll explore how to create such interactive plots using Matplotlib within Jupyter. Before we proceed with the steps, let's understand some key concepts related to pro
3 min read
How to convert IPython notebooks to PDF and HTML?
In this article, we will see how to convert IPython notebooks to PDF and HTML. Sometimes according to our necessity, we would want to share or convert the Jupyter notebook to HTML or pdf format. Our Jupyter Notebooks can be easily converted to PDF and HTML files. You may access your notebook as a PDF with only a few simple steps. Stepwise Implement
1 min read
Creating Interactive Form in Figma
Creating an interactive form in Figma has become an essential skill for UX/UI designers aiming to craft engaging and efficient digital experiences. Figma is a collaborative design tool that helps in UX/UI designing. This tool helps to build creative UI designs. You can use this tool to create interactive forms before actually implementing them in t
6 min read
Create a Dashboard App using React-Native
A dashboard app using react native is a software application designed to provide a consolidated and visual representation of important information, data, or features in a single, easily accessible interface. Dashboards are commonly used in various industries to help users monitor, analyze, and manage data efficiently. Preview of final output: Let u
6 min read
What is a Dashboard in Data Analytics ?
Understanding data is the key to making the best decisions for any organization in today's world. However, even the most data-savvy individual might get overwhelmed by the amount of information available at any one time. Creating a dashboard that conveniently shows all of your data visualizations in one location is one of the simplest methods to ma
6 min read
What is a Data Visualization Dashboard?
Businesses and organizations are continuously looking for ways to make sense of the enormous volumes of data they generate and gather in this era of information overload. Organizations gain crucial insights and patterns that might otherwise go unnoticed by converting data into visual representations like charts, graphs, and maps it is where data vi
22 min read