Sometime ago we learnt about how to upload files and process the uploaded files in Streamlit. We dived deeper into the Streamlit UploadedFile Class and how it reads it as a file-like Byte IO type.
In this tutorial we will learn about how to save our uploaded file into a directory. This is useful in case you want to save all the uploaded file and use them later.
Let us see how to go about that.
By the end of this post, you will learn about;
- How to upload files in streamlit using st.file_uploader()
- How to save the uploaded file to a directory/folder
To make it quite simple – any uploaded file is treated as a file-like Byte-IO type. Hence you can write that file using python’s standard library IO utils.
This approach makes it quite easier to both process and save our file by just using the write() function in python and the .getbuffer() of the uploaded file.
To save our uploaded file we can write that file in the “wb” format to a location of our choice. We can use os.path to point us to the directory we want to save it . Don’t forget to create the directory you want to save your files to eg tempDir
In order to save the uploaded file with the same name we can use the .name attribute of our uploadedFile class . Putting all of them together
with open(os.path.join("tempDir",uploadedfile.name),"wb") as f: f.write(uploadedfile.getbuffer())
We can then place this code under our uploaded file section as below
image_file = st.file_uploader("Upload An Image",type=['png','jpeg','jpg']) if image_file is not None: file_details = {"FileName":image_file.name,"FileType":image_file.type} st.write(file_details) img = load_image(image_file) st.image(img,height=250,width=250)with open(os.path.join("tempDir",image_file.name),"wb") as f:
f.write(image_file.getbuffer())
st.success("Saved File")
We can also convert it into a function so that it is reusable everywhere and anytime.
def save_uploadedfile(uploadedfile):
with open(os.path.join("tempDir",uploadedfile.name),"wb") as f:
f.write(uploadedfile.getbuffer())
return st.success("Saved File:{} to tempDir".format(uploadedfile.name))
Now we can easily use it like this
datafile = st.file_uploader("Upload CSV",type=['csv']) if datafile is not None: file_details = {"FileName":datafile.name,"FileType":datafile.type} df = pd.read_csv(datafile) st.dataframe(df) save_uploadedfile(datafile)
To conclude we have seen how to save our uploaded file into a directory in our Streamlit app. Streamlit is amazing. You can also check out the video tutorial and other courses here.
Thanks For Your Time
Jesus Saves
By.Jesse E.Agbe(JCharis)
Hello Jesse ,
so this is the error message i am getting when i try to execute the second block of code on your blog :
NameError: name ‘load_image’ is not defined
PS: Was load image defined anywhere ?
Hello Benniah,
The load_image function is a separate function (From the Working with Uploads Tutorial)
But you can also add it
# Image
from PIL import Image
@st.cache
def load_image(image_file):
img = Image.open(image_file)
return img
Hope this helps
Thanks a lot Jesse !
I found a workaround for multiple file uploads by using a Loop:
import streamlit as st
import os
def save_uploadedfile(uploadedfile):
with open(os.path.join(“Data”, uploadedfile.name), “wb”) as f:
f.write(uploadedfile.getbuffer())
return st.success(“Saved File:{} to Data”.format(uploadedfile.name))
st.title(” PDF File upload”)
st.text(” A simple way to upload files directly into a directory”)
uploadedfiles = st.file_uploader(“Upload PDF”, type=[‘pdf’], accept_multiple_files=True)
for file in uploadedfiles:
if uploadedfiles is not None:
save_uploadedfile(file)
Great work Benniah
Bro! After a attempting a model training of an ml algo. Suddenly the streamlit app stops running. But, the background process of training is going on terminal. So, if we refresh again the training starts from beginning.
Is there any way to store the trained data into a csv file after the training the data gets completed.
Hello Tirumalesh, yes you can do that – you can also use featurestore for this. Moreover it is usually recommended to deploy the pre-trained model (batch ml) for the app to increase performance instead of training on the go (online ml).
how to save .txt and pdf file, please give complete code
Hello Saurabh, the same approach will work for .txt and pdf files.
Hope this helps