cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

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

satniks_o
New Contributor

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

2 ACCEPTED SOLUTIONS

Accepted Solutions

BigRoux
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

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

View solution in original post

4 REPLIES 4

BigRoux
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.

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

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

satniks_o
New Contributor

Thanks @BigRoux  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.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group