How to get logged in user name/email in the databricks streamlit app?

satniks_o
New Contributor III

I have created a Databricks App using streamlit and able to deploy and use it successfully.

I need to get the user name/email address of the logged in user and display in the streamlit app. Is this possible?

If not possible at the moment, any roadmap for adding this feature?

regards,

--

satniks

Louis_Frolio
Databricks Employee
Databricks Employee

Yes, it is possible to retrieve and display the username or email address of the logged-in user in a Streamlit app deployed on Databricks. This can be achieved through two primary approaches:

1. Using "st.experimental_user" in streamlit.

import streamlit as st

# Access the user's email

user_email = st.experimental_user.get("email", "Unknown User")

# Display the email in the app st.write(f"Logged in as: {user_email}")

2. Using HTTP Headers in Databricks Apps.

import os

import streamlit as st

# Access headers passed by Databricks Apps

user_email = os.getenv("X-Forwarded-Email", "Unknown User")

# Display the user's email

st.write(f"Logged in as: {user_email}")

 

Hope this helps. Cheers, Louis.

View solution in original post

X-Forwarded-Email worked. I assume you mean using st.context.headers.get() instead of os.getenv().

Shannon_O
New Contributor III
user_email = st.context.headers.get("X-Forwarded-Email")
st.write(f"email: {user_email}")

View solution in original post

satniks_o
New Contributor III

Thanks @Louis_Frolio  and @Shannon_O  for your response.

I tried st.experimental_user.get() API but it gives me hardcoded response as test@example.com 

Suggestion to use X-Forwarded-Email header worked and I got correct email address. Thanks for your help. I am unblocked now.

Carl_B
New Contributor II

I have also tried to deploy a streamlit app, however I was not able to deploy it.