Skip to content

Flask Introduction

This content is not available in your language yet.

What is Flask?

The Python micro framework for building web applications.

Flask is a Python framework for building web applications. It is lightweight and modular, and is a great tool for building simple websites. In this guide, we will build a simple website using Flask.

Knowledge Required

Basic familiarity with Python, HTML/CSS and SQL will be needed to build this website and resources can be found here:

Getting Started

Installing flask

To install Flask, you can use pip, which is the package manager for Python. Run the following command in your terminal:

Terminal window
pip install Flask

Basic Flask App

Flask applications are relatively simple to create. Here is a basic Flask app that displays “Hello, World!” on the webpage:

app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"

To run the Flask app, save the code in a file called app.py and run the following command in your terminal:

Terminal window
$ flask run
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit

This will start a development server on your local machine, and you can access the website by visiting http://127.0.0.1:5000 in your browser. You should see the text “Hello, World!” displayed on the webpage.

So how does this work?

  1. Import the Flask class from the flask module.

    from flask import Flask;

    This imports the Flask class from the Flask module, which is used to create a Flask application.

  2. Create the Flask app.

    app = Flask(__name__)

    This creates a new Flask application instance. The __name__ argument is a special Python variable that is set to the name of the current module. This is used by Flask to determine the root path of the application.

  3. Define a route.

    @app.route('/')

    Flask uses things called decorators @app.route('/') to map functions to different webpages (routes). In this case, we are mapping the index function to the root URL /.

  4. Define the index function.

    def hello():
    return 'Hello, World!'

    When a user visits the root URL, the hello function is called, and it returns the text Hello, World!. For more complex applications, you can return HTML or JSON data.