An introduction to Flask – a Python framework for web applications


Lesson 4 of 14 · Python for the web

Now that we understand how the HTTP protocol and the client-server model work, we can start making a real web application in Python.

In this lesson we will get to know Flask – one of the most popular Python frameworks for web programming.

Flask is a lightweight Python framework that lets you make web applications with very little code.

What is a web framework?

A web framework is a set of libraries and tools that make building web applications easier.

Instead of the programmer writing every part of the server, the framework already offers ready-made solutions for:

  • handling HTTP requests
  • managing URL routes
  • working with templates
  • working with databases
A framework saves time and lets the programmer focus on what the application does.

Why use Flask?

Flask is especially popular among beginners because it is easy to learn.

  • simple syntax
  • a small number of files
  • easy to understand
  • a large community of users

That is why Flask is an excellent choice for your first Python web projects.


Installing Flask

Flask is installed with the Python package manager pip.


pip install flask

After installing it we can start writing our first web application.

If you use several Python versions, you may need to use the command:

pip3 install flask

The simplest Flask application

Now we will make our first Python web server.


from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from the Python server!"

app.run()

When we run this program, Flask starts a small web server on our computer.

If we open this address in the browser:


http://127.0.0.1:5000

we will see the text that we defined in the function.

The result in the browser
Hello from the Python server!

How does this code work?

  • Flask(__name__) creates a web application
  • @app.route("/") defines a URL address
  • the function home() returns the content of the page
  • app.run() starts the web server
Every URL in a Flask application is linked to one Python function that returns the server's response.

Mini project – The first web page

Let us make a small application that shows a simple HTML page.


from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "<h1>Welcome to my Python website</h1>"

app.run()

How does this project work?

  • Flask starts a local web server
  • the function home() returns HTML content
  • the browser shows the HTML as a web page

Where is Flask used?

  • web applications
  • API services
  • backends for mobile applications
  • project prototypes
  • educational projects

Related lessons


Below are answers to the most common questions about Flask and Python web programming.


Frequently asked questions

What is Flask?

Flask is a Python framework for developing web applications.

Is Flask good for beginners?

Yes. Flask is one of the simplest web frameworks to learn.

What is the difference between Flask and Django?

Flask is simpler and more flexible, while Django offers more ready-made features.

Can Flask run big applications?

Yes. Flask is also used in big projects, but often together with other tools.