2017-08-30 74 views
0

我有一個現有的Flask網絡應用程序,我想將一個現有的Dash應用程序(a Plotly Flask app)加入其中。作爲Flask's documentation recommends,我使用DispatcherMiddleware正是如此來實現這一目標:Flask的DispatcherMiddleware與Plotly的破折號不搭配很好

flask_app = Flask(__name__) # App with both apps attached to it 
app = Flask(__name__) # Existing Flask App 
dash_app = Dash(__name__) # Dash app 

dash_app.config.supress_callback_exceptions = True 

# Use DispatcherMiddleware to route separate apps into one 
flask_app.wsgi_app = DispatcherMiddleware(app, {'/dash': dash_app.server}) 

我們最終運行flask_app

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

然而,當我前往127.0.0.1:<port>/dash/,我得到以下錯誤出現在網頁:

enter image description here

我看到CONSO以下日誌:

127.0.0.1 - - [30/Aug/2017 11:11:02] "GET /dash HTTP/1.1" 301 - 
127.0.0.1 - - [30/Aug/2017 11:11:02] "GET /dash/ HTTP/1.1" 200 - 
127.0.0.1 - - [30/Aug/2017 11:11:03] "GET /_dash-layout HTTP/1.1" 404 - 
127.0.0.1 - - [30/Aug/2017 11:11:03] "GET /_dash-dependencies HTTP/1.1" 404 

我怎樣才能讓我的Dash應用程序正確加載佈局?

回答

1

這就是我克服這個問題的方法。不漂亮,但有效。

您需要run.py:

from werkzeug.wsgi import DispatcherMiddleware 
from werkzeug.serving import run_simple 
from server import server 
from dash_app import app as app1 
from flask_app import app as app2 

app = DispatcherMiddleware(server, 
          {'/myflaskapp': app2}) 
if __name__ == '__main__': 
    run_simple('localhost', 5000, app, use_reloader=True) 

你server.py看起來就像這樣:

from flask import Flask, render_template 

server = Flask(__name__) 

@server.route('/') 
def server_root(): 
    return render_template('index.html') 

你dash_app.py:

import dash 
from server import server 
app = dash.Dash(name='mydash', sharing=True, server=server, url_base_pathname='/mydash') 

最後你flask_app。 py:

from flask import Flask 
app = Flask(__name__) 
@app.route("/news") 
def news(): 
    return render_template('news.html') 

你flask_app是/ myflaskapp /新聞

你dash_app是/ mydash

+0

謝謝回答。這是有道理的,但是你能否提供更多關於_why_它的作品的細節?只是好奇。 –

+1

從代碼中,dash_dependencies在服務器根目錄('/')中'留下',而不是移動到應用程序駐留的路徑'/ dash',然後佈局重新排序失敗。使用指定的url_base_pathname將依賴關係移動到指定的路徑。希望它是有道理的! – PeaceLeka