使用用户信息个性化您的 Streamlit 应用程序

The st.user (https://docs.streamlit.io/develop/api-reference/user/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.

以下示例通过查看者的 Snowflake 用户名向其打招呼:

import streamlit as st

st.write(f"Hello, {st.user.user_name}!")

查找查看者的显示名称(可选)

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.

以下示例通过查看者的显示名称向其打招呼:

import streamlit as st

conn = st.connection("snowflake")
session = conn.session()

user_info = session.sql(
    f"DESCRIBE USER {st.user.user_name}"
).collect()
display_name = st.user.user_name
for row in user_info:
    if row["property"] == "DISPLAY_NAME":
        display_name = row["value"]
        break

st.write(f"Hello, {display_name}!")

个性化数据查询

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:

import streamlit as st

conn = st.connection("snowflake")
session = conn.session()
data = session.sql(
    "SELECT * FROM my_table WHERE owner = ?",
    params=[st.user.user_name],
).to_pandas()
st.dataframe(data)

跟踪执行操作的人员

将数据写回 Snowflake 时包含查看者的身份:

import streamlit as st

conn = st.connection("snowflake")
session = conn.session()

with st.form("entry_form"):
    value = st.text_input("Enter a value")
    submitted = st.form_submit_button("Save")

if submitted:
    session.sql(
        "INSERT INTO my_table (created_by, value) VALUES (?, ?)",
        params=[st.user.user_name, value],
    ).collect()
    st.success("Saved!")

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

运行时差异

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.