Getting started with the Flask web framework

Build and run a simple Flask application in under 5 minutes

What is Flask?

Flask is a Python web framework built with a small core and easy-to-extend philosophy that was originally designed and developed by Armin Ronacher in 2010 and became the most popular Python web framework in 2018.

What is a web framework?

A web framework is a code library that makes web development faster and easier by providing common patterns for building reliable, scalable and maintainable web applications. Since the turn of the century, it is common practice for web development projects to use an existing web framework except in very unusual situations. Other than Flask, popular web frameworks include Django, ASP.NET, Laravel, and Ruby on Rails.

  • Input form handling and validation
  • HTML, XML, JSON, and other output formats with a templating engine
  • Database connection configuration and persistent data manipulation through an object-relational mapper (ORM)
  • Web security against Cross-site request forgery (CSRF), SQL Injection, Cross-site Scripting (XSS) and other common malicious attacks
  • Session storage and retrieval

What is web development and how is Flask related?

Web development is the umbrella term for conceptualizing, creating, deploying and operating web applications and application programming interfaces for the Web.

Installing Flask

Prior to installing Flask, we need to install Python. Python can easily be installed using the npm package manager, a.k.a. ‘Node Package Manager’, and the npm install command.

npm install Python
pip install Flask
pip list
Flask dependencies

Key dependencies

Flask depends on the Jinja template engine and the Werkzeug WSGI toolkit. These dependencies will also be installed when we install Flask.

Building our basic Flask application

It’s quick and easy to get a basic application built in Flask. The code below is a fully functional Flask application that I wrote in under one minute. I break down this code further at the end of this section. To use this code as boilerplate code for your own Flask application, you can find the source code on my GitHub.

from flask import FlasksimpleApp = Flask(__name__)@simpleApp.route(‘/’)
def home():
return ‘<h1 style=”color: red”>Hiyee! I am a simple Flask application.</h1>’
if __name__ == ‘main’:
else :
  1. Next, we need to create an instance of the Flask class. We will name it simpleApp. The first argument that we will pass in our instantiation of the class is the name of our application’s module. Since we are using a single module, we pass __name__ as the value because this name will be different depending on if our application is started as a standalone application or imported as a module. This value is needed so that knows where to look for resources like templates and static files.
  2. Next, we use the route() decorator to tell Flask the URL that should trigger our main function.
  3. Next, we create our main function and name it home. Our home() function returns some basic HTML with inline styling. This is what will be displayed in the client, e.g. our browser.
  4. Finally, we save the file and give it a name. I have saved this file as but any filename will due as long as it is not because this would conflict with Flask itself.

Running our basic Flask application

For development purposes, Flask provides a built-in server. To run the application on this built-in server, we first point our terminal to our Flask project’s directory. From within this directory, we need need to tell our terminal the application to work with by exporting the FLASK_APP environment variable:

flask run
python -m flask run

What to do now?

When learning Flask, I found the following checklist to be a great learning map:

  1. Work through a detailed Flask tutorial.
  2. Study open source examples that use Flask so that you can take parts of those projects and reuse the code in your own Flask application.
  3. Build a first iteration of your Flask application.
  4. Learn Flask’s deployment options to make your application accessible on the web.

About the author

Colin Kraczkowsky recently returned to web development after exploring the craft of product management. Colin’s professional history includes working in both enterprise and start up environments to code web and mobile applications, launch new products, build mockups and prototypes, analyze metrics, and continuously innovate.

Problem solver wielding JavaScript and Python as my tools. Builder of RESTful web services and progressive web applications. Scholar of the newly possible.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store