Running a Dash app within a Flask app

后端 未结 4 1246
时光取名叫无心
时光取名叫无心 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条回答
  •  Happy的楠姐
    2020-11-29 19:01

    Ok for those who are lazy enough like me, here is the code

    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)
    

提交回复
热议问题