23
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 toolkit, which implements requests, response objects, and other utility functions. This enables building a web framework on top of it
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.
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
Extensions are extra packages that add functionality to a Flask application.
The most convenient way to do that is to use a virtual environment.
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.
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.
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
from flask import Flask
app = Flask(name)
@app.route('/')
def hello_world():
return 'Hello World’
if name == 'main':
app.run()
$ export FLASK_APP=app.py
$ flask run
#output
Hello World
23