How to upload a file in Streamlit
This post walks through how to upload files into a Streamlit application and what can be done with them.
1. Simple syntax
2. Upload as DataFrame
3. Upload for predictions
4. Upload multiple files
5. Upload images
How to upload files into a Streamlit application
Giving users the ability to upload files into your Streamlit application is simple, in its simplest form all that is required is:
import streamlit as st
uploaded_file = st.file_uploader("Upload your file here...")
This will produce a well-designed file upload widget in your app which will look like this:

By uploading a file using this widget, it can be used in your application in the same way as any file stored locally. Common use cases for this, that we will cover in this post, are:
- Analysing the file as a Pandas DataFrame
- Using a machine learning model to make a prediction for the uploaded data
Which file types can be uploaded to Streamlit?
According to Streamlitโs documentation, all file types are accepted. This means that all the common filetypes are able to be uploaded, such as:
json
csv
txt
png
jpeg
Upload file as Pandas DataFrame in Streamlit
One common use case for using the file uploader is to be able to analyse the data as part of a DataFrame. This could be done like so:
import streamlit as st
import pandas as pd
uploaded_file = st.file_uploader("Upload your file here...", type=['csv'])
if uploaded_file is not None:
dataframe = pd.read_csv(uploaded_file)
st.write(dataframe)
Upload file to Streamlit for machine learning predictions
Another interesting use case is to use a pre-trained machine learning model to return a prediction for the uploaded data.
Here is a simple example:
import pickle
import streamlit as st
import pandas as pd
# load pre-trained model
trained_model = pickle.load(open(model_filename, 'rb'))
uploaded_file = st.file_uploader("Upload your file here...", type=['csv'])
if uploaded_file is not None:
dataframe = pd.read_csv(uploaded_file)
X = dataframe[["column1","column2"]]
result = trained_model.predict(X)
st.write(f"Your prediction is: {result}")
Upload multiple files into Streamlit
Itโs also possible to upload multiple files, to do this you just need to add the accept_multiple_files
argument.
import streamlit as st
import pandas as pd
uploaded_files = st.file_uploader("Upload your files here...", accept_multiple_files=True)
for uploaded_file in uploaded_files:
dataframe = pd.read_csv(uploaded_file)
st.write(dataframe)
Upload image file in Streamlit
The uploader also works for images and requires no extra code, just a change in the accepted file formats if you wish:
import streamlit as st
uploaded_file = st.file_uploader("Upload your file here...", type=['png', 'jpeg', 'jpg'])
if uploaded_file is not None:
st.image(uploaded_file)
Related articles
Pandas loc vs iloc
Set value for multiple rows in Pandas DataFrame