Personalize your Streamlit app with user information¶
The st.user API lets your Streamlit in Snowflake app access information about the person currently
viewing the app. You can use this to display personalized greetings, filter data by user,
or track who performed an action.
What st.user provides in Streamlit in Snowflake¶
In Streamlit in Snowflake, st.user provides two attributes for the current viewer:
st.user.user_name– the viewer’s Snowflake username.st.user.email– the viewer’s email address.
The following example greets the viewer by their Snowflake username:
Look up the viewer’s display name (optional)¶
st.user.user_name returns the Snowflake username (for example, JSMITH). To
show a friendlier name, you can look up the user’s display name with
DESCRIBE USER. This requires elevated privileges:
the app owner’s role must have the MONITOR privilege on the user object, or be an account
administrator.
The following example greets the viewer by their display name:
Personalize data queries¶
Filter query results so each viewer sees only their own data. This example uses
session.sql() instead of conn.query() so that the query runs fresh on every
rerun rather than returning cached results:
Track who performed an action¶
Include the viewer’s identity when writing data back to Snowflake:
Relationship to CURRENT_USER()¶
The SQL function CURRENT_USER() returns the Snowflake username of the session owner. In
Streamlit in Snowflake apps using owner’s rights, this is the owner of the Streamlit object, not the viewer.
To identify the viewer in your Python code, use st.user instead.
If you need the viewer’s identity in SQL queries, consider using restricted caller’s rights (Preview). With restricted caller’s rights, queries run as the viewer, so CURRENT_USER() returns the viewer’s identity. For more information, see Restricted caller’s rights and Streamlit in Snowflake.
Runtime differences¶
st.user is available in both container and warehouse runtimes. The behavior is
the same in both environments: it returns the identity of the person viewing the app,
not the owner of the Streamlit object.