How to Install Flask and Create Web Applications?

By | June 13, 2022
How to Install Flask and Create Web Applications?

If you are a beginner in web application development, you may want to start out with Python’s flask because it is easy to follow. In Python, there are many web application frameworks and Flask provides the quickest and easiest way to develop your applications. Flask is a microframework that offers many features and tools, making it easy to apply while being suitable for complex projects. In this article, we will show you how to install flask and build a web application.

What Do You Need to Install Flask?

In the Flask Micro Web framework, you will find many important tools, libraries, and technologies that will help you build web pages as well as e-commerce applications. However, when beginner developers are learning backend web development, they often have a hard time selecting the suitable framework option. Flask’s simplicity makes it easy for beginners to learn. We will discuss how to install Flask on your virtual environment shortly. Here are some elements to consider before moving on:

  • Install Python 2.7 or Python 3.5 or any later version of Python
  • You will need CLI with admin privileges.

If you have installed Python and have the command-line interface, then let’s look at the steps of installing Flask on your computer. But we have also discussed above how you can install the virtual environment and activate it before installing Flask. So, let’s look at that first.

Step 1: Install Python

We have already stated above that you need Python 2.7 or 3.5 to install Flask. Hence, you need to check out the version of Python your system is already running. If the python version is incompatible with Flask then it will have conflicts with the libraries. If you are using Python 2, you need to install the virtualenv module because Python 2 does not come with that by default. But Python 3 users can skip the virtualenv installation. Here we have demonstrated how you can install the ‘virtualenv’ module on your Linux, Mac, and Windows.

To Install the Virtualenv Module on Linux

Linux package manager has virtualenv and you can easily install that by applying a few commands. If you are running Ubuntu or Debian, you can run this command on your terminal:

sudo apt install python-virtualenv

If you are using CentOS or Fedora, or Red Hat, you can enter this command on your terminal:

sudo yum install python-virtualenv

And the command will install the existing virtualenv package on your Linux system.

To Install virtualenv on Mac OS and Windows

To install virtualenv on Mac, open your terminal and run the following command to install the package:

sudo python2 -m pip install virtualenv

To install it on Windows

Windows does not have a terminal and to install virtualenv, you will have to run a simple command on its Command-line prompt that has administrator access. Run this command on the Command line.

py -2 -m pip install virtualenv

Once you learn how to install Python, you need to create a virtual environment. The following steps will teach you how to set up a virtual environment on various operating systems.

Step 2: Formulate a Virtual Environment

To install Flask, you will create an individual directory with a command: mkdir <project name>

To move into the above directory, use this command:

cd <project name>

A virtual environment of Flask can be created in that directory. As soon as you design the background, you will find a unique folder in the directory with the same name as your environment. To create an environment for Python 3 on a Linux or Mac computer, you must give the venv module a name with the following command:

python3 -m venv <name of environment>

If your system is running Python 2, then you can apply the virtualenv module and create an environment and give it a name.

python -m virtualenv <name of environment>

To form an environment in Windows, for Python 3, design and define a virtual environment with the following command:

py -3 -m venv <name of environment>

As to python 2, design the background by the virtualenv module:

py -2 -m virtualenv <name of environment>

Now, insert the folder construction utilising the following command:

dir *<project name>*

Your project directly will show you the recently created virtual environment on Python and your next step will be to activate the environment. To install Flask, you need to activate the environment first.

Step 3: Initiate the Environment

Initiate the environment with the following commands for different operating systems. The newly activated environment name will appear in the CLI following activation.

To activate the environment in Mac OS and Linux:

. <name of environment>/bin/activate

To activate the environment in Windows:

<name of environment>Scriptsactivate

After the activation is complete, you can Install Flask on your system. The Flask installation process comes with a few steps. Follow the section below to learn about them.

Step 4: Establish the Flask

The first step of installing flask includes creating a directory for Flask and you can use the following command for that:

$ mkdir flask_website

Change the directory to the Flask_website directory:

$ cd flask_website

We have already stored the tools of the environment in the env folder but if you have missed that step, let’s do it now:

$ python3 -m venv env

Now, install Flask by running this command:

(env)$ pip install flask

Your Flask will be installed on its own by all its dependencies. When it’s done, the next step will be to test the development environment.

Step 5: Analysis of the Development with a Flask Application

  • Create a new file in the Flask project folder and rename it with hello.py and edit that file with a text editor.
  • While you are editing the file, you should attach the resulting codes so that it becomes a statement that reprints “Hello world!”:

from flask import Flask

app = Flask(__name__)

@app.route(‘/’)

def hello_world():

return ‘Hello world!’

  • Save the new file and exit the editor.
  • And navigate where the project folder is with the cd command from the console. Here you will have to configure the FLASK_APP environment variable.
    Setting the environment variable for flask requires a command which is different for Linux or Mac and Windows systems.
  • For Linux and Mac, apply the following command:

export FLASK_APP=hello.py

  • For Windows system, perform the following command:

setx FLASK_APP “hello.py”

  • Now if you operate the Flask software, you can run the following command:

flask run

The output will show you a verification report and also will show you an address. Copy that address and search for it in the browser and you will see that your Flask project is running.

Conclusion

You can also structure your web application using a few commands and using the requirement.txt file. The “requirement.txt” file will be there in the app folder and will help you define the flexibility of your Flask application. A flexible flax application package will be suitable for importing in any part of the web application. And on the other hand, the run.py file works as the pointer to Flask that informs Flask where the application is and helps it run the web application. Additionally, the requirement.txt file stores all of the project’s packages. The requirement.txt file can be created by running (env)$ pip freeze > requirements.txt. The requirement file will list all the packages used in Flask but there won’t be more than seven packages.

So, that’s basically how you create an environment for Flask and install it and operate it on your computer. To know more about this or for additional questions, you can drop us a comment below with your queries.

Leave a Reply

Your email address will not be published.