Flask Tutorial for Beginner: Meme Site (Part 1)
Project description
This will involve creating and deploying a small web application to view memes, funny images, etc. The objective is to master through practice the basics of web programming with the Python language. To do this, we will use the Flask miniframework, so we will not have to reinvent the wheel to create the web. Given the simplicity of our web application, we will do without a database this time, we will instead consume APIs. As for the visual, we are not going to make it complicated, we will just use HTML and Tailwindcss to make a simple and effective interface. Finally, we will use Vercel to deploy our application, in fact vercel turns out to be perfectly suited to our current case, but for more complex python projects, it can quickly become unusable, we will see as a bonus how to also deploy our application on Render.
Project initialization
Firstly, we will create a folder, to which we will give the name of our project.
Next, we’ll open a terminal inside the project folder, and we’ll make sure we have everything we need to get started:
- Check that you have python installed on your computer by typing the command in the terminal:
python3 --version
You should get a response like this :
Python 3.11.6
If not, then you will need to install python on your computer before you can continue.
- Check that you have virtualenv installed on your computer by typing the command in the terminal:
virtualenv --version
You should get a response like this :
virtualenv 20.24.6 from /usr/lib/python3.11/site-packages/virtualenv/__init__.py
If not, you will need to install virtualenv with the python pip package manager on your computer before you can continue with the following command.
pip install virtualenv
If everything is in order, we can continue.
Creating the virtual environment
When initiating a project in Python, it is recommended to create a virtual environment in order to isolate the project dependencies from those of the system. This helps avoid dependency conflicts and facilitates their management if you have several projects on which you work at once.
We will create our virtual python environment, making sure we are in the folder created above for the project, and we will execute the command :
virtualenv env
As soon as the command finishes executing, we should see a folder named env appear in our folder, which represents our virtual environment. You can of course give it whatever name you want when it is created by replacing ‘env’ in the command with whatever you want, as long as it does not cause confusion.
Dependencies installation
Now that we have our virtual environment, we need to install the dependencies that our project will need. To do this, we will activate our virtual environment by running the command :
source env/bin/activate
This being done, we can now install the dependencies that we will need and which are:
- Flask: the python miniframework
- Jinja: the templating tool that will allow us to inject data into our HTML with python
- Requests: the package that will allow us to make HTTP requests
To install all of this, we will run the command:
pip install flask jinja2 requests
To make life easier for those who might pick up the project later, we will generate a file with all the dependencies we have in our virtual environment, so they will not have to install them one by one. We will do this with the following command:
pip freeze > requirements
A text file named requirements.txt should appear, if you open it, you will see listed all the packages that are in your virtual environment.
To install all the file dependencies at once, use the command:
pip install -r requirements.txt
In the remainder of this tutorial, we will discuss the creation of the Flask application, as well as the creation and display of pages.