Streamlit error after running a upload and download

I have a streamlit app that I am uploading a file to use Pandas to format the data and then I am allowing the user to download the re-formatted file. Before they can upload the file they have to pick the format they are wanting to format to. Then they can download the formatted file. Then they should be able to do the process all over again.

What is happening is that something is hanging out there that is still hanging on to the previous file. How do I get my code to refresh so that they can start the process over again without the error? It’s an error based on the file is trying to use the previous format from my code, it’s not giving me a streamlit-specific error, but when I choose another format, it runs fine. (it’s just an ugly error that I want to get rid of).

I found the issue. The file uploader is keeping the file. So when I have a user click to choose a different format they have to actually close the file that was uploaded last. Is there a way to program python to clear the file uploader after the file has been downloaded so when a user picks a new file format, the old file won’t be in the “file uploader” and they won’t get the ugly error message and they won’t have to close out of the previous file.

I tried this to use st.form, but I’m still getting the error as it’s still holding on to the old file:
with st.form(“my-form”, clear_on_submit=True):
uploaded_file = st.file_uploader(“Upload Redcap data file.”)
submitted = st.form_submit_button(“UPLOAD!”)

any other ideas?

Here is a little example:

File uploader is cleared if either the type_in or type_out is changed:

import streamlit as st
import pandas as pd

left, right = st.columns(2)
type_in = left.selectbox('Convert from', ['csv', 'json', 'parquet'])
type_out = right.selectbox('Convert to', ['csv', 'json', 'parquet'])

# The file uploader will reset automatically when type_in or type_out is changed
file = st.file_uploader(f'Convert file to {type_out}',type=type_in)

if file is not None:
    match type_in:
        case 'csv':
            df = pd.read_csv(file)
        case 'json':
            df = pd.read_json(file)
        case 'parquet':
            df = pd.read_parquet(file)
    
    st.write(df)

    match type_out:
        case 'csv':
            st.download_button('Download as CSV', df.to_csv(), 'data.csv')
        case 'json':
            st.download_button('Download as JSON', df.to_json(), 'data.json')
        case 'parquet':
            st.download_button('Download as Parquet', df.to_parquet(), 'data.parquet')

File uploader is cleared if the type_in is changed (but remains as-is when changing type_out):

Just change the file uploader to:

# The file uploader will reset automatically when type_in is changed
file = st.file_uploader('Upload a file',type=type_in)

This didn’t work for me, I got a message that ‘type_in’ is not defined.

Did you include the code that defines type_in? The last, one-liner is only intended as a modification of the larger example above. It’s not a line of code to use by itself.

No, thank you. Let me try that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.