Streamlit Python

Posted onby admin

One of the cool things about building web applications is the ability to either upload or download files from the web app.

In this tutorial we will be exploring streamlit file upload feature. With this feature you can upload any type of files and use them in your app. To work with the file uploads you will have to use the st.file_uploader() function.

Now, with an app framework called Streamlit, data scientists can build machine learning tools right from the beginning of the project. Utilising Streamlit, data scientists can visualise their code output while analysing data. They can build ML tools that can be utilised to analyse data through clicks and sliding bars.

Have you ever wanted to build a web app for your data science project? But perhaps did not because of the extensive time needed to code the web app or intimi. A bi-directional Streamlit Component varies slightly from a pure Python library in that it must contain pre-compiled frontend code. This is how base Streamlit works as well; when you pip install streamlit, you are getting a Python library where the HTML and frontend code contained within it. Streamlit; as their official website tells, is the 'fastest way to build and share data apps.' Now as you are getting really excited; let me give you some more good news. Yes, you can use streamlit just as a python library because instead of being an app running software; streamlit data apps can be created just using the streamlit library. The apps run from your terminal on saying 'streamlit run' where the is just a normal python script where you code the app. Developing Data Web App Using Streamlit Table of Contents. Data Science is one of the most trending search topics on google these days. Because of its high demand. About Streamlit. S treamlit is an open-source python library that is useful to create and share data web apps.

Let us see how the st.file_uploader() functions works.

First of all let us explore the various features of the st.file_uploader()

  • Ability to specify the type of file you want to allow(type=[]): This feature is quite useful as it gives you a form of security out of the box with little code. Hence unspecified file types are disallowed and restricted when the user uploads a file.
  • Ability to receive multiple files: (accept_multiple_files=True): With this feature you can accept multiple file as well as even select multiple files and upload them.
  • Ability to specify the limit of files to upload : By default the maximum limit is 200mb but Streamlit allows you to change the limit.

These are the commonest features out of the box but there are some other nice features that needs explanation.

Let us see how the file_uploader() works.

The UploadedFile Class

Any file uploaded will be seen under the UploadedFile Class which in this context is a subclass of ByteIO . This is ‘file-like ‘ so in case you are working with it – you are to treat it like how you will treat a file. Therefore you will have to use the right file processing library or package to process or read your uploaded file.

Streamlit Python Version

Streamlit Python

Hence if it is an image you will have to use PIL or OpenCV ,etc.

Filetype and Their Respective Packages

Image(PNG,JPEG,JPG): PIL,OpenCV,Scikit-Image

CSV: Pandas, DataTable,CSV

PDF: pyPDF2,pdfplumber,etc

Docx: Docx2Txt,python-docx,textract,

Getting the FIlename and Filesize

Streamlit Python

With the UploadedFile Class you can get the original filename, filesize and filetype. Doing a dir(uploaded_file) will show all the attributes and methods of this class. Hence you can get the original details.

For the original filename,filesize and filetype you can use the code below

This is the same thing that shows just below the drag and drop section when you upload a file.

Python Streamlit Tutorial

Reading Each File Type

To make it easy I have built a simple file_upload app and you can check the code below

You can also check out the video tutorial below

Thanks For Your Time

Jesus Saves

Dash Plotly

By Jesse E.Agbe(JCharis)