Installing and importing packages
To make our web application work properly, we need the following Python packages:
- streamlit: This is the core – that is, the framework that makes the magic happen
- textblob: This is a nice package for basic sentiment analysis and some other basic NLP tasks (NLP is how computers understand human language, its meaning, its syntax, and so on)
- spacy: This is quite an advanced package; it’s state of the art and can be used for almost any NLP task
- neattext: A very simple package for text cleaning
- matplotlib: Python’s most famous package for plotting graphs, diagrams, and so on
- wordcloud: A package dedicated to nice word cloud creation and visualizations
We can install all these packages in our virtual environment (so we must already be inside the virtual environment) by typing the following unique instruction:
pipenv install streamlit textblob spacy neattext matplotlib wordcloud
Please note that this operation can take a few minutes to run:
Figure 4.4: Package installation
If you check the Pipfile from Sublime Text, you will see that all the packages have been installed correctly, as reported in Figure 4.5:
Figure 4.5: Pipfile with the installed packages
spaCy is a very powerful package for NLP and requires a dedicated language model for each language we want to manage. So, before we start coding, we need to download the English language model. Let’s write the following instruction in our terminal:
pipenv run python –m spacy download en_core_web_sm
This instruction will download the en_core_web_sm English language model, whose size is 12.8 MB. The file’s name is quite self-explanatory: en means English, core_web means that the model has been trained on a dataset containing text coming from the web, and sm stands for small (if you want to get an idea of all languages, models, and their size, you can check spaCy’s official website):
Figure 4.6: Downloading the spaCy language model
Now, we have everything we need to start coding. There’s just one thing missing: the Python file! Without it, we cannot code. Let’s create it by typing a simple instruction:
touch app.py
Here’s the output:
Figure 4.7: The app.py file
So far, everything is ready: our environment is OK, our packages have been installed, and our empty app.py file is there.
We are now ready to start writing the code for our first working web application.
The first step is to open the app.py file in Sublime Text and import all the libraries we have installed so that we can write the code, as shown in Figure 4.8:
Figure 4.8: Importing the necessary libraries
The preceding code imports streamlit with the st alias and imports all NLP packages (textblob, spacy, and neattext) and all the visualization libraries. Please note that for matplotlib, we are using the Agg engine since, with Streamlit, it works better than the default one.
After importing, we are finally ready to write all the code for our first real web application. Let’s do it right now!