Exploring the Flask web framework in Python
Hip Flask
The Flask micro framework emerged from an April Fool's prank, but this simple and easy web development aid is no joke.
Developers who are looking for a Python web framework often turn to the popular Django framework [1], but Django isn't the best choice for every situation. For instance, developers occasionally face tasks that are very difficult to solve with Django's object relational mapper (ORM) or that require additional applications or other frameworks. In some cases, the versatility of Django leads to a complexity that simply isn't needed for a quick and simple project. When less is actually more, a micro web framework such as Flask [2] is often a better option.
This article demonstrates the Flask web framework by putting it to work on a real-world web application: a handy little brainstorming tool known as Geistesblitze. The word Geistesblitze is German for brainstorms or flashes of genius. The tool consists of a web application with graphical Web UI for quickly capturing and associating ideas. This simple example will introduce you to the most important and interesting aspects of Flask. The complete code is available under a BSD license on GitHub [3].
April Fool!
In 2010, the Denied [4] micro web framework popped up on the web for the first time. Python developer Armin Ronacher published it as April Fool's day joke; he wanted to poke fun at the business model of various micro web frameworks that were popular at the time. He bundled all the code into a single file called deny.py
and posted a badly programmed web page (Figure 1) with fake quotes. He also shot a movie with a Dutch friend posing as the French developer of Denied [5].
To his surprise, the reactions to Denied were mostly positive and significantly exceeded those of his other projects. Some developers found the ideas interesting and refreshingly new, and the Flask project was born.
Flask is also available under a BSD-style license and is based on the Werkzeug Toolkit [6], as well as the Jinja 2 template engine [7]. Both projects come from the Pocoo team [8] led by Georg Brandl and Armin Ronacher.
Flask comes with everything you need to design Web applications. Developers will especially appreciate the elegance, the intuitive interface, and the ease with which they can adapt Flask features to suit their needs via extensions. The framework supports Unicode and Python's web server gateway interface (WSGI), as well as several popular security features.
Flashes of Genius
The Geistesblitze project gives users the ability to register and sketch out their ideas, their flashes of genius. Every idea gets a name and a description. The application demonstrates the use of some features of Flask, including templates (render_template
), redirects (redirect
), and messages (flash
). The project also uses the Flask Bootstrap [9], Flask Login [10], Flask SQL Alchemy [11], and Flask WTF [12] extensions.
The structure of a Flask application is largely open. The only requirements are that the static
and templates
directories must exist and reside in the same directory as the application. Even these requirements can change, however, if you use the appropriate parameters when creating the app object.
The rest of the application can live in a single app.py
Python file. You can also break down the complete application into various files, such as models.py
, forms.py
, and views.py
. Separate files are recommended as soon as the code becomes complex and large. For a sample application, a complex file structure is not worth the trouble; the Geistesblitze application consists of the files app.py
, create_all.py
, run.py
; the folders templates
and static
; and some standard files, including requirements.txt
, README.md
, and LICENSE
.
Virtually Secured
Flask supports versions 2.7 and 3.4 of Python. I used a system with Ubuntu 14.04 (64 bit) and Python 3.4.
The first step is to download the Geistesblitze files [3], unpack them, and change to the newly created directory (Listing 1). Use pyvenv
to create a virtual environment and enable it via the commands in line 2. Because Ubuntu 14.04 does not deliver the Pip Python package manager, line 3 retrieves it from a web page and then proceeds to install. Pip, in turn, retrieves all the Geistesblitze dependencies, which are listed in the requirements.txt
file.
The create_all.py
file, which the Flask user executes in the penultimate step, consists of four lines of code and is used to create two tables, users
and ideas
, separately from the application itself in a local SQLite file: geistesblitze.sqlite
. Also the run.py
file is only four lines in length; it starts the actual application by the name of app.py
.
You can then type http://localhost: 5000/register
(Figure 2) to run Geistesblitze in a browser like Firefox. To exit the virtual Python environment, type the (venv) deactivate
command.
If Python 2.7 is used, you also need virtualenv
, which you can install using sudo apt-get install python-virtualenv
. Then follow the steps 1, 2, and 5, through 7 from Listing 1, but change line 1 to virtualenv venv
.
Listing 1
Installing Geistesblitze
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.