✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Dash application for multiple users

Hi everyone,

I have a question about how can Dash show different plots for different users.

I have created a dashboard with user login page. For example, if there are two users user_A and user_B, their usernames are used to find the right path to the dataset (csv files), and cached.

TIMEOUT = 60
@cache.memoize(timeout=TIMEOUT)
def query_data():
    if current_user.is_authenticated:
        try:
            if current_user.admin == 1:
                username = 'user_admin'
            else:
                # client-login
                username = current_user.username

            csv_path = '../../dataset/' + username + '/opt/'
        except IOError as e:
            print('Invalid input config file path')
        # import dataframe
        m_df = pd.read_csv(csv_path + 'm_Data.csv')
        m_json = m_df.to_json(orient='split')

        sc_df = pd.read_csv(csv_path + 'sc_Data.csv')
        sc_json = sc_df.to_json(orient='split')
    return m_json, sc_json

def get_dataframe(activity):
    m_json, sc_json = query_data()
    if activity == 'm':
        return pd.read_json(m_json, orient='split')
    elif activity == 'sc':
        return pd.read_json(sc_json, orient='split')
    else:
        print('Invalid input dataframe')
        return pd.DataFrame()

Then in the graph section, the get_dataframe is called to import the dataframe based on which activity that user selected. Once user log into the dashboard, they are only allowed to select the data that belong to them (and their data will be stand out from other points).

image

However, I have two issues

  • If two different users log in at the time, only one of them can see their standed out data. Tihs means that this dashboard cannot serve multiple users at the same time.

  • Because I use mysql database to manage user information, sometimes, it also reports this error:

    sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, ‘Lost connection to MySQL server during query’)

I am new to the dash, not sure if Dash or Flask has any solution for my problem?

Many Thanks,

Yihao

1 Like

If your are using sqlalchemy, just add this pool_recycle argument:

from sqlalchemy import create_engine
e = create_engine("mysql://user:pass@localhost/db", pool_recycle=1800)

Try to run your app using gunicorn when you have ‘real’ users using.