我有一个现有的Flask应用程序,我希望有一个到另一个应用程序的路径。更具体地说,第二个应用是Plotly Dash应用。如何在现有的Flask应用程序中运行我的Dash应用程序?
MERGE
我还尝试添加一个到Dash实例的路由,因为它是一个Flask应用程序,但我收到错误:
@app.route('/plotly_dashboard')
def render_dashboard():
# go to dash app
答案 0 :(得分:38)
来自docs:
基础Flask应用程序位于
app.server
。import dash app = dash.Dash(__name__) server = app.server
您还可以将自己的Flask应用实例传递给Dash:
import flask server = flask.Flask(__name__) app = dash.Dash(__name__, server=server)
现在您已拥有Flask实例,您可以添加所需的任何路由和其他功能。
@server.route('/hello')
def hello():
return 'Hello, World!'
对于更一般的问题"如何为彼此旁边的两个Flask实例提供服务",假设您不会像上面的Dash答案那样结束使用一个实例,那么您将使用{ {3}}安装两个应用程序。
dash_app = Dash(__name__)
flask_app = Flask(__name__)
application = DispatcherMiddleware(flask_app, {'/dash': dash_app.server})
答案 1 :(得分:24)
在Dash实例中设置qmlRegisterType<interfacageQML>("Interfacage", 1, 0, "Component:MouseArea");
。
url_base_pathname
现在,您可以在任何您想要的Flask路线下重定向到您的Plotly Dashboard应用程序。
app_flask = flask.Flask(__name__)
app_dash = dash.Dash(__name__, server=app_flask, url_base_pathname='/pathname')
答案 2 :(得分:3)
好吧,像我这样懒惰的人,这是代码
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 app1 for dashboards')])
dash_app2.layout = html.Div([html.H1('Hi there, I am app2 for reports')])
@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)
答案 3 :(得分:1)
答案 4 :(得分:-3)
要解决这个问题,这就是我所做的并取得了成功。这应记录在官方DASH文档中
####################################
import dash_core_components as dcc
import dash_html_components as html
from dash import Dash
from dash.dependencies import Input, State, Output
from flask import Flask, flash, redirect, render_template, request, session, abort, url_for, json, make_response
url_router=''
@application.route("/view_tables", methods=['GET','POST'])
def view_tabales:
# Logic for displaying dashboard using Dash
server.layout = html.Div(
children=[
#division for graph 1
html.Div([html.H1(children='Capital Charge'),],className='text-center'),
html.Div([html.Div([html.H3(children='''Correlation for assets'''),],className='text-primary'),
# define the graph
dcc.Graph(
id='Delta-graph',
figure={
'data': [
{'x': df_delta['Correlation_Level'],
'y': df_delta['Capital_Charge'],
'type': 'bar',
'name': 'Delta',
#'domain': {'x': [0, .48],'y': [0, .49]},
}
],
# sizes the graph
'layout': {
'title': 'Delta','margin': {'l': 10, 'r': 0, 't': 30, 'b': 10},
"height":300,
}
}
)],className='col-md-4'),
url_router = 'Dash(__name__,server=application, url_base_pathname="/dash")'
然后,您可以控制从烧瓶内走向哪个仪表板
if url_router !='':
server = url_router
server.layout = html.Div(children = [html.H1(children = ' MEP dashboard - error 404')])
# run the app.
if __name__ == "__main__":
# Setting debug to True enables debug output. This line should be
# removed before deploying a production app.
server.secret_key = os.urandom(12)
server.run_server(debug=True,port=5000)
您可以在Flask代码之间使用不同的图形创建不同的函数,并继续调用破折号
中的代码