Skip to content

Introduction to Flask

What is Flask?

Flask is a micro framework for web applications written in Python.

It is classified as a micro-framework because it does not require specific tools or libraries. There is no database abstraction layer, form validation, or any other components where existing third party libraries provide common functionality. However, it does support extensions that can add app features as if they were implemented in Flask itself. There are extensions for object-relational mappers, form validation, submit handlers, various open authentication technologies, and some popular framework-related tools. Extensions are updated much more often than Flask itself.

Applications that use the Flask environment include Pinterest, LinkedIn, and a community website for Flask itself.

First application

To install Flask you need to use pip:

pip install flask

And the simplest application written in Flask could look like this:

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello World!"

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

and its execution woul require running the following command:

python app.py

Which would have the effect that after visiting http://localhost:5000/ we would see "Hello World!"

Flask allows you to define multiple views, i.e. if the code above is extended to:

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello World!"

@app.route('/hey')
def hey():
    return "Hey!"

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

After entering http://localhost:5000/hey, we will get the inscription "Hey!".