跳转到主要内容

Streamlit连接云和远程文件存储。

项目描述

Streamlit FilesConnection

从您的Streamlit应用程序连接到云(或本地)文件存储。由st.experimental_connection()fsspec提供支持。适用于Streamlit >= 1.22。

任何fsspec兼容的协议都应该工作,只需安装即可。有关Streamlit连接的更多信息,请参阅官方文档

快速入门

请参阅示例目录,以获取使用S3和/或GCS的完整示例。

pip install streamlit
pip install git+https://github.com/streamlit/files-connection

注意:将从pypi安装即将推出

import streamlit as st
from st_files_connection import FilesConnection

"# Minimal FilesConnection example"
with st.echo():
    conn = st.experimental_connection('my_connection', type=FilesConnection)

# Write a file to local directory if it doesn't exist
test_file = "test.txt"
try:
    _ = conn.read(test_file, input_format='text')
except FileNotFoundError:
    with conn.open(test_file, "wt") as f:
        f.write("Hello, world!")

with st.echo():
    # Read back the contents of the file
    st.write(conn.read(test_file, input_format='text'))

用于云文件存储

您可以将协议名称传递给st.experimental_connection()作为第一个参数,用于任何已知的fsspec协议

# Create an S3 connection
conn = st.experimental_connection('s3', type=FilesConnection)

# Create a GCS connection
conn = st.experimental_connection('gcs', type=FilesConnection)

# Create a Weights & Biases connection
conn = st.experimental_connection('wandb', type=FilesConnection)

对于云文件存储工具(或任何需要配置/凭证的工具),您可以以两种方式指定它

  • 使用底层库的本地配置/凭证方法(例如配置文件或环境变量)
  • 使用Streamlit密钥

对于Streamlit密钥,在你的.streamlit/secrets.toml文件中创建一个名为[connections.<name>]的部分,并添加参数。你可以传递给fsspec文件系统构造函数的任何内容。此外

  • 对于GCS,密钥的内容假定是令牌文件的密钥(例如,它作为{"token":{<secrets>}}字典传递)

主要方法

read()

conn.read("path/to/file", input_format="text|csv|parquet|json|jsonl" or None, ttl=None) -> pd.DataFrame

指定文件路径和输入格式。可选地指定缓存TTL。

input_format=的有效值

  • text返回一个字符串
  • json返回一个字典或列表(取决于JSON对象)- 每个文件只支持一个对象
  • csvparquetjsonl返回一个pandas DataFrame
  • None将尝试从path的文件扩展名推断输入格式
  • 其他任何内容(或未识别的推断类型)都会引发一个ValueError
conn = st.experimental_connection("s3", type=FilesConnection)
df = conn.read(f"my-s3-bucket/path/to/file.parquet", input_format='parquet')
st.dataframe(df)

注意:我们希望添加一个format=参数以指定输出格式并提供更多选项,欢迎贡献!

open()

conn.open("path/to/file", mode="rb", *args, **kwargs) -> Iterator[TextIOWrapper | AbstractBufferedFile]

与fsspec AbstractFileSystem.open()类似。

fs

使用conn.fs来访问底层FileSystem对象API

贡献

欢迎为此仓库做出贡献。我们仍在探索,并希望随着时间的推移可能得到一些使用。我们希望保持API非常简单,并不过度增加维护面积。如果您有兴趣帮助维护它,请与我们联系。如果您需要几天时间才能回复,请耐心等待。

提交您可能想工作的想法的最佳方式是打开一个issue并标记@sfc-gh-jcarroll和/或任何其他列出的贡献者。请在我们检查之前不要花费大量时间在PR上工作,因为这可能导致您的工作被浪费并让您感到沮丧。

请注意,Streamlit的experimental_connection()接口对第三方包开放,我们期待在生态系统中推广高质量的包。如果您有一个与我们的方向不同的想法,我们非常愿意您分叉/克隆、构建它,并与我们以及更广泛的社区分享。谢谢!

项目详情


下载文件

为您的平台下载文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

st-files-connection-0.1.0.tar.gz (9.8 kB 查看散列)

上传时间

构建分布

st_files_connection-0.1.0-py3-none-any.whl (10.8 kB 查看散列)

上传时间 Python 3

支持者