获取Streamlight中上载文件的原始名称

2024-04-25 21:06:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用streamlit制作了一个基本的可视化应用程序来比较两个数据集,为此我使用了Marc Skov从streamlit gallery制作的以下示例:

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个月前的一个例子,我想知道现在是否有其他方法来实现这一点


Tags: theto数据storeinifvaluestatic
1条回答
网友
1楼 · 发布于 2024-04-25 21:06:27

9 July上进行的提交中,对file_uploader()进行了轻微修改。它现在返回一个dict,其中包含:

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

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

相关问题 更多 >