Introduction to Python Flask Framework

Flask is a popular lightweight micro-framework based on Werkzeug, Jinja2 for developing web applications authored by Armin Ronacher.
Flask Framework depends on two external libraries: The Jinja2 template and Werkzeug WSGI toolkit.
  • Werkzeug is a WSGI utility library. WSGI is a specification for a universal interface between the web server and the web applications.
  • Werkzeug is a WSGI toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it
  • Jinja2 is a popular templating engine for Python.
  • A web framework is an architecture containing tools, libraries, and functionalities suitable to build and maintain massive web projects using a fast and efficient approach.
    Why is flask referred to as a microframework
    It aims to keep the core of an application simple yet extensible.
    NB: Flask has no native support for accessing databases, validating web forms, authenticating users, or other high-level tasks. Instead, Flask supports the extensions to add such functionality to the application
    Popular Flask extensions
    Extensions are extra packages that add functionality to a Flask application.
  • Flask Mail − provides SMTP interface to Flask application
  • Flask WTF − adds rendering and validation of WTForms
  • Flask SQLAlchemy − adds SQLAlchemy support to Flask application
  • Flask Sijax − Interface for Sijax - Python/jQuery library that makes AJAX easy to use in web applications
  • Flask-Login − provides user session management
  • Features of Flask
  • Built-in development server, fast debugger.
  • Integrated support for unit testing.
  • RESTful request dispatching.
  • Jinja2 Templating.
  • WSGI compliance
  • URL routing
  • Sessions
  • Support for secure cookies.
  • Lightweight and modular design that allows for a flexible framework
  • Installation
    The most convenient way to do that is to use a virtual environment.
    Virtual Environments
    A virtual environment is a copy of the Python interpreter into which you can install packages privately, without affecting the global Python interpreter installed in your system.
    Advantages of virtual environment
  • Prevent package clutter and version conflicts in the system’s Python interpreter.
  • Ensures that applications have access only to the packages that they use, while the global interpreter remains neat and clean and serves only as a source from which more virtual environments can be created.
  • Can be created and managed without administrator rights, unlike the system-wide Python interpreter
  • Setting up a Virtual Environment with Python 3
    Install
    $ sudo apt-get install virtualenv
    or
    $ sudo apt-get install python3-venv
    Create a virtual environment
    $ python3 -m venv virtual-environment-name
    NB: This command needs administrator privileges. Add sudo before pip on Linux/Mac OS. If you are on Windows, log in as Administrator.
    Creating the Application Directory
    $ mkdir flask_app
    $ cd flask_app
    #create a vitual environment in this folder
    $ python3 -m venv venv
    After the command completes, you will have a subdirectory with the name venv inside flask_app, with a brand-new virtual environment that contains a Python interpreter for exclusive use by this project.
    Working with a Virtual Environment
    When you want to start using a virtual environment, you have to “activate” it.
    $ source venv/bin/activate # Linux or macOS
    $ venv\Scripts\activate # Windows
    We are now ready to install Flask in this environment
    $ pip install Flask
    Test Flask installation
    from flask import Flask
    app = Flask(name)
    
    @app.route('/')
    def hello_world():
    return 'Hello World’
    
    if name == 'main':
    app.run()
    Running Flask App
    $ export FLASK_APP=app.py
    $ flask run
    #output
    Hello World

    23

    This website collects cookies to deliver better user experience

    Introduction to Python Flask Framework