在streamlit的file_uploader中使用自己的文件扩展名
Streamlit 的 file_uploader 小部件让您可以通过类型
选项:
类型(str 或 str 列表或 None)
允许的扩展名数组。 ['png', 'jpg'] 默认为 None,即 表示允许所有扩展。
与常见的文件扩展名相比,我正在使用非标准的文件扩展名,例如 .details.txt
。如果配置了 type='txt
,则可以选择这些文件:
uploaded_files = st.file_uploader(
"Choose file(s)",
type=['txt'],
accept_multiple_files=True,
)
但是,这也允许用户提供我想要阻止的其他 txt
文件。用户应该只能上传 .details.txt
文件,而不能上传 .report.txt
之类的文件。
指定 type='.details.txt'
或 type='details.txt'
并不能解决问题。在这些情况下,我无法选择任何文件。
以下代码片段可用于重现所描述的行为:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import streamlit as st
uploaded_files = st.file_uploader(
"Choose file(s)",
type=['txt'],
# type=['.txt'],
# type=['details.txt'],
# type=['.details.txt'],
accept_multiple_files=True,
)
此外,您需要在文件系统上创建一个 something.txt
和一个 something.details.txt
文件。
有没有办法指定自己的非标准文件类型?
Streamlit's file_uploader widget let's you define the allowed file extensions via the type
option:
type (str or list of str or None)
Array of allowed extensions. ['png', 'jpg'] The default is None, which
means all extensions are allowed.
In contrast to the common file extensions I am working with a non-standard one like .details.txt
. These files can be selected if type='txt
is configured:
uploaded_files = st.file_uploader(
"Choose file(s)",
type=['txt'],
accept_multiple_files=True,
)
However, this also allows the user to provide other txt
files which I want to prevent. The user should be able to upload .details.txt
files only, but not files like .report.txt
.
Specifying type='.details.txt'
or type='details.txt'
did not do the trick. In these cases I was not able to select any file.
The following snippet can be used to reproduce the behavior described:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import streamlit as st
uploaded_files = st.file_uploader(
"Choose file(s)",
type=['txt'],
# type=['.txt'],
# type=['details.txt'],
# type=['.details.txt'],
accept_multiple_files=True,
)
In addition, you would need to create a something.txt
and a something.details.txt
file on your filesystem.
Is there a way to specify own non-standard file types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下内容适用于 Streamlit 版本 1.6.0。
The following works for me in Streamlit, version 1.6.0.