how to make a dashboard using all the charts figure ,kpis created in jupyter notebook

Uday shadani
7 min readDec 16, 2022

--

To create a dashboard using the charts and figures created in a Jupyter Notebook, you can use a library such as voila. Voila is a library that allows you to turn a Jupyter Notebook into a standalone web application. Here's an example of how you can use voila to create a dashboard:

  1. Install voila by running pip install voila in your terminal.
  2. In your Jupyter Notebook, create the charts and figures that you want to include in your dashboard.
  3. Save the notebook and close it.
  4. In your terminal, navigate to the directory where the notebook is saved and run the voila command, followed by the name of the notebook file: voila my_notebook.ipynb
  5. This will launch a web server and open a new tab in your web browser with the dashboard.

Voila will automatically convert the cells in your notebook into interactive widgets and display them in the dashboard. You can rearrange the widgets on the dashboard by dragging and dropping them.

Keep in mind that voila is designed for simple dashboards and may not be suitable for more complex dashboards with a large number of widgets. For more advanced dashboard development, you can consider using a tool such as Dash or Panel.

For more advanced dashboard development, you can consider using a library such as Dash or Panel.

These libraries allow you to create interactive dashboards with a wide range of features and customization options.

Here’s an example of how you can use the Dash library to create a dashboard from a Jupyter Notebook:

  1. Install the Dash library by running pip install dash in your terminal.
  2. Import the necessary libraries and components in your Jupyter Notebook:
import dash
import dash_core_components as dcc
import dash_html_components as html

3. Create the layout of your dashboard using the html and dcc components:

app = dash.Dash()

app.layout = html.Div([
html.H1("My Dashboard"),
dcc.Graph(id="graph1", figure={
"data": [{"x": [1, 2, 3], "y": [4, 1, 2]}],
"layout": {"title": "Graph 1"}
}),
dcc.Graph(id="graph2", figure={
"data": [{"x": [1, 2, 3], "y": [2, 4, 5]}],
"layout": {"title": "Graph 2"}
})
])

4. Run the dashboard by calling the run_server method:

app.run_server()

This will launch a web server and open a new tab in your web browser with the dashboard. You can customize the layout and appearance of the dashboard by modifying the html and dcc components in the layout.

You can also use the Panel library to create a dashboard from a Jupyter Notebook. Panel is built on top of the HoloViews library and allows you to create interactive dashboards using a declarative syntax. You can learn more about Panel and how to use it in the documentation: https://panel.holoviz.org/

Here’s an example of how you can use the Dash library to create a dashboard with a custom layout and HTML elements:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px

# Load the data
df = px.data.gapminder()

# Create the Dash app
app = dash.Dash()

# Define the layout
app.layout = html.Div([
html.H1("My Dashboard"),
html.Div(id="output"),
dcc.Dropdown(
id="year-dropdown",
options=[{"label": year, "value": year} for year in df["year"].unique()],
value=2007,
style={"width": "300px"}
),
dcc.Graph(id="graph1")
])

# Define the callback function
@app.callback(
dash.dependencies.Output("output", "children"),
[dash.dependencies.Input("year-dropdown", "value")]
)
def update_output(year):
return f"Selected year: {year}"

@app.callback(
dash.dependencies.Output("graph1", "figure"),
[dash.dependencies.Input("year-dropdown", "value")]
)
def update_graph(year):
# Filter the data for the selected year
df_year = df[df["year"] == year]

# Create the figure
figure = px.scatter(df_year, x="gdpPercap", y="lifeExp", size="pop", color="continent", hover_name="country", log_x=True)

return figure

# Run the app
app.run_server()

This code creates a dashboard with a dropdown menu and a graph. The dropdown menu allows the user to select a year, and the graph shows the GDP per capita and life expectancy for each country in the selected year. The layout of the dashboard consists of an HTML heading, a div element with the output ID (which will be used to display the selected year), the dropdown menu, and the graph.

The callback functions are used to update the output element and the graph based on the value selected in the dropdown menu. The update_output function updates the output element with the selected year, and the update_graph function filters the data for the selected year and creates a scatter plot using the plotly.express library.

To create advanced dashboard in jupyter notebook , we can use panel

To create an advanced dashboard using the Panel library from a Jupyter Notebook, you can use the following steps:

  1. Install the Panel library by running pip install panel in your terminal.
  2. Import the necessary libraries and components in your Jupyter Notebook:
import panel as pn
import holoviews as hv
hv.extension('bokeh')

3 .Create the layout of your dashboard using the pn and hv components:

# Create a Panel layout
layout = pn.Row(
pn.Column(
pn.Spacer(height=30),
pn.TextInput(name="Name"),
pn.TextInput(name="Age"),
pn.Button(name="Submit")
),
pn.Column(
pn.Spacer(height=30),
pn.DataTable(name="table", width=400)
)
)

4. Add interactivity to the dashboard by defining callback functions

# Define a callback function
def submit(event):
name = event.new.get("Name")
age = event.new.get("Age")
table = event.new.get("table")
table = table.append({"Name": name, "Age": age})
return {"table": table}

# Attach the callback function to the Submit button
layout.get("Submit").param.watch(submit, "clicks")

5. Display the dashboard using the show method:

layout.show()

This code creates a dashboard with a text input and a button on the left, and a data table on the right. The user can enter a name and age in the text inputs and click the Submit button to add a new row to the data table. The layout is created using the pn and hv components, and the interactivity is added using a callback function that is triggered by the clicks event of the Submit button.

Here’s an example of how you can use the Panel library to create a dashboard with a custom layout and interactive elements:

import panel as pn
import holoviews as hv
hv.extension('bokeh')

# Load the data
df = hv.Dataset(
{"x": [1, 2, 3, 4, 5], "y": [1, 4, 9, 16, 25], "c": ["a", "b", "b", "b", "c"]},
"x", ["y", "c"]
)

# Create a Panel layout
layout = pn.Row(
pn.Column(
pn.Spacer(height=30),
pn.Select(name="Color", options=["a", "b", "c"]),
pn.Button(name="Update")
),
pn.Column(
pn.Spacer(height=30),
hv.Scatter(df, "x", "y", color="c").opts(width=400, height=300)
)
)

# Define a callback function
def update(event):
color = event.new.get("Color")
scatter = event.new.get("scatter")
scatter.iloc[:, 1] = color
return {"scatter": scatter}

# Attach the callback function to the Update button
layout.get("Update").param.watch(update, "clicks")

# Display the dashboard
layout.show()

This code creates a dashboard with a select menu and a button on the left, and a scatter plot on the right. The select menu allows the user to select a color, and the button updates the color of the points in the scatter plot. The layout is created using the pn and hv components, and the interactivity is added using a callback function that is triggered by the clicks event of the Update button. The scatter plot is created using the hv.Scatter function and is displayed in the dashboard using the show method.

I hope this helps! Let me know if you have any questions.

here are several other libraries that you can use to create dashboards from a Jupyter Notebook. Some other popular options include:

  • Bokeh: Bokeh is a library for creating interactive visualizations in the web browser. You can use Bokeh to create a dashboard by defining the layout and interactivity using the Bokeh API, and then displaying the dashboard using the show function.
  • Plotly Dash: Plotly Dash is a library for creating interactive web applications with Python. You can use Plotly Dash to create a dashboard by defining the layout and interactivity using the Dash API, and then running the app using the run_server function.

. Jupyter Dashboards: The Jupyter Dashboards extension allows you to create interactive dashboards using a simple API based on the Jupyter widgets. You can use the extension to create a dashboard by creating a notebook with the widgets and layout you want to include in the dashboard, and then using the jupyter dashboards quick-setup command to create the dashboard.

Here are some examples of how you can use these libraries to create a dashboard from a Jupyter Notebook:

Bokeh:

from bokeh.io import show
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Slider

# Create a figure with a slider
source = ColumnDataSource(data=dict(x=[1, 2, 3], y=[1, 4, 9]))
plot = figure()
plot.line("x", "y", source=source)
slider = Slider(start=1, end=10, value=1, step=1)

# Define a callback function
def update(attrname, old, new):
x, y = source.data["x"], source.data["y"]
y = [i**new for i in x]
source.data = dict(x=x, y=y)

# Attach the callback function to the slider
slider.on_change("value", update)

# Create the layout and display the dashboard
layout = column(slider, plot)
show(layout)

This code creates a dashboard with a slider and a line plot. The slider allows the user to change the exponent of the line, and the callback function updates the plot accordingly. The layout is created using the column function, and the dashboard is displayed using the show function.

Here’s an example of how you can use the Plotly Dash library to create a dashboard from a Jupyter Notebook:

Plotly dash -

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px

# Load the data
df = px.data.gapminder()

# Create the Dash app
app = dash.Dash()

# Define the layout
app.layout = html.Div([
html.H1("My Dashboard"),
dcc.Dropdown(
id="year-dropdown",
options=[{"label": year, "value": year} for year in df["year"].unique()],
value=2007,
style={"width": "300px"}
),
dcc.Graph(id="graph1")
])

# Define the callback function
@app.callback(
dash.dependencies.Output("graph1", "figure"),
[dash.dependencies.Input("year-dropdown", "value")]
)
def update_graph(year):
# Filter the data for the selected year
df_year = df[df["year"] == year]

# Create the figure
figure = px.scatter(df_year, x="gdpPercap", y="lifeExp", size="pop", color="continent", hover_name="country", log_x=True)

return figure

# Run the app
app.run_server()

This code creates a dashboard with a dropdown menu and a graph. The dropdown menu allows the user to select a year, and the graph shows the GDP per capita and life expectancy for each country in the selected year. The layout of the dashboard consists of an HTML heading, the dropdown menu, and the graph.

The callback function is used to update the graph based on the value selected in the dropdown menu. The function filters the data for the selected year and creates a scatter plot using the plotly.express library.

Here’s an example of how you can use the Jupyter Dashboards extension to create a dashboard from a Jupyter Notebook:

# Install the extension
!jupyter nbextension install --py jupyter_dashboards
!jupyter nbextension enable --py jupyter_dashboards
!jupyter serverextension enable --py jupyter_dashboards

# Import the necessary libraries and components
from ipywidgets import interact
import holoviews as hv
import panel as pn

# Create a Panel layout
layout = pn.Row(
pn.Column(
pn.Spacer(height=30),
pn.Slider(name="X", value=1, start=1, end=10),
pn.Slider(name="Y", value=1, start=1, end=10)
),
pn.Column(
pn.Spacer(height=30),
hv.Curve([]).opts(width=400, height=300)
)
)

# Define a callback function
def update(X, Y):
curve = hv.Curve([(i, i**X + Y) for i in range(10)])
layout.get("Curve").update(curve)

# Attach the callback function to the sliders
interact(update, X=layout.get("X"), Y=layout.get("Y"))

# Create the dashboard
pn.extension("jupyter_dashboards")
layout.servable()

--

--

Uday shadani

Data > Opinion (On my way to become a data storyteller )