Flask’s my favorite web framework for Python. It’s small, minimal, and easy. Flask lets you include third party modules for additional functionality, it doesn’t force an architecture on you.

A Web Framework is a collection of modules / libraries that ease the creation of the web apps.

Related course: Python Flask: Create Web Apps with Flask

python flask example

what is Flask?

Flask is a microframework written in Python that provides only critical components, such as routing, request handling, sessions, and so on. It’s called “beginner friendly” because it doesn’t have a boilerplate or dependencies that can confuse you.

The features that make Flask the perfect platform for creating web apps are:

  • Flask offers both a development server and a debugger.
  • Use the Jinja2 templates and WSGI compliant (more on that later)
  • There are several plugins available for Flask that can be used to improve its functionality.
  • Great for building your backend

install flask

You can install Flask globally or in a virtual environment. If you use an IDE like PyCharm, you can just install the module in your project.
To install Flask globally you can use the command

pip install Flask --user

To test if it’s installed correctly, start the python CLI and try to import the Flask module.
If it’s correctly installed you’ll see this output:

$ 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
Type "help", "copyright", "credits" or "license" for more information.
>>> from flask import Flask
>>>

import flask

setup project

Flask doesn’t force any directory structure on you, you can do this yourself.
If you want, you can have all your files in a single directory.

Create a file example.py with this contents:

from flask import Flask
app = Flask(__name__)

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

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

Then run it with the command python example.py

Python will output this:

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

In your webbrowser you can open the url it outputs, it will output the functions return value.

python flask example

Useful Flask modules

Flask utilizes modules from third parties to provide additional functionality like mail or databases.
When you develop your app you’ll want to use a variety of modules depending on your needs.

This course teaches you all Flask for building web apps.

Some modules you can use are

Deployment

It’s good to code the app on your machine, but launching it in the internet is better, especially if it is the first project. You can use PythonAnywhere to put your app online.

Jinja template language

Jinja is a template language that is used in Python Flask. What is a template language?

Templates are the front-end that the consumer uses. For a Flask website, the models are the HTML files. Jinja lets you inject Python variables in the HTML ifles.

WSGI - Web Server Gateway Interface

The Web Server Gateway Interface, or more generally referred to as WSGI, is a standard that specifies the requirements for communication between a web server and a client application. You can find more about WSGI in this PEP

Related course: Python Flask: Create Web Apps with Flask