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!”)
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)
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.