首页 > 解决方案 > 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 

标签: pythonplotly-dash

解决方案


推荐阅读