How to add a file uploader widget in Streamlit

In this recipe, we will learn how to add a file uploader widget in Streamlit. We will also take a look at a streamlit app that consists of a file uploader widget.

Recipe Objective: How to add a file uploader button in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add a file uploader that enables the user to upload files to your Streamlit app using "st.file_uploader".

  Syntax: st.file_uploader(label, type, accept_multiple_files, key, help, on_change, args, kwargs)
  Parameters:
   label -> A simple label that explains what this input widget is for
   type -> It consists of an array of allowed extensions. ['txt','png','jpg'] The default is None, which implies all extensions are allowed.
   accept_multiple_files -> If it is set True, it allows the user to upload multiple files at the same time. A list of files will be returned in this case.
   key -> An optional string or int to be used as a unique key for this widget. If omitted, a key will be generated for the widget based on its content. Multiple widgets of same types cannot share the same key.
   help -> An optional tooltip that get displayed next to the file uploader.
   on_change -> An optional callback invoked when there is a change in this file uploader's value
   args -> An optional tuple of args that can be passed to the callback
   kwargs -> An optional dictionary of kwargs that can be passed to the callback

Code:

#importing required libraries
import streamlit as st
from io import StringIO

#adding a file uploader
file = st.file_uploader("Please choose a file")
if file is not None:
#To read file as bytes:
bytes_data = file.getvalue()
st.write(bytes_data)

#To convert to a string based IO:
stringio = StringIO(file.getvalue().decode("utf-8"))
st.write(stringio)

#To read file as string:
string_data = stringio.read()
st.write(string_data)

#Can be used wherever a "file-like" object is accepted:
df= pd.read_csv(file)
st.write(df)

#adding a file uploader to accept multiple CSV files
uploaded_files = st.file_uploader("Please choose a CSV file", accept_multiple_files=True)
for file in uploaded_files:
bytes_data = file.read()
st.write("File uploaded:", file.name)
st.write(bytes_data)

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.