Uploading your dashboard to PythonAnywhere#
Structure your app directory#
Here is an example of how I structured my directory on my local computer for a zillow dashboard example.
zillow app folder
|-- zillow.py
|-- assets
| |-- favicon.ico
| |-- data
| | |-- zillow_data.csv
Structuring your app directories in a similar way will help you avoid referencing issues.
Create account#
Create a free account at https://www.pythonanywhere.com
With a free account, you can host one dashboard. Paid accounts are obviously available if you want to do more.
With a free account, you are limited to 100 seconds of CPU time.
With a free account, your web app will be located at your_username.pythonanywhere.com.
After creating an account and logging in, you will be in the PythonAnywhere dashboard.
Create virtual environment#
Create a new virtual environment.
We will use
mkvirtualenv
to create the environment. I will name this environment dashboards.mkvirtualenv dashboards --python=/usr/bin/python3.9
Once the environment has been created, it should be activated automatically, and you should see
(dashboards)
at the start of the line.If you come back to this later to make some changes, you can activate a particular environment to work on it using
workon dashboards
. You can then deactivate the environment by using thedeactivate
command.With the dashboard activated, we need to add the necessary packages.
pip install pandas plotly dash dash-bootstrap-components
If you are using other packages include those here as well.
If you ever want to see a list of your virtual environments, simply use the command
lsvirtualenv
.If you ever want to remove one of the environments, deactivate it and then
rmvirtualenv dashboards
.
This virtual environment can be used for multiple dashboards.
Now go back to the main dashboard.
Create the web app#
Click on Web in the top right and then Add a new web app.
Enter your app’s domain. With a free account, it will just be your_username.pythonanywhere.com
When prompted to select a python web framework, select Flask.
When prompted to select a python version, select the version you used to create your dashboard.
Specify the path for your files (e.g., /home/VanAlfen/zillow-app/zillow.py). You get to choose the directory and the file name, but do not change the
/home/username/
.Click Next and you should be taken to the settings page for your web app.
In the Code section of the page, click the WSGI configuration file.
At the end you will see something like:
from zillow import app as application
In this case,
zillow
is referring to our filezillow.py
. Change it to:from zillow import app application = app.server
Save these changes and go back to your web app settings.
In the Virtualenv section, enter the path to your virtual environment. It will probably look something like this:
/home/VanAlfen/.virtualenvs/dashboards
Though not required, I would recommend enabling Force HTTPS in the Security section.
In the Code section, go to the source code directory.
Create the necessary directories and upload external files such as your data. The folder structure should look like the structure at the beginning of this guide.
Now go back and click on your main code file —
zillow.py
— to edit it.Replace the contents of this file with your app’s code.
Make sure the following adjustments to your code have been made.
Remove the
debug=True
fromapp.run_server()
at the end.If you reference external files (e.g., data, favicon):
Make sure you’ve included
__name__
as the first argument in your app’s instantiation. For example:app = Dash( __name__, external_stylesheets=[dbc.themes.SANDSTONE] )
Update the paths to the external files. For example, the line of code specifying my path to the zillow data would be:
zillow_data_raw = pd.read_csv('./zillow-app/assets/data/zillow_data.csv')
Save the file and go back to your web app settings.
Now at the top, click to reload your app. Your app should now be visible at your url.
If there are errors, you can find them in the Error log on the settings page in the Log files section.
Helpful resources#
A beginner’s guide: https://blog.pythonanywhere.com/121/
Using a virtuanenv for you web app: https://help.pythonanywhere.com/pages/Virtualenvs
Free accounts:
Disk quota: https://help.pythonanywhere.com/pages/DiskQuota
“Batteries Included” packages: https://www.pythonanywhere.com/batteries_included/
Whitelisted sites: https://www.pythonanywhere.com/whitelist/