Tuesday, December 10, 2024
How ToLinuxProgrammingTech/Web

How to Install Flask on Ubuntu

Flask is a free and open source micro web framework for Python designed to help developers build secure, scalable and maintainable web applications. It based on the Werkzeug, Jinja, MarkupSafe. It was originally created in 2010 and has since powered some of the largest websites on the internet.

Unlike Django, by default Flask doesn’t include ORM, form validation or any other functionalities provided by third party libraries. Flask is built with extensions in mind, which are Python packages that add functionality to a Flask application.

There are different methods to install Flask, depending on your needs. It can be installed system-wide or in a Python virtual environment using pip.

The main purpose of Python virtual environments is to create an isolated environment for different Python projects. In the way 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. But If you install Flask into the global environment then you can install only one Flask version on your System.

Dependencies

These distributions will be installed automatically when installing Flask.

  • Werkzeug implements WSGI, the standard Python interface between applications and servers.
  • Jinja is a template language that renders the pages your application serves.
  • MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
  • ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
  • Click is a framework for writing command line applications. It provides the flask command and allows adding custom management commands.

You may like also : What is Jinja

Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by typing:

python3 -V

output

Python 3.6.6

Create a virtual environment is to use the venv module. To install the python3-venv package that provides the venv module by this command:

sudo apt install python3-venv

Now You can create a virtual environment for our Flask application.

Navigate to the directory where you would like to store your Python 3 virtual environments. It can be any location where your user has read and write permissions.

Create a new directory for your Flask application and navigate into it:

mkdir my_flask_appcd my_flask_app

Create your new virtual environment:

python3 -m venv venv

The command above creates 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  you need to activate it by running the activate script:

source venv/bin/activate

Once activated, the virtual environment’s bin directory will be added at the beginning of the $PATH variable. Also your shell’s prompt will change and it will show the name of the virtual environment you’re currently using. In our case that is venv:

Step 3. Install Flask

Now that the virtual environment is activated, you can use the Python package manager pip to install Flask:

pip install Flask

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

Verify the installation

python -m Flask --version

How To use Flask

We will create a simple hello world application which will just display the text “Hello World!”.

Open your text editor or PyCharm and create the following file:

~/my_flask_app/hello.py
from flask import Flask
app = Flask(__name__)

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

Save the file as hello.py

We’ll use the flask command to run the application but before that we need to tell Flask how to load the application by specifying the FLASK_APP environment variable:

export FLASK_APP=helloflask run

The command above will launch the development builtin server.

Output

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

If you use Flask with virtual machine and you want to access Flask development server then you cnn make the server publicly available by appending --host=0.0.0.0 to the flask run command.

Open http://127.0.0.1:5000 in your web browser and you will be presented with the “Hello World!” message.

To stop the development server type CTRL-C in your terminal.

After finishing your work, deactivate the environment.

deactivate

Visit the Flask documentation page and learn how to develop your first Flask app. If you have any question, please leave a comment below.


You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

3 thoughts on “How to Install Flask on Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *