Running a Dash app within a Flask app

后端 未结 4 1268
时光取名叫无心
时光取名叫无心 2020-11-29 18:09

I have an existing Flask app, and I want to have a route to another app. More concretely, the second app is a Plotly Dash app. How can I run my Dash app within my e

4条回答
  •  广开言路
    2020-11-29 19:00

    From the docs:

    The underlying Flask app is available at app.server.

    import dash
    app = dash.Dash(__name__)
    server = app.server
    

    You can also pass your own Flask app instance into Dash:

    import flask
    server = flask.Flask(__name__)
    app = dash.Dash(__name__, server=server)
    

    Now that you have the Flask instance, you can add whatever routes and other functionality you need.

    @server.route('/hello')
    def hello():
        return 'Hello, World!'
    

    To the more general question "how can I serve two Flask instances next to each other", assuming you don't end up using one instance as in the above Dash answer, you would use DispatcherMiddleware to mount both applications.

    dash_app = Dash(__name__)
    flask_app = Flask(__name__)
    
    application = DispatcherMiddleware(flask_app, {'/dash': dash_app.server})
    

提交回复
热议问题