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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 10:50:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 10:50:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 10:50:03       45 阅读
  4. Python语言-面向对象

    2024-07-21 10:50:03       55 阅读

热门阅读

  1. LeetCode 150, 112, 130

    2024-07-21 10:50:03       17 阅读
  2. 《李彦宏在世界人工智能大会发言的深度洞察》

    2024-07-21 10:50:03       11 阅读
  3. 录入学生信息

    2024-07-21 10:50:03       17 阅读
  4. 分布式ID是什么?有哪些解决方案?

    2024-07-21 10:50:03       16 阅读
  5. 【c++】c++新概念“列表”

    2024-07-21 10:50:03       13 阅读
  6. test_demo

    2024-07-21 10:50:03       15 阅读
  7. C语言MAX_PATH和PATH_MAX的区别

    2024-07-21 10:50:03       14 阅读
  8. jEasyUI 创建简单窗口

    2024-07-21 10:50:03       18 阅读
  9. TMS320F28335多级中断及中断响应过程

    2024-07-21 10:50:03       13 阅读
  10. 微信小程序反编译工具安装【PC端程序包详解】

    2024-07-21 10:50:03       16 阅读