由于页面重新加载,在 streamlit 中上传的 sing st.file_uploader() 视频丢失了。如何保留它?

Due to page reload, the video uploaded sing st.file_uploader() in streamlit is lost. How to retain it?

我有一个带侧边栏的 streamlit 应用程序,它由一个具有 2 个值的单选按钮组成:A 和 B。A 和 B 都使用 st.file_uploadeer() 将视频上传到应用程序。 但是,当我在 A 和 B 之间切换时,上传的视频由于页面重新加载而丢失。除非用户专门更改上传的文件,否则如何保留上传的视频? 我认为可以使用会话状态或 st.file_uploader() 函数的 on_change() 回调来完成,但我不知道该怎么做。

Screenshot of my app

import streamlit as st

val=st.sidebar.radio('Pick one!',['A','B'])

def upload_video(key):
    #Let's user upload a file from their local computer
    uploaded_file = st.file_uploader(
            'Choose a file', key=key)

    if uploaded_file is not None:
        # gets the uploaded video file in bytes
        bytes_data = uploaded_file.getvalue()
        file_details = {'Filename: ': uploaded_file.name,
                        'Filetype: ': uploaded_file.type,
                        'Filesize: ': uploaded_file.size}
        # st.write('FILE DETAILS: \n', file_details)
        st.write('\n\nUploaded video file')
        # displays the video file
        st.video(uploaded_file)

        # saves the uploaded video file
        with open(uploaded_file.name, "wb") as vid:
            vid.write(bytes_data)
        return uploaded_file.name
# if the user presses "A" in the radio button
if val=="A":
    upload_video("A")

# if the user presses "B" in the radio button
if val=='B':
    upload_video("B")

我添加了代码片段 现在,当我按下边栏中的 B 按钮然后导航回 A 时,上传的视频将被清除。如何解决这个问题? 另外,这是我的第一次堆栈溢出post,对于提问中的任何错误,我深表歉意。任何帮助将不胜感激。

你可以这样写:

import streamlit as st

val = st.sidebar.radio('Pick one!', ['A', 'B'])


def upload_video(key):
    st.write('key : ', key)
    if 'video_path' not in st.session_state:
        st.session_state['video_path'] = None
    # Let's user upload a file from their local computer

    uploaded_file = st.file_uploader(
        'Choose a file', key=key)
    if st.session_state['video_path'] != None:
        agree = st.checkbox(
            'Previous file found! Do you want to use previous video file?')
        if agree:
            vid = open(st.session_state['video_path'], 'rb')
            video_bytes = vid.read()
            st.video(video_bytes)
            return st.session_state['video_path']

    if uploaded_file is not None:
        # gets the uploaded video file in bytes
        bytes_data = uploaded_file.getvalue()
        file_details = {'Filename: ': uploaded_file.name,
                        'Filetype: ': uploaded_file.type,
                        'Filesize: ': uploaded_file.size}
        # st.write('FILE DETAILS: \n', file_details)
        st.session_state['video_path'] = uploaded_file.name
        st.write(st.session_state['video_path'])
        st.write('\n\nUploaded video file')
        # displays the video file
        st.video(uploaded_file)

        # saves the uploaded video file
        with open(uploaded_file.name, "wb") as vid:
            vid.write(bytes_data)
        return uploaded_file.name


# if the user presses "A" in the radio button
if val == "A":
    upload_video("A")

# if the user presses "B" in the radio button
if val == 'B':
    upload_video("B")

在您手动刷新页面之前,视频文件仍然存在。