Python应用开发——30天学习Streamlit Python包进行APP的构建(17):连接和数据库

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

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

此星光明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值