How To Install and Use Flask on Centos 7

In this section of the Linux Tutorials, we want to teach you How To Install and Use Flask on Centos 7.

Flask is a web framework that provides you with tools, libraries, and technologies that allow you to build a web application. This web application can be some web pages, a blog, and a wiki or go as big as a web-based calendar application or a commercial website.

Install and Use Flask on Centos 7

You need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article about the Initial Server Setup with Centos 7.

The easiest way to install Flask on your Centos 7 is to use the YUM package manager. But, it’s recommended to use the Python virtual environment.

In this way, you can have multiple different Flask environments on a single computer and install a specific version of a module on a per-project basis without worrying that it will affect your other Flask installations. If you install Flask into the global environment then you can install only one Flask version on your computer.

To install Flask on a Python virtual environment follow the steps below.

Install Python 3 and Create a Virtual environment on Centos 7

Centos 7 comes with Python 2.7.5. The SCL (Software Collections) allows you to install the Python 3.x instead of the default Python v2.7.5

First, update your local package index with the following command:

sudo yum update

Then, enable the SCL by installing the Centos SCL release file which is included in the Centos extras repository with the following command:

sudo yum install centos-release-scl

When you have enabled the repository, install Python 3.6 with the following command:

sudo yum install rh-python36

Now you can start to create a virtual environment.

First, switch to the directory where you want to store your Python 3 virtual environments.

Then, you need to launch a new shell instance to access Python 3. To do this run the following command:

scl enable rh-python36 bash

Now you need to create a new directory for your Flask app and switch to it on Centos 7 with the following commands:

$mkdir my_flask_app
$cd my_flask_app

To create a new virtual environment run the command below:

python3 -m venv venv

This command will create a directory called venv which contains a copy of the Python binary, the pip package manager, the standard Python library, and other supporting files. You can use any name you want for the virtual environment.

Now activate the virtual environment with the following command:

source venv/bin/activate

You will see that your shell prompts will change to the name of the virtual environment that you are using.

Now you can start to install Flask.

Installing Flask

When you have activated the virtual environment, you can use pip to install the Flask on Centos 7:

(venv) $ pip install Flask

Note: From the virtual environment, you can use the command pip instead of pip3 and python instead of python3.

Verify your installation with the following command:

(venv) $ python -m Flask --version

In your output you will see:

Output
Python 3.6.12
Flask 2.0.2
Werkzeug 2.0.2

The version of Flask may be different from here.

At this point, we will show you how to use Flask by creating a minimal Flask app.

Using Flask

At this step, we will show you how to create a simple Hello World application that will display the text “Hello World!”.

First, create a Flask file with your favorite text editor, here we use vi:

(venv) $ sudo vi ~/my_flask_app/hello.py

Then add the following content to the file:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'

When you are finished save and close the file.

Here you need to tell Flask how to load the application on Centos 7. To do this, you can use the following command:

(venv) $ export FLASK_APP=hello.py

Then run the Flask:

(venv) $ flask run

In your output you will see:

Output
Serving Flask app "hello.py"
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: off
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Open your web browser and type http://127.0.0.1:5000, you will see the hello world message.

To stop the development server type ctrl+c.

When you are done with your work, you can deactivate your virtual environment by typing:

(venv) $ deactivate

Conclusion

At this point, you learn to create a Python virtual environment and install Flask on your Centos 7.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!