获取流式上传文件的原始名称

问题描述

我正在使用streamlit制作一个基本的可视化应用程序来比较两个数据集,为此,我使用的是streamlit画廊中Marc Skov制作的以下示例:

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个月前的示例,我想知道是否还有另一种方法可以完成此任务。

解决方法

在对9 July的提交中,对file_uploader()进行了一些修改。现在,它返回一个包含以下内容的字典:

  • 名称键包含上载的文件名
  • 数据键包含BytesIO或StringIO对象

因此,您应该能够使用result.name获取文件名,并使用result.data获取数据。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...