Dash Plotly in Python

Getting Started with Dash Plotly in Python: A Beginner’s Guide

Build analytical web applications without requiring advanced web development knowledge.

Shweta Bochare
3 min readSep 27, 2023

--

Data Visualization with Python Dash

Introduction

Dash Plotly is a powerful tool for creating interactive data dashboards in Python. Dash by Plotly is a Python framework that allows you to create interactive, web-based data dashboards with ease. Whether you’re a data scientist, analyst, or just someone interested in exploring and presenting data in an interactive way, Dash can be a powerful tool in your toolkit.

In this beginner’s guide, we’ll walk you through the basics of getting started with Dash Plotly in Python, from installation to creating your first interactive dashboard.

Prerequisites

Before diving into Dash, make sure you have the following prerequisites installed:

  1. Python: You’ll need Python installed on your computer. You can download and install Python from the official website (https://www.python.org/downloads/).
  2. PIP: PIP is Python’s package manager, and it usually comes bundled with Python. To check if you have PIP installed, open your terminal or command prompt and run:
pip --version

If PIP is not installed, you can install it following the instructions on the PIP website (https://pip.pypa.io/en/stable/installation/).

3. Virtual Environment: While not strictly necessary, it’s a good practice to create a virtual environment to manage your project’s dependencies. You can create a virtual environment using the following command:

python -m venv myenv

Activate your virtual environment:

  • On Windows:
myenv\Scripts\activate
  • On macOS and Linux:
source myenv/bin/activate

Now that you have the prerequisites in place, let’s dive into creating your first Dash Plotly application.

Installing Dash Plotly

To get started with Dash, you need to install the Dash library. Open your terminal or command prompt and run the following command:

pip install dash

Dash is built on top of Plotly, which is another powerful library for creating interactive visualizations. You can also install Plotly to enhance your Dash applications:

pip install plotly

With Dash and Plotly installed, you’re ready to start creating your first interactive dashboard.

Creating Your First Dash Application

Let’s create a simple Dash application that displays a basic scatter plot. You can follow along by creating a new Python script or using a Jupyter Notebook.

# Import necessary libraries
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd

# Create a Dash application
app = dash.Dash(__name__)
# Sample data
data = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [10, 11, 12, 13, 14]
})
# Define the layout of your app
app.layout = html.Div([
html.H1("My First Dash Application"),

dcc.Graph(
id='scatter-plot',
figure={
'data': [
{'x': data['x'], 'y': data['y'], 'type': 'scatter', 'mode': 'markers', 'name': 'Scatter Plot'},
],
'layout': {
'title': 'Scatter Plot Example'
}
}
)
])
# Run the Dash app
if __name__ == '__main__':
app.run_server(debug=True)

In this code:

  • We import the necessary libraries, including Dash, Plotly, and Pandas.
  • We create a Dash application using dash.Dash().
  • We define some sample data for our scatter plot.
  • We create the layout of our application using HTML and Dash Core Components (DCC). In this case, we have a title and a scatter plot.
  • Finally, we run the Dash app using app.run_server(). Set debug=True for development purposes.

Running Your Dash Application

Save the Python script and run it from your terminal or command prompt:

python first_app.py

You should see an output indicating that your Dash application is running. Open a web browser and navigate to http://localhost:8050 to see your scatter plot dashboard.

Congratulations! 🎉 You’ve created your first Dash Plotly application.

From here, you can explore more advanced features of Dash and Plotly to create sophisticated, interactive data dashboards for your projects.

Conclusion

Dash offers a wide range of customization and interactivity options to help you tell compelling data stories.

In this beginner’s guide, you’ve learned how to install Dash, create a simple Dash application, and run it locally. This is just the beginning.

As you continue your journey with Dash, explore the official documentation here and dive deeper into its capabilities.

Happy dashboarding! 🙂

--

--

Shweta Bochare

Frontend Developer with a Passion for Crafting Responsive Websites using React, Node, HTML, CSS, SASS, and More! | Senior Software Engineer @ Peak.ai