Black Lives Matter. Please consider donating to Black Girls Code today.

Error with Gunicorn

Hi,

I’m trying to deploy a dash app using nginx and gunicorn. However, when I run nginx and gunicorn with these command (dash file is app.py):

sudo /etc/rc.d/init.d/nginx start

gunicorn app:app -b localhost:8000 &

I get this error:

[2018-02-09 02:39:57 +0000] [5525] [INFO] Listening at: http://127.0.0.1:8000 (5525)
[2018-02-09 02:39:57 +0000] [5525] [INFO] Using worker: sync
[2018-02-09 02:39:57 +0000] [5528] [INFO] Booting worker with pid: 5528
Application object must be callable.
[2018-02-09 02:39:58 +0000] [5528] [INFO] Worker exiting (pid: 5528)
[2018-02-09 02:39:58 +0000] [5525] [INFO] Shutting down: Master
[2018-02-09 02:39:58 +0000] [5525] [INFO] Reason: App failed to load.

I’ve tried several different python files, and they all give me this same error, even a copy and pasted dash app from the dash getting started page, so I don’t think it’s an issue with the actual dash app.

Can someone point me in the right direction? It’s probably just something small…

Thanks!

2 Likes

You need to point gunicorn at the Flask instance attached to Dash. This is not app, but rather app.server. Your command should therefore be:

gunicorn app:app.server -b localhost:8000

7 Likes

Thanks, this helped me

1 Like

Lovely

So simple. this helped me as well thanks

Thank you ! It helped me too :heart_eyes:

Hello,

This didn’t fix the same issue for me.

Firstly, I can run app.py by using “sudo python3 app.py”. This command runs app.py on 0.0.0.0:8050.

I tried adapting your command by using “gunicorn app:app.server -b 0.0.0.0:8050” but it didn’t solve my issue.

I continue to get the error code of:

“Application object must be callable.”

How can this be solved?

Maybe your Dash instance inside app.py is not called app?

The command app:app.server assumes you have a Python module app.py with a top level attribute app that is your Dash instance. You may need to adjust accordingly.

I have a Python module mvp.py with a top level attribute app that is my Dash instance. However, the command gunicorn mvp:app.server -b :8000 returns

Failed to parse ‘app.server’ as an attribute name or function call.

Unfortunately, this syntax was broken in the latest version of gunicorn (or never officially supported in the first place) - https://github.com/benoitc/gunicorn/issues/2213

So, now you have to create a variable named server inside app.py rather than referring to it as app.server. That is, include:

app = dash.Dash(__name__)
server = app.server
3 Likes

It seems to be working now, thanks.

Is there another issue open related to the gunicorn and nginx configuration blocks? Even though the server seems to be running, the page goes directly to the default 404 Error page, i.e.

nginx error! The page you are looking for is not found.

Thank you in advance.

What OS is NGINX running on?

Thank you @chriddyp for the hint, I had this issue on a fresh new install, and I was clueless.

I included the:
server = app.server in my run.py,
then started Gunicorn with:
gunicorn -w 3 run:server

And it worked. :+1: