【问题标题】:Integrating Dash apps into Flask: minimal example将 Dash 应用程序集成到 Flask:最小示例
【发布时间】:2020-04-24 22:24:45
【问题描述】:

我想创建一个 Flask 网络应用程序。我想将几个 Dash 应用程序集成到该站点中,并在主页上显示指向每个 Dash 应用程序的链接。 这是一个最小的例子: 主页应如下所示:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
   return "Hello World"

if __name__ == "__main__":
   app.run(debug = True)

假设我们有一个如下所示的 Dash 应用程序:

import dash
import dash_html_components as html 
app = dash.Dash(__name__)

app.layout = html.Div("Hello world 1")

if __name__=="__main__":
    app.run_server(debug=True)

我现在的问题是:如何通过以下方式将 Dash 应用程序集成到 Flask 应用程序中: 1) Flask-app 中应该有一个指向 Dash 应用程序的链接 2) Dash 应用程序的位置应该是例如/dash(在这种情况下主页位于/) 3) 如果有是第二个和第三个 Dash-app,应该很容易添加另一个链接和位置(例如/dash2/dash3,...)

有很多帖子处理这个问题 - 但是,我没有找到最小的例子。

【问题讨论】:

    标签: python flask plotly-dash


    【解决方案1】:

    将一个或多个 Dash 应用与现有 WSGI 应用结合

    以下示例通过将两个 Dash 应用与一个 Flask 应用相结合来说明这种方法。

    flask_app.py

    from flask import Flask
    
    flask_app = Flask(__name__)
    
    @flask_app.route('/')
    def index():
        return 'Hello Flask app'
    

    app1.py

    import dash
    import dash_html_components as html
    
    app = dash.Dash(
        __name__,
        requests_pathname_prefix='/app1/'
    )
    
    app.layout = html.Div("Dash app 1")  
    

    app2.py

    import dash
    import dash_html_components as html
    
    app = dash.Dash(
        __name__,
        requests_pathname_prefix='/app2/'
    )
    
    app.layout = html.Div("Dash app 2") 
    

    wsgi.py

    from werkzeug.wsgi import DispatcherMiddleware
    
    from app1 import app as app1
    from app2 import app as app2
    
    application = DispatcherMiddleware(flask_app, {
        '/app1': app1.server,
        '/app2': app2.server,
    })  
    

    在此示例中,Flask 应用程序已安装在 / 并且两个 Dash 应用程序已安装在 /app1 和 /app2。在这种方法中,我们不会将 Flask 服务器传递给 Dash 应用程序,而是让它们创建自己的,DispatcherMiddleware 根据传入请求的前缀将请求路由到这些应用程序。在每个 Dash 应用中,requests_pathname_prefix 必须指定为应用的挂载点,以匹配 DispatcherMiddleware 设置的路由前缀。

    请注意,wsgi.py 中的应用程序对象是 werkzeug.wsgi.DispatcherMiddleware 类型,它没有运行方法。这可以像这样作为 WSGI 应用程序运行:

    $ gunicorn wsgi:application 
    

    或者,您可以使用 Werkzeug 开发服务器(不适合生产)来运行应用程序:

    run.py

    from werkzeug.wsgi import DispatcherMiddleware
    from werkzeug.serving import run_simple
    
    from app1 import app as app1
    from app2 import app as app2
    
    application = DispatcherMiddleware(flask_app, {
        '/app1': app1.server,
        '/app2': app2.server,
    })
    
    if __name__ == '__main__':
        run_simple('localhost', 8050, application) 
    

    如果您在使用这种方法时需要访问 Dash 开发工具(无论是使用 WSGI 服务器运行,还是使用 Werkzeug 开发服务器),您必须为每个 Dash 应用手动调用它们。可以在初始化 DispatcherMiddleware 之前添加以下行来执行此操作:

    app1.enable_dev_tools(debug=True)
    app2.enable_dev_tools(debug=True)  
    

    注意:调试模式不应在生产中启用。在 Gunicorn 中使用调试模式时,需要 --reload 命令行标志才能使热重载工作。

    在此示例中,与两个 Dash 应用程序组合的现有应用程序是一个 Flask 应用程序,但是这种方法可以组合任何实现 WSGI 规范的 Web 应用程序。可以在包含一个或多个 Dash 应用程序的 WSGI 文档中找到 WSGI Web 框架列表。

    参考 - https://dash.plot.ly/integrating-dash

    已编辑:

    没有 WSGI 的多个 Dash 应用

    from dash import Dash
    from werkzeug.wsgi import DispatcherMiddleware
    import flask
    from werkzeug.serving import run_simple
    import dash_html_components as html
    
    server = flask.Flask(__name__)
    dash_app1 = Dash(__name__, server = server, url_base_pathname='/dashboard/')
    dash_app2 = Dash(__name__, server = server, url_base_pathname='/reports/')
    dash_app1.layout = html.Div([html.H1('Hi there, I am Dash1')])
    dash_app2.layout = html.Div([html.H1('Hi there, I am Dash2')])
    @server.route('/')
    @server.route('/hello')
    def hello():
        return 'hello world!'
    
    @server.route('/dashboard/')
    def render_dashboard():
        return flask.redirect('/dash1')
    
    
    @server.route('/reports/')
    def render_reports():
        return flask.redirect('/dash2')
    
    app = DispatcherMiddleware(server, {
        '/dash1': dash_app1.server,
        '/dash2': dash_app2.server
    })
    
    run_simple('0.0.0.0', 8080, app, use_reloader=True, use_debugger=True)
    

    【讨论】:

    • 好的,我已经阅读了这篇文章,但我想知道是否可以在不使用 wsgi 的情况下实现这一目标?
    • 我已经用没有 wsgi 的例子更新了答案
    • 非常感谢!这很有帮助!
    • 我收到ImportError: cannot import name 'DispatcherMiddleware' from 'werkzeug.wsgi'。我相信DispatcherMiddleware 已在werkzeug 1.0.1 中从werkzeug.wsgi 中删除。
    【解决方案2】:

    将一个或多个 Dash 应用程序与现有 WSGI 应用程序结合多个 Dash 应用程序不使用 WSGI 中似乎没有什么变化并且需要更新。

    1. DispatcherMiddleware 现在需要从werkzeug.middleware.dispatcher 导入,而不是run.py / wsgi.py 中的werkzeug.wsgi 导入。
    2. 另外,flask_app 需要正确导入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2018-06-30
      相关资源
      最近更新 更多