avatarAndy McDonald

Summary

This article provides instructions on how to use the file_uploader function from Streamlit to upload single and multiple files, as well as how to read the uploaded files.

Abstract

The article begins by explaining the basics of using the file_uploader function from Streamlit to upload single and multiple files. It then goes on to provide examples of how to read the uploaded files, including using pandas to read a single CSV file and concatenating data from multiple CSV files. The article also covers how to restrict the file types that can be uploaded by setting the type parameter.

Opinions

  • The author suggests that the Streamlit file_uploader function is a great way to improve dashboards and recommends adding it to your next Streamlit dashboard.
  • The author encourages readers to subscribe to their content and sign up for their newsletter.
  • The author suggests that readers can get the full Medium experience and support thousands of other writers and themselves by signing up for a membership.
  • The author promotes an AI service that they recommend, which provides the same performance and functions as ChatGPT Plus (GPT-4) but at a more cost-effective price.

Uploading and Reading Files with Streamlit

Allow Your Streamlit Dashboard Users to Upload Their Own Data

Photo by Conny Schneider on Unsplash

Streamlit is an awesome open-source Python library that can be used to create interactive dashboards and applications. This can help simplify the data science workflow. If you are just starting out with Streamlit, at some point you are going to want to allow users to upload their own data. This is easily achieved with the file_uploader function.

Within this short article we are going to cover the essentials of the file_uploader function from Streamlit. We will go through the basics of using it to upload single files, multiple files and how to read the uploaded files.

This article forms part of a series of articles I have created on Streamlit. You can explore the other articles at the links below.

Setting Up Streamlit

The Streamlit library can easily be installed via your Terminal or Command Prompt using pip.

pip install streamlit

After Streamlit has been installed, we can create a new file called app.py . This is where we will be adding our code.

Before we begin coding our app, we first need to import the Streamlit library, which is shortened to st.

import streamlit as st

To run your Streamlit app, all we need to do is type the following into the terminal.

streamlit run app.py

A new browser window / tab will open up with a blank Streamlit app. If you make changes to your app, you can rerun the app by click on the Rerun button that appears at the top of the window.

Uploading a Single File with st.file_uploader

For many applications and use cases, we are likely to have our data stored within a single file. This makes things simple when uploading the data to our Streamlit app.

To upload a file, we can create a new variable called uploaded_file and assign it to st.file_uploader(). The file_uploader function takes several arguments, but we are only interested in applying a label.

st.header('Single File Upload')
uploaded_file = st.file_uploader('Upload a file')

After rerunning our Streamlit app, we will see the file uploader widget.

Streamlit st.file_uploader for a single file. Image by the author.

When we click on Browse Files, we can find the file we want to use and select it.

Once a file has been selected, we will see the file listed below the upload widget.

Streamlit st.file_uploader for a single file after a file has been selected. Image by the author.

Reading a Single CSV File

After the file has been uploaded, we can move on to reading the file's contents. As we are working with a single CSV file, this process is relatively simple.

We import pandas and read the CSV file like so:

import pandas as pd

df = pd.read_csv(uploaded_file)
st.write(df)

When we rerun the Streamlit app, we can see the dataframe ( df )that we created.

Dataframe header after using the Streamlit st.file_uploader for a single file. Image by the author.

Uploading Multiple Files with st.file_uploader

There may be occasions where we have multiple files, such as sensor data that has been written to new files daily. If we have multiple files that we want to load into our Streamlit app, we need to add the argument accept_multiple_files and set it to True.

st.header('Multiple File Upload')
uploaded_files = st.file_uploader('Upload your files',
 accept_multiple_files=True)

When we rerun the Streamlit app, we will see that the wording has changed slightly on the file uploader widget from “file” to “files”.

The st.file_uploader widget after allowing multiple files to be uploaded. Image by the author.

When we then click on Browse Files, we can select multiple files. These will then be listed underneath the file uploader widget.

The st.file_uploader widget after uploading multiple files. Image by the author.

We can check our uploaded_files objects by looping over them.

for f in uploaded_files:
    st.write(f)

Which returns an UploadedFile object with details about the file, including its name, type and size.

Contents of UploadedFile object from st.file_uploader. Image by the author.

Reading Data of Multiple Uploaded CSV Files in Streamlit

We can also use pandas to read the data from the uploaded files.

To do this, we first create an empty list and then loop over each of the files within uploaded_files.

The data can then be read using pd.read_csv() and added onto the data_list.

Once the loop is completed, we can concatenate the data together using pandas concat method.

data_list = []
for f in uploaded_files:
    data = pd.read_csv(f)
    data_list.append(data)

df = pd.concat(data_list)

We can then work with a single dataframe for further analysis or machine learning.

Restricting Files Allowed by Extension Types

If we are planning to publish our dashboard or app to other users, we may want to restrict the file types that the end user can upload.

This is easily achieved by setting the type parameter and passing in a list of file extensions we want to allow.

In the example below, we restrict the types to .png and .pdf. Notice that we do not have to include the period before the file extension.

st.header('Restricted File Extensions')
uploaded_files = st.file_uploader('Upload your files',
 accept_multiple_files=True, type=['png', 'pdf'])

After rerunning the Streamlit app, we will see the file extensions within the uploader widget.

st.file_uploader widget after restricting the file extensions to png and pdf. Image by the author.

When we try to browse for files, we will only be able to select the file types that we explicitly mentioned above. On macOS, the files we cannot select will be shown in greyed-out text, as seen below.

MacOS Finder view showing which files are allowed by st.file_uploader. Image by the author.

Summary

The Streamlit file uploader is a great way to improve your dashboards. Within this short tutorial we have seen how to add the file uploader to our Streamlit dashboard, and set it to accept single or multiple files. Try adding it to your next Streamlit dashboard.

Thanks for reading. Before you go, you should definitely subscribe to my content and get my articles in your inbox. You can do that here! Alternatively, you can sign up for my newsletter to get additional content straight into your inbox for free.

Secondly, you can get the full Medium experience and support thousands of other writers and me by signing up for a membership. It only costs you $5 a month, and you have full access to all of the fantastic Medium articles, as well as the chance to make money with your writing.

If you sign up using my link, you will support me directly with a portion of your fee, and it won’t cost you more. If you do so, thank you so much for your support.

Streamlit
Data Science
Python
Python Programming
Data Analysis
Recommended from ReadMedium