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.

Stephen Allwright
Stephen Allwright
๐Ÿ“‹

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:

Streamlit upload file widget

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
๐Ÿ‘‡
Letโ€™s look at some common use cases for using the upload file function

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}")
๐Ÿ’ก
This could be helpful when you want to test your model on a set of internal users before it goes into production.

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)
๐Ÿ’ก
This will display the uploaded image within the app, but it could just as simply be analysed or used in a machine learning model as with the text examples.

Pandas loc vs iloc
Set value for multiple rows in Pandas DataFrame

References

Streamlit documentation

Streamlit

Stephen Allwright Twitter

I'm a Data Scientist currently working for Oda, an online grocery retailer, in Oslo, Norway. These posts are my way of sharing some of the tips and tricks I've picked up along the way.