DEV Community

Emma Donery
Emma Donery

Posted on

Introduction to Python Flask Framework

image

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
Enter fullscreen mode Exit fullscreen mode

or

$ sudo apt-get install python3-venv
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment

$ python3 -m venv virtual-environment-name
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

We are now ready to install Flask in this environment

$ pip install Flask
Enter fullscreen mode Exit fullscreen mode

Test Flask installation

from flask import Flask
app = Flask(name)

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

if name == 'main':
app.run()
Enter fullscreen mode Exit fullscreen mode

Running Flask App

$ export FLASK_APP=app.py
$ flask run
Enter fullscreen mode Exit fullscreen mode
#output
Hello World
Enter fullscreen mode Exit fullscreen mode

Reference

Discussion (2)

Collapse
mccurcio profile image
Matt Curcio

Awesome!
Thanks

Collapse
emma_donery profile image
Emma Donery Author

welcome