Flask is a small web framework. You pick the pieces you want and it gets out of the way, which is why it is good for an API or a small site.

On this page is a complete small app: one route that renders a template, one that returns json, and the line that starts the server.

Flask’s unique selling proposition lies in its minimalistic and modular nature. You can integrate third-party modules based on the requirements of your project, giving you complete control over the architecture of your application.

Understanding Web Frameworks

A web framework is a set of libraries that handles the boring parts: routing, sessions, form data and the database.

Dive into Flask

What Makes Flask Special?

Flask is termed a “microframework” primarily because it focuses on providing only the essentials, such as:

  • Routing: Directing users to specific pages.
  • Request handling: Managing data being sent by users.
  • Sessions: Storing user-specific data.

There is no project layout to learn and no dependencies you did not ask for, which makes it easy to start with.

Some good things of Flask include:

  • Built-in development server and debugger for effective debugging.
  • Compatibility with Jinja2 templates and adherence to WSGI standards.
  • An extensive library of plugins for enhancing its features.
  • Ideal platform for crafting robust backend solutions.

Installing Flask

Install it globally or in a virtual environment. If you use PyCharm you can add it as a package in the project settings.

To install it globally:

pip install Flask --user

Check it worked by importing it in the Python shell:

$ python
Python 3.7.7 (default, Mar 13 2020, 10:23:39)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
>>> from flask import Flask
>>>

Import Flask Image

Setting Up Your Flask Project

You decide the folder layout yourself. For a small app everything can sit in one folder.

Here’s a basic Flask application named example.py:

from flask import Flask
app = Flask(__name__)

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

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

Run the file with the command python example.py, and you’ll receive an output similar to:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Go to the displayed URL in your browser to view the greeting “Hello World”.

Python Flask Application

Extend Your Flask App with Modules

One of Flask’s strengths is its ability to integrate with third-party modules. Depending on your application’s needs, you might find these modules helpful:

  • Flask Login - Efficient user management within Flask.
  • Flask Mail - Enables sending emails directly from your Flask application.
  • SQLAlchemy - A tool to integrate SQL databases with Flask seamlessly.

Deploying Your Flask App

Testing on your own machine is fine, but to put the app online you need a host. PythonAnywhere is the easy one for a Flask app.

Power of Jinja Template Language in Flask

Jinja, a well known template language, finds extensive use in Flask. Templates are essentially the user-facing frontend. For Flask, these templates manifest as HTML files. With Jinja, you can easily integrate Python variables within your HTML files, enhancing the dynamic capabilities of your webpages.

Demystifying WSGI

The Web Server Gateway Interface (WSGI) is a pivotal standard that outlines the interaction protocols between web servers and Python web applications. For an in-depth understanding of WSGI, consider reading this PEP.

If you want to try this yourself, the exercises on PyChallenge turn the same idea into a few short problems with instant feedback.