I received this error: AxiosError: Request failed with status code 413 for an application built in Streamlit. I noticed that the error is from the server, and I have increased the limit in nginx.
However, I would like to capture this error and display a message to the user saying that the file size is too large. I understand that the error is received from the server and rendered in Streamlit.
So how can I retrieve that specific error? Where does Streamlit handle server errors exactly? I hope you can help me.
Instead of capturing the error, here’s an implementation that conditionally display messages using if/else statement:
If no file is uploaded a warning message is displayed via st.warning() to remind the user to upload a file
If the file exceeds the 1 MB limit then it displays an error message via st.error()
Else the success message is displayed via st.success()
import streamlit as st
MAX_FILE_SIZE = 1024 * 1024 # Maximum file size in bytes
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None and len(uploaded_file.read()) > MAX_FILE_SIZE:
st.error("Error: File size is too large.")
elif uploaded_file is None:
st.warning("Please upload a file!")
else:
st.success('File successfully uploaded and processed.')
Thank you for the response and the idea. It could be a good approach that we will consider, but in this case, the file will be uploaded to the server, and I wanted to avoid uploading very large files. I will create a function that will delete any file in the end, but I’m trying to avoid their upload to the server. Maybe I didn’t fully explain in the first message. I looked closer now and realized that I could handle the issue differently. I could manipulate the st.file_uploader to have a limit of, for example, 20MB. I would need to add the following lines to the config.toml file in the .streamlit folder:
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.