【问题标题】:How to set and access environment variables in python file for Streamlit app?如何在 Streamlit 应用程序的 python 文件中设置和访问环境变量?
【发布时间】:2022-05-06 20:41:43
【问题描述】:

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

【问题讨论】:

    标签: python streamlit


    【解决方案1】:

    在您部署应用程序之前,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

    【讨论】:

      猜你喜欢
      • 2014-12-23
      • 2012-04-26
      • 1970-01-01
      • 2021-06-01
      • 2020-10-03
      • 2017-04-20
      • 2019-08-15
      • 1970-01-01
      • 2020-10-26
      相关资源
      最近更新 更多