Build a dashboard in python from a CSV file [ Easy Steps ]

Python Dash Board From Csv

Hello readers! In this tutorial, we’ll be looking at how we can quickly build a dashboard in Python using dash, from a CSV file.

Dash is a Python framework that makes it easy for anyone to build dashboards in Python, while not having to deal with the frontend required directly.

Steps to build a dashboard in Python

Let’s now get started and build a dashboard in Python using the dash library to display data from a CSV File!

Step 1: Plot the data using Plotly

We’ll be using a simple CSV file for the data source, namely a COVID time series dataset.

I’m using this COVID-19 dataset from Kaggle. Once you have it ready, we can start using it.

To render the plots, we’ll be using the Python plotly library. To install this library, use:

pip install plotly

Let’s now plot the time series data for various states. We’ll use the Pandas read_csv() function to read the data from our CSV dataset. It’s just 3 simple lines of code!

import plotly.express as px

df = pd.read_csv('covid_19_india.csv')

# Plot the scatterplot using Plotly. We ploy y vs x (#Confirmed vs Date)
fig = px.scatter(df, x='Date', y='Confirmed', color='State/UnionTerritory')
fig.update_traces(mode='markers+lines')
fig.show()

Now plotly should give you a nice visualization of the data. Let’s now render this in our Dash application.

Step 2: Embed the graph with Dash

To render our dashboard application, we’ll be using Dash. Install this library using:

pip install dash

We’ll use dash to render the data in a layout.

Before that, let’s set up some stylesheets (CSS) for our page to look good! I’m using the default data from this dash official tutorial.

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

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

colors = {
    'background': '#F0F8FF',
    'text': '#00008B'
}

Let’s now configure our data in this layout.

# Our dataframe
df = pd.read_csv('covid_19_india.csv')

fig = px.scatter(df, x='Date', y='Confirmed', color='State/UnionTerritory')
fig.update_traces(mode='markers+lines')

app.layout = html.Div(children=[
    html.H1(children='COVID-19 Time Series Dashboard'),

    html.Div(children='''
        COVID-19 Dashboard: India.
    '''),

    dcc.Graph(
        id='example-graph',
        figure=fig
    )
])

Step 3: Run the application server with Flask

Now, let’s finally run the application server (via Flask):

if __name__ == '__main__':
    app.run_server(debug=True)

This will start the server on local port 8050. Let’s look at the output now, when we go to http://localhost:8050

Dashboard Output
Dashboard Output

As you can see, we indeed have a nice looking interactive dashboard in just a few lines of Python code!


Conclusion

In this tutorial, we learned how we could build a dashboard in Python from a CSV file using Dash.