Search code examples
pythonplotly-dashuvicornasgi

TypeError: 'Dash' object is not callable


I am trying to run a dash app in a uvicorn server. The app is very simple, just displays the text 'hello' to the screen. The code is as follows:

from dash import Dash, html
dash_app = Dash(__name__)
dash_app.layout = html.Div(id='main-layout', children=["hello"])

If I run the app using dash_app.run() the app runs as expected and I can visit the page on localhost.

However, I am trying to run the app using uvicorn. The command that I am using to start the server looks like this:

uvicorn uvicorn_test.__main__:dash_app

I am running the main file in that exists in a directory called uvicorn_test.

When I run the uvicorn command, the server starts up, but when I try to access the web page, I am getting an error that says "TypeError: 'Dash' object is not callable"


Solution

  • Uvicorn is used to run ASGI (as fastapi apps) servers and not WSGI servers (as Flask apps);

    In the context of Dash, we need to use GUNICORN instead of UVICORN;

    To be able to run your code you need to:

    server = dash_app.server

    and to run it in your context you need to do:

    gunicorn app:server -b 0.0.0.0:8050

    Where app, is the app.py file;

    Let me know if it works.

    Leonardo