如何在 python 文件中为 Streamlit 应用程序设置和访问环境变量?

How to set and access environment variables in python file for Streamlit app?

将文件部署到 github 以部署到 Streamlit 应用程序时,如何正确设置密钥或环境变量?

在您部署应用程序之前,Streamlit 在高级设置中有一个选项可以存储您的密钥和环境变量

在您的应用程序中,您可以按如下方式查询变量:

import streamlit as st

# Everything is accessible via the st.secrets dict:

st.write("DB username:", st.secrets["db_username"])
st.write("DB password:", st.secrets["db_password"])
st.write("My cool secrets:", st.secrets["my_cool_secrets"]["things_i_like"])

# And the root-level secrets are also accessible as environment variables:

import os

st.write(
    "Has environment variables been set:",
    os.environ["db_username"] == st.secrets["db_username"],
)

来源:https://docs.streamlit.io/streamlit-cloud/get-started/deploy-an-app/connect-to-data-sources/secrets-management