How to Build a Web Application with Flask: A Beginner's Guide

If you’re new to web development, you may be wondering where to start. There are many programming languages, frameworks, and tools to choose from. Flask is a popular Python web framework that makes it easy to build web applications. In this beginner’s guide, we’ll cover the basics of building a web application with Flask.

What is Flask?

Flask is a lightweight web framework for Python. It was created by Armin Ronacher in 2010 and is based on the Werkzeug toolkit and the Jinja2 template engine. Flask is easy to use and can be used to build web applications of various sizes and complexities.

Installing Flask

Before you can start building a web application with Flask, you need to install it. You can do this using pip, the Python package manager. Open a command prompt or terminal window and type the following command:

pip install flask

This will download and install the latest version of Flask.

Creating a Flask Application

To create a Flask application, you need to create a Python file with a .py extension. This file will contain the code for your web application. Here’s a basic Flask application:

from flask import Flask

app = Flask(__name__)

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

This code imports the Flask module and creates a Flask application instance. The @app.route() decorator associates the / URL with the index() function, which returns the string “Hello, World!”.

Running the Flask Application

To run the Flask application, you need to execute the Python file that contains the code. In a command prompt or terminal window, navigate to the directory where the file is located and type the following command:


This will start the Flask development server and your web application will be accessible at http://localhost:5000.

HTML Templates

To create a more complex web application, you’ll need to use HTML templates. Flask uses the Jinja2 template engine, which allows you to create dynamic HTML pages.

Here’s an example of a Flask application that uses a template:

from flask import Flask, render_template

app = Flask(__name__)

def index():
    return render_template('index.html', name='John')

if __name__ == '__main__':

In this code, we’ve added the render_template() function, which renders an HTML template called index.html. This file should be located in the “templates” directory. We’ve also passed a variable name to the template, which will be displayed on the web page. The debug=True parameter enables debugging mode, which is useful during development.

Static Files

In addition to HTML templates, you’ll also need to use static files such as CSS and JavaScript files. Flask can serve static files from a directory called static in your application’s directory. For example, you can add the following to the index.html template to reference a stylesheet:

<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">

The url_for() function generates a URL for a static file.


Flask is a powerful and flexible web framework that makes it easy to build web applications with Python. In this beginner’s guide, we’ve covered the basics of creating

See also