Streamlit库的相关函数用法汇总
streamlit.file_uploader
streamlit.file_uploader(label, type=None, accept_multiple_files=False, key=None)
参数详解:
label:文件上传器的标签,显示在上传按钮的旁边。
type:可选的文件类型,用于限制用户可以上传的文件类型。它可以是一个字符串或者字符串列表,例如type="image/*"表示只允许上传图片文件,type=[".csv", ".txt"]表示只允许上传CSV和文本文件。
accept_multiple_files:一个布尔值,指示是否允许用户上传多个文件。默认为False。
key:用于Streamlit的响应性重建的关键字。如果您在一个应用中有多个文件上传器,您可能需要为它们指定唯一的键以确保正确的响应性行为。
用法举例:
import streamlit as st
# 创建文件上传器,允许上传单个文件,限制为CSV文件
uploaded_file = st.file_uploader("上传CSV文件", type=["csv"])
# 如果用户上传了文件
if uploaded_file is not None:
# 读取上传的CSV文件
df = pd.read_csv(uploaded_file)
# 在Streamlit中显示DataFrame
st.write(df)
在上述示例中,我们创建了一个文件上传器,允许用户上传单个CSV文件。如果用户上传了文件,我们使用pd.read_csv函数读取上传的CSV文件,并使用st.write函数在Streamlit应用程序中显示DataFrame。
当需要上传多个文件时,代码如下所示:↓↓↓
uploaded_content_file = st.file_uploader("上传待合成内容图像",accept_multiple_files=True , type="jpg",on_change=temp)
# 如果用户上传了图片
if uploaded_content_file is not None:
# 遍历每个上传的文件对象
for uploaded_file in uploaded_content_file:
# 获取图片内容
image_data = uploaded_file.read()
# 在Streamlit中显示图片
st.image(image_data, caption='Uploaded Image.', use_column_width=True)
streamlit.image
streamlit.image(image, caption=None, width=None, use_column_width=False, clamp=False, channels='RGB', format='JPEG')
参数详解:
image:要显示的图像。可以是图像的URL、本地文件路径、图像数据的字节流或者Numpy数组。
caption:图像的标题,显示在图像下方。
width:图像的宽度,可以是像素值或者百分比。
use_column_width:如果设置为True,图像的宽度将自动调整为当前列的宽度。
clamp:如果设置为True,当图像宽度大于屏幕宽度时,图像将被剪切以适应屏幕。
channels:图像的颜色通道,默认为RGB。
format:图像的格式,默认为JPEG。
用法举例:
import streamlit as st
# 显示本地图片
image_path = "example.jpg"
st.image(image_path, caption="Example Image", use_column_width=True)
# 显示远程图片
image_url = "https://example.com/example.jpg"
st.image(image_url, caption="Remote Image", use_column_width=True)