在Flask应用程序中运行Dash应用程序 [英] Running a Dash app within a Flask app

查看:627
本文介绍了在Flask应用程序中运行Dash应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Flask应用程序,并且我希望有一条通往另一个应用程序的路线.更具体地说,第二个应用程序是 Plotly Dash 应用程序.如何在现有的Flask应用中运行Dash应用?

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 existing Flask app?

@app.route('/plotly_dashboard') 
def render_dashboard():
    # go to dash app

由于它是Flask应用程序,因此我还尝试了向Dash实例添加路由,但出现错误:

I also tried adding a route to the Dash instance, since it's a Flask app, but I get the error:

AttributeError: 'Dash' object has no attribute 'route'

推荐答案

来自文档:

底层的Flask应用程序可从app.server获得.

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

您还可以将自己的Flask应用实例传递到Dash:

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

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

现在有了Flask实例,您可以添加所需的任何路由和其他功能.

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!'


对于更普遍的问题我如何才能彼此相邻地服务两个Flask实例",假设您最终没有使用上述Dash答案中的一个实例,则可以使用


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})

这篇关于在Flask应用程序中运行Dash应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆