仅当用户按下我的 html 模板之一中的按钮时,我才尝试在 Flask 构建的网站内初始化 Dash 应用程序。到目前为止,我所看到的所有解决方案和解决方法,无论是否将其粘贴在函数中,都会在通用 Falsk 应用程序 init.py 文件中启动 Dash 应用程序。但我希望 Dash 应用程序仅在用户按下我网站中的按钮时才运行资源,而不是从用户进入网站后一直挂在那里。
我尝试了很多方法,例如:
https://github.com/toddbichard/plotlydash-flask-tutorial/blob/master/plotly_flask_tutorial
https://hackersandslackers.com/plotly-dash-with-flask/
但这些都不能让我到达我想去的地方。 尝试一些原创的想法,我尝试为我的按钮创建一条路线,该路线将启动 Dash 应用程序,然后重定向到 de url_base_pathname。到目前为止,失败了。我的文件结构:
main.py:
from portfolio_felipe import app
if __name__ == '__main__':
app.run(debug= True)
portfolio_felipe.init.py(这是我遇到的所有解决方案都会启动 Dash 应用程序的地方,这就是我试图避免的,因为它为我网站的许多其他部分占用了不必要的资源):
from flask import Flask
app = Flask(__name__)
from portfolio_felipe import routes
dash_nba.init.py:
from dash import Dash, dcc, html, callback, Output, Input
import pandas as pd
import plotly.express as px
def create_dash_nba(flask_app):
dash_app= Dash(server= flask_app, name='dashnba', url_base_pathname='/dashnba/')
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv')
dash_app.layout = html.Div([
html.H1(children='Title of Dash App', style={'textAlign':'center'}),
dcc.Dropdown(df.country.unique(), 'Canada', id='dropdown-selection'),
dcc.Graph(id='graph-content')
])
return dash_app.server
在我的portfolio_felipe.routes.py 文件中,我正在尝试类似的操作:
@app.route('/button_use')
def button_use():
dash_app= create_dash_nba(app)
return redirect('/dashnba/')
我真的很想解释一下为什么我的逻辑有缺陷,因为我似乎真的无法理解记录在案的 Flask 和 Dash 对象及其方法。如果它不是完全有缺陷,我在哪里失去了我的脚步,我应该做什么才能让它发挥作用。但我也欢迎一个完整的解决方法,对我的代码和文件结构进行完全重制。预先感谢您的帮助!
我目前也面临着同样的问题。你找到解决办法了吗?
致以诚挚的问候