How to Install and Use Flask on Debian 11/10

Flask is a microweb framework written in Python that is widely used for building web applications. It is a lightweight framework that does not require particular tools or libraries to be installed. Flask provides developers with the ability to add functionality to their applications through the use of libraries and modules.

In this tutorial, we will show you how to install Flask on Debian 11. Debian 11, also known as “Bullseye,” is the latest stable release of the Debian operating system. It is a free and open-source operating system that is widely used on servers and other systems.

Prerequisites

Before you start, make sure that you have Python and pip installed on your system. You can check if you have Python installed by running the following command:

python3 --version 

If you do not have Python installed, you can install it by running the following command:

sudo apt update 
sudo apt install python3 python3-pip python3-venv 

To check if you have pip installed, run the following command:

python3 --version 
pip3 --version 

Step 1: Installing Flask

Once you have Python and pip installed, you can install Flask system-wide by running the following command: `pip3 install flask`

But I recommended creating a virtual environment for your Flask application.

  1. Create a directory for your Flask application:
    mkdir flask-app && cd flask-app 
    
  2. Create the Python virtual environment.
    python3 -m venv venv 
    
  3. Activate the virtual environment:
    source venv/bin/activate 
    
  4. Install the Python Flask module within the virtual environment.
    pip3 install flask 
    

This will install the latest version of Flask and all the required dependencies under the virtual environment.

Step 2: Creating a Flask Application

Now that Flask is installed, you can create your first Flask application. Switch to the newly created application directory:

cd flask-app 

Next, create a file called `app.py` and add the following code to it:

from flask import Flask

app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()

This code creates a simple Flask app that listens for incoming HTTP requests on the root path (/) and returns the message “Hello, World!”.

Step 3: Run Your Flask Application

To run the application, execute the following command:

flask run 

This will start the Flask development server and you will see the following output:

Output:
* Serving Flask app "app" * 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)

Now, open your web browser and go to “http://127.0.0.1:5000/” to view your Flask app. You should see the message “Hello, World!” displayed on the page.

Conclusion

In this tutorial, you learned how to install and use Flask on Debian 11. You also learned how to create a simple Flask app and run it using the Flask development server. Flask is a powerful and easy-to-use web framework that makes it easy to build web applications with Python.

The post How to Install and Use Flask on Debian 11/10 appeared first on TecAdmin.

Application Debian Flask Python Framework TecAdmin