Dash is a framework from Plotly to program web apps for data analysis/visualization in Python, R, or Julia. Dash is based on React, a well-known Javascript web framework, and Flask, one of the most famous web servers in Python. Plotly also offers commercial solutions for hosting web apps, however as Dash is completely free and open-source you can install it on your own server that supports Python.
Install and Use Dash Plotly on Debian 11 Bullseye Server
Here in the tutorial, we will learn the process to install on Debian 11 server and how to create a Dash Application Project.
Requirements:
- A sudo user access
- Internet connectivity
- Python
- Debian 11 Server
1. Run system update
The first thing before installing any package we must do is the running of the system update command. This ensures the existing packages are up to date and also rebuilds the system repo cache.
sudo apt update
2. Install Python & Pip
Although a full-fledged server comes with Python 3 out of the box, however, the minimal server would not. Therefore to install and set up Dash, first install Python and Pip 3.
sudo apt install python3 python3-pip
3. Install Dash using Pip or Anaconda
Well, here we are using pip to set up Dash on Debian 11, however, if you are already a user of Anaconda then can use the Conda package manager to get this Plotly framework. Note: Learn steps to install Anaconda on Debian 10 or 11 Bullseye.
To get Dash HTML, core components including Plotly using PIP, run:
pip install dash
Whereas for front-end
pip install dash-renderer
Trivia: If you are using Anaconda then either get it from its GUI or use the command:
conda install dash
4. Create your first Dash project on Debian 11
Create a project folder, let’s say myproject
mkdir myproject
Switch to it:
cd myproject
Create a python file, let’s say – first_app.py
.
nano first_app.py
Let’s first create a simple HTML page using DASH to display some text we want, for example: “Here is my first Dash project”
In the file paste the below-given lines. Using which we are importing Dash’s HMTL components, directing to display the text in the H1 heading, and also at the end of the file we declare a code to start the server in the main routine.
import dash import dash_html_components as html app = dash.Dash ( __name__ ) app.layout = html.H1(children = "Here is my first Dash project!") if __name__ == "__main__" : app.run_server ( debug = True )
Save the file by press Ctrl + O, hit Enter key, and to exit use – Ctrl+X.
Run the above create Python app file:
Now, use Python to run the above-created file:
python3 first_app.py
Access Dash App
As you run your created first Dash app, you will see the URL to access it. By default, it will be 127.0.0.1 at 8050 port.
http://127.0.0.1:8050
5. To Run Dash on HTTP Port
As we know by default Dash server runs on 8050 port and if you want to run it on some custom one or let’s say on HTTP port 8080.
Then in the code, we used to start the server, define the port number. You can also define a specific Ip-address to use, in case needed.
if __name__ == '__main__': app.run_server(host='0.0.0.0', debug=True, port=80)
Demo Histogram Project
To go one step ahead to see something interactive on the Dash app, create a new app file and add the following code.
nano new_app.py
Paste the following code:
import dash import dash_core_components as dcc import dash_html_components as html import plotly.express as px import pandas as pd app = dash.Dash(__name__) # assume you have a "long-form" data frame # see https://plotly.com/python/px-arguments/ for more options df = pd.DataFrame({ "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"], "Amount": [4, 1, 2, 2, 4, 5], "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"] }) fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group") app.layout = html.Div(children=[ html.H1(children='Hello Dash'), html.Div(children=''' Dash: A web application framework for your data. '''), dcc.Graph( id='example-graph', figure=fig ) ]) if __name__ == '__main__': app.run_server(debug=True)
Save the file by press Ctrl + O, hit Enter key, and to exit use – Ctrl+X.
Run the created App file:
python3 new_app.py
Now, go to the browser and connect to your server. You will have the following output:
Error: python dash Oserror Errno 98 address already in use
If you have closed the current session of Dash Server and after that running any other app on terminal giving error – the address is already in use. Then simply run the below-given command, this will terminate all the current python active running services.
kill -9 $(ps -A | grep python | awk '{print $1}')