Error deploying simple Dash app on Pythonanywhere

I’m trying to build a simple Dash app on Pythonanywhere but encountered errors that I don’t know how to fix. Thanks in advance!

File “app.py”:

from dash import Dash, html

dash_app = Dash()

dash_app.layout = [html.Div(children='Hello World')]

WSGI file:

import sys

# add your project directory to the sys.path
project_home = '/home/weiqizhang/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from app import dash_app   # noqa
application = dash_app.server

Here is the error log:
2024-06-08 04:10:49,581: Error running WSGI application
2024-06-08 04:10:49,581: dash.exceptions.NoLayoutException: Layout must be a dash component or a function that returns a dash component.
2024-06-08 04:10:49,581: File “/var/www/weiqizhang_pythonanywhere_com_wsgi.py”, line 16, in
2024-06-08 04:10:49,581: from app import dash_app # noqa
2024-06-08 04:10:49,582:
2024-06-08 04:10:49,582: File “/home/weiqizhang/mysite/app.py”, line 5, in
2024-06-08 04:10:49,582: dash_app.layout = [html.Div(children=‘Hello World’)]
2024-06-08 04:10:49,582:
2024-06-08 04:10:49,582: File “/usr/local/lib/python3.10/site-packages/dash/dash.py”, line 551, in layout
2024-06-08 04:10:49,582: _validate.validate_layout_type(value)
2024-06-08 04:10:49,582:
2024-06-08 04:10:49,582: File “/usr/local/lib/python3.10/site-packages/dash/_validate.py”, line 383, in validate_layout_type
2024-06-08 04:10:49,583: raise exceptions.NoLayoutException(
2024-06-08 04:10:49,583: ***************************************************
2024-06-08 04:10:49,583: If you’re seeing an import error and don’t know why,
2024-06-08 04:10:49,583: we have a dedicated help page to help you debug:
2024-06-08 04:10:49,583: Debugging with sys.path / ImportError issues | PythonAnywhere help
2024-06-08 04:10:49,583: ***************************************************

my wsgi file usually looks like:

from app import server

if __name__ == "__main__":
    server.run()

and I setup my app.py like:

from flask import Flask
from dash import *

server = Flask(__name__)

app = Dash(
    __name__)

layout= html.Div(children=['Hello World'])

# Define the main function that will run the app
if __name__ == "__main__":
    app.run_server(debug=True, host="localhost", port="8212")

Hi @weiqi.zhang
The wsgi file looks correct.

If you are using a virtual environment, please make sure that you are using the latest version of Dash.

As of dash 2.17 the layout can be a list of components, for example:
app.layout = [...]

Prior to dash==2.17, it was necessary for the layout to be a dash component or a function that returns a dash component. For example:
app.layout=html.Div([...])

1 Like

The Bash of pythonanywhere shows I’m using dash==2.4.1.

I changed the app to the following and it worked:

from dash import Dash, html

dash_app = Dash()

dash_app.layout = html.Div(html.H1("hello world"))

I’m not using virtual environment. do you think I should create one and update to dash==2.17?

@weiqi.zhang
Well, it depends what type of app you would like to make. You can run simple apps and most of the examples in the dash-docs using this version. You can see what’s included in the default environment here:

If you want to use the latest dash features or other libraries such as dash-bootstrap-components or dash-ag-grid, you will need to make a virtual environment.

1 Like

Thank you. Creating a virtualenv now.

1 Like

Just a few tips if you are trying to stay in the free tier:

Start by installing libraries one at a time and keep an eye on the file usage after each library. Large libraries like tensorflow will eat up all of it.

If you reach the limit uninstall the library and delete the cache

2 Likes