What is Flask?
A beginner-friendly introduction to Flask, the minimal Python micro-framework — what it is, how it works, and how to build your first app.
Most programmers would agree that Python is one of the easiest languages. For me, it is a lot like English with a lot of logic and spacing 😊. One of the best things about Python is its ability to be modified for different purposes, one being web development, in the form of web frameworks. There are many Python frameworks but those that stand out include Django, Flask, and FastAPI.
In this article, we shall take a peek at Flask Web Framework.
So, what is Flask?
To understand Flask, let’s first understand what a framework is. A framework in programming simply means a tool that provides you with some sort of functionality and utilities out of the box. For example, Django will provide you with routing, authentication, an admin portal, database connections and more — right out of the box.
This is great, it lets you focus on the main part of development. But sometimes you might not need all that functionality, or you want something simple with your own implementation.
Here is where Flask comes in. Flask is a minimal Python micro-framework based around Werkzeug and Jinja that gives you essential functionality and allows you to add your own with other Python packages.
How does it work?
Flask was developed by Armin Ronacher, who led a team of international Python enthusiasts called Pocoo. Its foundation is based on the Werkzeug WSGI toolkit and Jinja2 templating.
WSGI
Stands for Web Server Gateway Interface. This is a standard used in Python that specifies how web servers communicate with web applications.
Werkzeug
A WSGI toolkit that implements requests, response objects, and utility functions.
Jinja2
A popular Python template engine. It allows the combination of a web template with a data source to render dynamic data — enabling Python-like syntax inside HTML files.
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
Setting up a simple Flask app
The only requirement is Python and pip.
pip install flask
😲 Just like that you can use Flask on your machine.
Let’s build a small Flask app that returns “Hello World” at http://localhost:5000:
# import flask
from flask import Flask
# create a Flask instance
app = Flask(__name__)
# create our initial route
@app.route('/') # In Flask this is a decorator
def index():
return "Hello world"
if __name__ == '__main__':
app.run(debug=True)
Run it:
python app.py
Visit http://localhost:5000 and you’ll see “Hello world”.
Rendering a template
In a real-world project you’d return a template instead of a plain string. Create a templates/index.html alongside app.py:
<!DOCTYPE html>
<html lang="en">
<head><title>Our Flask app</title></head>
<body>
<div class="container">
<h2>Hello Flask app</h2>
</div>
</body>
</html>
Then update app.py to render it:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
Flask looks for a template named index.html in the templates folder and returns it to the browser.
Passing data to a template
Let’s return a list of animals from our route and render them in the template:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
animals = ['Dog', 'Cow', 'Lion', 'Panther']
return render_template('index.html', animals=animals)
if __name__ == '__main__':
app.run(debug=True)
And in index.html:
<ul>
{% for animal in animals %}
<li>{{ animal }}</li>
{% endfor %}
</ul>
Reload http://localhost:5000 and your list of animals appears — powered by Jinja2.
Conclusion
By no means have we exhausted what Flask is capable of. There are many powerful features but this is a short introduction to get your engines started for Flask exploration.
👍 Happy coding with Flask!