Dash App - Get User ID

Hello,

Is there a way to fire an event (e.g. pull User ID) whenever a user opens an app?

I know I can get User ID using the following python code, but I am confused about how to tell Dash to run those functions when a user visits my app?

import getpass
getpass.getuser()

I’m planning on using this UserId together with the login time stamp to track usage on my app.

Thanks,
Dawn

Are you using dash-auth? If so, see Get username for authenticated user with Dash Basic Auth

1 Like

This worked for me! Thanks Chris :slight_smile:

Hi Chris I have seen the link that you provided for user authentication. However in my use case i do not want user to enter their credentials every time. I just want to display the user id when he/she access the app. Currently i have tried using the method
import getpass
getpass.getuser()
However the issue with this is that it displays the admin ID where the app was hosted, when a new user tries to access the app from his browser it still shows the admin ID instead of the user who access the app through his browser.

I would like to know of a way where the app should be able to pull the user id from the browser itself from where the user is accessing the app

Is there a way to use getpass.getuser() from a callback request so that i can capture the user ID like we have for capturing the IP address when the user uploads any file:

@app.callback(
    Output("tabs-content-example", "children"),
    [Input("upload-data", "filename"), Input("upload-data", "contents"),Input('tabs-example', 'value')]
)
def update_output(uploaded_filenames, uploaded_file_contents,tab):
    """Save uploaded files and regenerate the file list."""
    def get_ip(uploaded_filenames):
        print(request.remote_addr)
        return html.Div(request.remote_addr)
    
    if uploaded_filenames is not None and uploaded_file_contents is not None and tab == 'tab-2-example':
        for name, data in zip(uploaded_filenames, uploaded_file_contents):
            save_file(name, data)
        filename=name
        
        children= uploaded_files(filename)
        
        return children,get_ip(uploaded_filenames)