Micro Frameworks for Rapid Web Development - Flask

by on

Web Microframeworks are small and focused frameworks which make very few assumptions or decisions about how it will be used in building websites, APIs or web services, all the while providing granular control of the codebase. Web Microframeworks provide a lot of freedom in terms of how it can be used and are not restrictive with respect to libraries and extensions.

Full fledged web frameworks such as Django and Ruby on Rails tend to include a lot of resources which when building a small website or web service introduces overheads. Microframeworks avoid this by providing only the bare essentials and leaving the rest for the web developer to decide.

Some of the popular microframeworks for the web are web.py, Flask, Sinatra. We will discuss Flask in this part.

Flask is a web microframework for Python which includes a development server and debugger. It supports Jinja2 templating, RESTful requests and includes out-of-the-box unit testing. Although Flask itself is small, it is very extensible and a lot of community supported extensions are available which can be used as required.


Where Flask Shines

Flask is great when a framework is required to help in -

  • Rapid prototyping - Prototyping using Flask can be very useful as it takes very little time to setup and use.
  • Small Websites - Websites which are not very complex can be built very easily and quickly using Flask. Although its possible to build large websites using Flask, a thorough framework such as Django or Ruby on Rails is usually used.
  • Lightweight - As flask tries to keep the core minimal, yet extensible, it is very lightweight.
  • Extensible and Configurable - There are lots of modules available that extend Flask, which make sure you don't reinvent the wheel. Very few conventions are used and most of Flask remains configurable.

Built Using Flask

Here are a few examples of where Flask is used -

Hyves - Second largest social network in Netherlands

Quokka - A CMS built on top of MongoDB

Rockyou FM - A German online FM Radio

BF3 Immersion - A Battlefield 3 news aggregator

Nuxeo Community - An Open Source Platform for Enterprise Content Management


Python is included by default in most modern Linux distributions. If Python is not already installed, it can be downloaded from Python's website. Python 2.7 is recommended, even though Python 3.x is supported. After Python is installed, pip needs to be installed which helps download Flask and all its dependencies. After this, Flask can be installed using --

pip install Flask 

If other Python environments needs to be used then virtualenv maybe required. Follow this installation guide here for the same.

The Simplest Web App

To display "hello, flask!" in a web browser, create a folder called hello_flask and create a file called hello-flask.py in the folder

> mkdir hello_flask && cd hello_flask
> touch hello-flask.py

Add the code snippet below to the hello-flask.py

from flask import Flask
app = Flask(__name__)

def hello():
    return 'hello, flask!'


Run this file and you should see the following message -

> python hello-flask.py
* Running on

Voilà! You have a web server running and can access your web page if you go to the given address.

What We Just Did

First, we include the Flask module and create a small flask application.

from flask import Flask
app = Flask(__name__)

Flask uses function decorators as routers. The line


routes the result to the root directory of the web server. So, routing in Flask is as simple as writing a function describing what needs to happen at the location and decorating it with the required router.

Next, we define a simple function called hello, which returns the text that needs to be displayed -

def hello():
    return 'hello, flask!'

To start the app when the program runs -


Making it Look Nice

To format the displayed content, Jinja2 templates are used. To create a simple template, create a file call hello.html in a folder called templates. So, your directory structure will look like this.


In hello.html, add a small HTML markup -

<!doctype html>
<head><title>Hello, World</title></head>
<body><h1>{{ message }}</h1></body>

The braces around 'message' are a part of the Jinja2 templating and is explained later.

Now, rewrite earlier hello-flask.py as-

from flask import Flask, render_template
app = Flask(__name__)

def hello():
    return render_template('hello.html', message='hello, flask!')


Changing Content Dynamically

Try running hello-flask.py and navigate to the address from the browser. You should see the message 'hello, flask!' in big bold letters now. Changing this message is as simple as changing the variable message to another string. So, to display 'hello, everyone', the line would change to -

return render_template('hello.html', message = 'hello, everyone!')

Notice that we also import 'render_template'.

from flask import Flask, render_template    

The HTML templating language used is Jinja2. A simple example of how its used is the braces - anything inside the braces is displayed as HTML after its processed. So, the message sent earlier by the hello function will be displayed here.

<body><h1>{{ message }}</h1></body>

This interaction makes it very easy to change what is displayed in the HTML pages dynamically. That's it, a simple web app in very few lines. To learn more about Flask, use the resources mentioned in the next section.

Learning Flask

The tutorial which is a part of the documentation is a good place to start learning about Flask. The official documentation of Flask is extensive and explains Flask’s features in detail.

Some other tutorials and sites of interest are -

Extensions Available

Some extensions of note are -

  • Flask-WTF- Integrates the popular WTForms with Flask.
  • Flask Uploads - Flask-Uploads allows your application to flexibly and efficiently handle file uploading and serving the uploaded files.
  • Flask SQLAlchemy - Adds SQLAlchemy support, a Python SQL toolkit and Object Relational Mapper
  • Flask Mail - The Flask-Mail extension provides a simple interface to set up SMTP
  • Flask Login - Provides additional user session management support.

Advanced Tutorials

These tutorials teach some advanced concepts useful for building robust Flask applications -

The choice of framework depends on a lot of factors such as language it supports, features, performance, and preference. Large frameworks can be an overkill for smaller projects and similarly, it maybe easier to use a larger framework for an extensive project. Flask shines in providing a lot of core features ideal for small-to-medium websites or services and remains extensible for other features.

Share the love

Published in web-development | Tagged with web-development, prototyping, frameworks






Talk to us, that's always a good idea!