python - Dash - 具有多个可能仪表板的多用户
问题描述
我正在构建一个多用户 Dash 应用程序,其中基于数据库中的某些凭据,用户只有在经过验证后才能看到某个仪表板。就像经理可以在仪表板中看到所有员工数据一样,但员工只能看到那里的数据。
我正在尝试能够加载一个 python 文件,该文件将能够包含一个破折号应用程序的整个布局、函数和回调,以便我可以将它们全部放在一个地方。但是,我一直无法弄清楚如何做到这一点。这有可能以任何方式吗?
def create_dashboard(server):
"""Create a Plotly Dash dashboard embedded within Flask"""
dash_app = dash.Dash(server=server,
routes_pathname_prefix='/dashapp/',
external_stylesheets=[dbc.themes.CERULEAN],
)
dash_app.layout = html.Div(children=[
html.Div([
html.Div([
html.H1(id="my-header", className="text-center"),
], className="col-md-12")
], className="row"),
html.A('Login', id="login-link", href="/login"),
html.Div(id="my-div", className="text-center"),
html.Button(id='submit-button-state', n_clicks=1, children='Submit',
style={'display': 'none'}),
])
@dash_app.callback(
[Output(component_id='my-header', component_property='children'),
Output(component_id='my-div', component_property='children'),
Output(component_id='login-link', component_property='style')],
[Input(component_id='submit-button-state', component_property='n_clicks')]
)
def get_user_name(n_clicks):
"""
The dashboard only loads if a user is authenticated
"""
if routes.get_user().is_authenticated:
welcome_msg = "Welcome back, " + routes.get_user().username
user_data = load_data()
link_style = {'display':'none'}
return welcome_msg, user_data, link_style
return "Your Princess is in another castle", ''
def load_data():
"""
The user's data is pulled from a database. As the user has already been
authenticated, the 'current_user' from 'flask_login' is used within a database
query, ensuring that the correct user's data is loaded. This is the key to
having multiple user authentication with dedicated views for each user.
"""
u = routes.get_user()
data = u.data_for_user.all()
if data.x1 == some data:
dash_app = dashboard.py # some dashboard py file that needs to be loaded for the user
return dash_app
return dash_app.server
解决方案
推荐阅读
- html - 即使使用移动视图,如何使标签、文本输入和错误消息在同一行
- php - 嵌套数组、对象输出的自动格式化程序
- python - 为什么我的登录功能只是重定向而不执行 func 中所写的内容
- javascript - React如何将变量传递给组件.jsx到另一个组件jsx
- python - Python,MYSQL 如何将两个单独列表中的两列加在一起(数字总和)到一个新列表中?
- python - 如何在循环中间更改输入
- xml - 无法使用自制 perl 5.32 在 M1 Mac 上安装 XML::Parser
- postgresql - postgres uuid_generate_v4() 最大值是多少?
- ios - 在整个应用程序中设置语言 - SwiftUI
- c++ - c++用char指针替换char指针的字符