Introduction to Dash in Python
Last Updated :
10 May, 2020
Dash is a Python framework for building analytical web applications. Dash helps in building responsive web dashboards that is good to look at and is very fast without the need to understand complex front-end frameworks or languages such as HTML, CSS, JavaScript. Let's build our first web dashboard using Dash.
Installation and Imports
Install the latest version of Dash
pip install dash
Basic Dashboard
In this section, we will make an app that shows a static(but responsive) graph on the web page using the dash.
Step 1: Importing all the required libraries
Now let's import Dash, Dash Core Components(which has components like graph, inputs etc., ) and Dash HTML Components(which has HTML components like meta tags, body tags, paragraph tags etc., )
Python3 1==
# importing required libraries
import dash
import dash_core_components as dcc
import dash_html_components as html
Step 2: Designing a layout
HTML components are just like HTML. Here
html.H1 refers to the
h1 tag in HTML.
Then, we make a graph which has various parameters such as
id(a unique ID to a particular graph),
figure(the graph itself),
layout(the basic layout, title of graph, X axis, Y axis data etc., ).
- The figure parameter is essentially a dictionary which has elements like x, y, type, name.
- x refers to the X-axis value(it can be a list or a single element), y is the same except it is associated with the Y-axis.
- The type parameter refers to the type of the graph, it maybe line, bar.
- The name parameter refers to the name associated with the axis of a graph
Python3 1==
app = dash.Dash()
app.layout = html.Div(children =[
html.H1("Dash Tutorial"),
dcc.Graph(
id ="example",
figure ={
'data':[
{'x':[1, 2, 3, 4, 5],
'y':[5, 4, 7, 4, 8],
'type':'line',
'name':'Trucks'},
{'x':[1, 2, 3, 4, 5],
'y':[6, 3, 5, 3, 7],
'type':'bar',
'name':'Ships'}
],
'layout':{
'title':'Basic Dashboard'
}
}
)
])
Step 3: Running the server
The dashboard is now ready, but it needs a server to run on. Thus we set up the server using the below code.
Python3 1==
if __name__ == '__main__':
app.run_server()
Open the app on the web browser in localhost and default port 8050.
http://127.0.0.1:8050/
Output:
Screenshot of the Basic Dash app.
Using Callbacks
The above teaches us a basic static app. But what if you want to let the user take the control. This is why we are going to use app callbacks provided in Dash. In this section, we are going to make a web app that takes in number from the user and return the square of the number
Step 1: Importing all the required libraries
Just like above we are going to import all the required libraries. Here we require an additional
dash.dependencies.Input and
dash.dependencies.Output to provide us with input and output callback functionality.
Python3 1==
# importing required libraries
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
Step 2: Designing a layout
We are going to design a simple textbox that will help out take input and a text label which will output the square of the number that is input or returns an error if the input is not a number.
Python3 1==
app = dash.Dash()
app.layout = html.Div(children =[
dcc.Input(id ='input',
value ='Enter a number',
type ='text'),
html.Div(id ='output')
])
Step 3: Callbacks
A callback is like a trigger that does a certain function on the change of the state of the input. In this case, it executes the method
update_value and takes in
input_data as the parameter and returns the square of that number. If the input is not a number, then it returns an error statement.
Python3 1==
@app.callback(
Output(component_id ='output', component_property ='children'),
[Input(component_id ='input', component_property ='value')]
)
def update_value(input_data):
try:
return str(float(input_data)**2)
except:
return "Error, the input is not a number"
Step 3: Running the server
Again, just like above, we are going to run the server.
Python3 1==
if __name__ == '__main__':
app.run_server()
Open the app on the web browser in local host and default port 8050.
http://127.0.0.1:8050/
Output:
Square of five using Python Dash callbacks.
Footnotes
The above two examples must be useful for you to understand the working of the Dash framework. Although the two examples written above might not be useful on itself, it does help in understanding the concepts of building web apps with Dash framework and that will help you in building a useful web app in the future using real data.
Similar Reads
Plotly tutorial Plotly library in Python is an open-source library that can be used for data visualization and understanding data simply and easily. Plotly supports various types of plots like line charts, scatter plots, histograms, box plots, etc. So you all must be wondering why Plotly is over other visualization
15+ min read
Introduction to Plotly-online using Python The plotly library is an interactive open-source library. This can be a very helpful tool for data visualization and understanding the data simply and easily. Plotly graph objects are a high-level interface to plotly which are easy to use. It can plot various types of graphs and charts like scatter
2 min read
Getting Started with Plotly The Plotly Python library is an interactive open-source graphing library. It is a very helpful tool for data visualization and understanding the data simply and easily. Plotly graph objects are a high-level interface to plotly which are easy to use. It can plot various types of graphs and charts lik
10 min read
Plotly for Data Visualization in Python Plotly is an open-source Python library designed to create interactive, visually appealing charts and graphs. It helps users to explore data through features like zooming, additional details and clicking for deeper insights. It handles the interactivity with JavaScript behind the scenes so that we c
12 min read
Creating a plot
Line Chart using Plotly in PythonPlotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
4 min read
Bar chart using Plotly in PythonPlotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar
4 min read
Histogram using Plotly in PythonPlotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
3 min read
Scatter plot using Plotly in PythonPlotly Python is a library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. Plotly python is an interactive visualization
5 min read
Bubble chart using Plotly in PythonPlotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization librar
3 min read
Pie plot using Plotly in PythonPlotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
3 min read
Box Plot using Plotly in PythonPlotly is a Python library which is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
8 min read
Gantt Chart in plotlyPlotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
Contour Plots using Plotly in PythonA Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
4 min read
Create Heatmaps using graph_objects class in PlotlyA Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
Sunburst Plot using Plotly in PythonPlotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
4 min read
Polar Charts using Plotly in PythonA Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
Ternary Plots in PlotlyA Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization libra
2 min read
3D Line Plots using Plotly in PythonPlotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
3D Surface Plots using Plotly in PythonPlotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
3D Bubble chart using Plotly in PythonPlotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
3D Mesh Plots using Plotly in PythonPlotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
4 min read
Advanced Plot Customization
Plotly Dash Basics
Python Plotly - Exporting to Static Images In this article, we will discuss how to export plotly graphs as static images using Python. To get the job done there are certain additional installations that need to be done. Apart from plotly, orca and psutil have to be installed. psutil (python system and process utilities) is a cross-platform P
2 min read