Understanding the customized features of the file uploader
Uploading a file is a simple task. There are several ways to do it, and these different approaches have different effects on the so-called user experience: the way users perceive the application itself. Moreover, a better-implemented uploading feature can speed up the entire application, making things easier for the users. Let’s imagine that we want to upload a file containing text. It could be a .txt file, so a plain text file, but also a .docx file, a Microsoft Word file, or even a .pdf file. One approach is to ask the customer, what kind of file do you need to upload (.txtx, .docx, .pdf)? If the user replies .txt, the application will launch the file_uploader widget customized for this file format; if the answer is .docx, the file_uploader widget customized for Microsoft Word will be executed, and so on. This kind of approach works perfectly, but it’s a little bit too complex.
What if the user updated a file and the web application recognized its type automatically without human intervention?
Let’s learn how to implement this feature according to both approaches, one requiring information from the user and another that’s completely automated.
Creating a new virtual environment
First, we’ll create a new virtual environment dedicated to this chapter. So, follow these steps:
- Create a new folder and call it FileUploader.
- Next, move to the newly created folder and, once inside it, as usual, write pipenv shell to create the virtual environment.
The instructions are shown in Figure 12.1:
Figure 12.1: Creating a new virtual environment dedicated to this chapter
- After that, the only library we need is Streamlit, so let’s type the following:
pipenv install streamlit
After a few seconds, our environment will be equipped with the latest Streamlit version that’s available.
- As usual, before we start coding, we have to create an empty Python file, which is a very easy operation. We can do this by writing the following instruction:
touch app.py
Here, app.py is the name of our file.
We can open the app.py file with our favorite IDE, such as Sublime Text.