python 在Flask应用中运行Dash应用

xggvc2p6  于 2023-08-02  发布在  Python
关注(0)|答案(5)|浏览(231)

我有一个现有的Flask应用程序,我想有一个路由到 * 另一个应用程序 *。更具体地,第二应用是Plotly Dash应用。如何在现有的Flask应用中运行我的达世币应用?

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

字符串
我还尝试向Dash示例添加一个路由,因为它是一个Flask应用程序,但我得到了错误:

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

jv4diomz

jv4diomz1#

关于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答案那样使用一个示例,您将使用DispatcherMiddleware来挂载两个应用程序。

dash_app = Dash(__name__)
flask_app = Flask(__name__)

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

hrirmatl

hrirmatl2#

在您的达世币示例中设置url_base_pathname

app_flask = flask.Flask(__name__)

app_dash = dash.Dash(__name__, server=app_flask, url_base_pathname='/pathname')

字符串
现在,您可以重定向到您的Plotly Dashboard应用程序下的任何Flask路由。

@app_flask.route('/plotly_dashboard') 
def render_dashboard():
    return flask.redirect('/pathname')

q7solyqu

q7solyqu3#

好了,对于那些像我一样足够懒惰的人来说,这里是代码

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)

字符串

xoefb8l8

xoefb8l84#

#here are full codee
from dash import Dash
import flask
from dash import html

server = flask.Flask(__name__)
app = Dash(__name__, server=server, url_base_pathname='/ATM_Data_Anlaysis/')
app.layout = html.Div([html.H1('This Is head',style={'textAlign':'center'})])

@server.route("/dash")
def MyDashApp():
    return app.index()

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

字符串

wqlqzqxt

wqlqzqxt5#

为了解决这个问题,这是我所做的,并取得了成功。这应记录在官方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")'

字符串
然后,您可以控制哪个 Jmeter 板,它是领导从内 flask

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代码之间创建具有不同图形的不同函数,并在破折号中继续调用代码

相关问题