python - 获取streamlit中上传文件的原名
问题描述
我正在使用 streamlit 制作一个基本的可视化应用程序来比较两个数据集,为此我使用了 Marc Skov 从 streamlit 库中制作的以下示例:
from typing import Dict
import streamlit as st
@st.cache(allow_output_mutation=True)
def get_static_store() -> Dict:
"""This dictionary is initialized once and can be used to store the files uploaded"""
return {}
def main():
"""Run this function to run the app"""
static_store = get_static_store()
st.info(__doc__)
result = st.file_uploader("Upload", type="py")
if result:
# Process you file here
value = result.getvalue()
# And add it to the static_store if not already in
if not value in static_store.values():
static_store[result] = value
else:
static_store.clear() # Hack to clear list if the user clears the cache and reloads the page
st.info("Upload one or more `.py` files.")
if st.button("Clear file list"):
static_store.clear()
if st.checkbox("Show file list?", True):
st.write(list(static_store.keys()))
if st.checkbox("Show content of files?"):
for value in static_store.values():
st.code(value)
main()
这确实有效,但是比较数据集却无法显示它们的名称很奇怪。该代码确实明确表示无法使用此方法获取文件名。但这是 8 个月前的一个例子,我想知道现在是否有其他方法可以做到这一点。
解决方案
在7 月 9 日的提交中,对 进行了轻微修改file_uploader()
。它现在返回一个包含:
- name 键包含上传的文件名
- 数据键包含一个 BytesIO 或 StringIO 对象
因此,您应该能够使用 获取文件名result.name
和使用result.data
.
推荐阅读
- excel - 带有换行符的 Excel 单元格。带有日期值的选项卡
- android - 后台可绘制的xml箭头不起作用
- sql - 如何在语句中获取与 OR 一起使用的索引
- aws-lambda - aws lambda 的 Swagger 架构
- javascript - XMLHttpRequest url 包含 (&)
- file - 从 Step 文件中提取几何图形
- c# - 在 MVC 视图上将列表声明为静态
- python - 如何从数据框中提取值以用于条件格式,同时一次将其应用于某些选择类别或数据条目?
- itext - 根据 HTML 内容设置 iText 7 页面大小
- javascript - 在 Sequelize 和 Node.js 中从 CSV 中删除重复值