Connections and databases连接和数据库
Setup your connection
设定连接
st.secrets
st.secrets 提供了一个类似字典的接口,用于访问存储在 secrets.toml 文件中的机密。它的举动与 st.session_state 类似。st.secrets 可以使用 key 和属性符号。例如,st.secret.your_key 和 st.secret["your_key"]指的是同一个值。有关使用 st.secrets 的更多信息,请参阅机密管理。
secrets.toml
机密可以全局保存,也可以按项目保存。保存这两种类型的机密时,Streamlit 会合并保存的值,但假如存在重复的密钥,则优先保存项目机密。有关怎样为开辟环境格式化和定位 secrets.toml 文件,请参阅 secrets.toml。
代码
- OpenAI_key = "your OpenAI key"
- whitelist = ["sally", "bob", "joe"]
- [database]
- user = "your username"
- password = "your password"
复制代码 在 Streamlit 应用程序中,以下值将为真:
- st.secrets["OpenAI_key"] == "your OpenAI key"
- "sally" in st.secrets.whitelist
- st.secrets["database"]["user"] == "your username"
- st.secrets.database.password == "your password"
复制代码 secrets.toml
secrets.toml 是一个可选文件,你可以为工作目录或全局开辟环地步说它。当 secrets.toml 文件同时在全局和工作目录中界说时,Streamlit 会合并这些机密,并优先处理工作目录中的机密。更多信息,请参阅机密管理。
文件位置
要在本地或按项目界说机密,请将 .streamlit/secrets.toml 添加到工作目录。您的工作目录就是您调用 streamlit 运行的地方。假如之前未创建 .streamlit 目录,则须要添加该目录。
要全局界说配置,必须先找到全局 .streamlit 目录。在安装过程中,Streamlit 会将此隐藏目录添加到操作系统的用户配置文件中。对于 MacOS/Linx,该目录为 ~/.streamlit/secrets.toml。对于 Windows,则是 %userprofile%/.streamlit/secrets.toml。
文件格式
secrets.toml 是一个 TOML 文件。
代码
- OpenAI_key = "your OpenAI key"
- whitelist = ["sally", "bob", "joe"]
- [database]
- user = "your username"
- password = "your password"
复制代码 在 Streamlit 应用程序中,以下值将为真:
- st.secrets["OpenAI_key"] == "your OpenAI key"
- "sally" in st.secrets.whitelist
- st.secrets["database"]["user"] == "your username"
- st.secrets.database.password == "your password"
复制代码 st.connection
创建与数据存储或 API 的新连接,或返回现有连接。
连接的配置选项、根据、保密信息等来自不同来源:
任何特定于连接的配置文件。
应用程序的 secrets.toml 文件。
传递给此函数的 kwargs。
Function signature[source] st.connection(name, type=None, max_entries=None, ttl=None, **kwargs)
Returns (Connection object)
An initialized Connection object of the specified type.
Parameters name (str)
The connection name used for secrets lookup in [connections.<name>]. Type will be inferred from passing "sql", "snowflake", or "snowpark".
type (str, connection class, or None)
The type of connection to create. It can be a keyword ("sql", "snowflake", or "snowpark"), a path to an importable class, or an imported class reference. All classes must extend st.connections.BaseConnection and implement the _connect() method. If the type kwarg is None, a type field must be set in the connection's section in secrets.toml.
max_entries (int or None)
The maximum number of connections to keep in the cache, or None for an unbounded cache. (When a new entry is added to a full cache, the oldest cached entry will be removed.) The default is None.
ttl (float, timedelta, or None)
The maximum number of seconds to keep results in the cache
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |