简体   繁体   中英

Python Dash server with waitress

I have a dashboard application written in Dash framework. It also has a few Restful API's written using flask. I am adding flask app to Dash server, as

import dash
import flask
import dash_bootstrap_components as dbc

flask_server = flask.Flask(__name__)
app = dash.Dash(__name__,server=flask_server, external_stylesheets=[dbc.themes.BOOTSTRAP])

And am running the server as

from dashboard import app
from waitress import serve

if __name__ == "__main__":
    app.title = 'Litmus'
    app.run_server(debug=False)
    # serve(app,host="0.0.0.0",port=8050)

The above code works fine when I am using app.run_server(debug=False) but it throws excetion when I use waitress to run the server. When I use following lines

#app.run_server(debug=False)
serve(app,host="0.0.0.0",port=8050)

I get following error

ERROR:waitress:Exception while serving /
Traceback (most recent call last):
  File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\channel.py", line 397, in service
    task.service()
  File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 168, in service
    self.execute()
  File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 434, in execute
    app_iter = self.channel.server.application(environ, start_response)
TypeError: 'Dash' object is not callable
ERROR:waitress:Exception while serving /favicon.ico
Traceback (most recent call last):
  File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\channel.py", line 397, in service
    task.service()
  File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 168, in service
    self.execute()
  File "C:\Users\litmus\AppData\Roaming\Python\Python38\site-packages\waitress\task.py", line 434, in execute
    app_iter = self.channel.server.application(environ, start_response)
TypeError: 'Dash' object is not callable

It's not working because you're passing the Dash app instead of the Flask app to serve .

So instead of this:

serve(app,host="0.0.0.0",port=8050)

pass the Flask app instance like this:

serve(app.server, host="0.0.0.0", port=8050)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM