Flask is a micro-framework written in Python, it’s based on Werkzeug and uses the Jinja2 template engine, it’s very fun and easy to use and the most important thing is that it’s free and open source (Released under BSD license). You can use Flask to develop web applications in Python.

There are many Python Frameworks out there like Django, bottle and others but their problem is that they are full of extra and unnecessary features that maybe only large websites may need, for most web applications, this is unneeded, that’s why we have “micro-frameworks”.

Flask is very simple; you can understand its code even if you weren’t a Python programmer, it has a lot of features like using Jinja2 template engine, RESTful request dispatching, secure cookies, supports Unicode, has a lot of extra extensions that you can download and use easily, and a lot more.

I have been using Flask for a while to develop Python web applications and it really did the job, the time needed to create an application and release it to the public is very much less comparing to other frameworks and the important thing here for programmers is that it’s fun, you would spend hours loving what you are doing.

Setup Flask

Flask actually is just a Python module, import from the library and you are ready to go!

On most Linux distributions, you can use the “pip” command to install the needed module, pip is the package manager for Python modules, it’s available on all distributions to install, for example, on Ubuntu/Mint you would run:

sudo apt install python-pip

On Fedora:

sudo dnf install python-pip

On openSUSE:

sudo zypper install python-pip

And almost same for other distributions, just search for “pip” or “python-pip” in your package manager to install the package. Now after you install pip, we can now use it to install Flask module:

sudo pip install flask

Example Application

Let’s take this official example from the official website to see how things are done:

from flask import Flask
app = Flask(__name__)

def hello():
    return "Hello World!"

if __name__ == "__main__":

What this mini-application will do is to setup a running server on localhost:5000, accept requests and then return a “Hello World!” string back to the user. Simple isn’t?

A More Useful Example

Basically, a web application developed by Flask would have the following structure of files:

  • templates (Folder) —> index.html, 404.html, search.html, layout.html, publish.html (And a lot more depending on your application).
  • static (Folder) —> style.css, images, (And all other assets files).
  • —> The main application file, the Flask code will be here.
  • –> The configuration file, this file will be imported by, this is extra actually, you can put it in, but it’s better for organizing your code and separating between the application code and the application configuration.
  • Schema.sql –> The structure of the SQL database that you will create for your application, this file will contain SQL statements that will create the necessary tables for your application.
  • database.db –> The database file, keep it secret, keep it safe.

Of course, everything may be different according to your needs and the web application that you are working on right now, but this is just the basic setup to start.

We have prepared a running demo application featuring most of those files for you, it’s a very basic blog script, just grab the code and run it locally using “python” command after you setup Flask module, Here’s a screenshot for it:

Flask Demo Web Application
Flask Demo Web Application

And here’s the code, just download the file and extract the .zip file contents to start. The code is full of comments in the file to explain everything you may need about it, if you have a question, send it in the comments 🙂

Serving Flask Applications

Python – Unfortunately – is not supported in web hosting companies like PHP, if you are using a shared hosting and that hosting doesn’t support Python, you won’t be able to run a Flask application on it.

But, if you are running a Linux server or a VPS (We recommend DigitalOcean), you have a lot of options to deploy your application, whether you are using Apache2 or Nginx, Flask almost supports them all:

Further Reading

One of the awesome features of Flask is that it has a really big and well-written documentation, it covers everything you may need to develop a professional web application, you can check it at their official website.

You can also check other available code snippets, if you needed help, head to their community page.

Load More Related Articles