Flask Introduction – Learn Python Flask Framework

Introduction

Flask is one of the most popular Python web frameworks used for building web applications, REST APIs, and backend services. It is lightweight, flexible, and easy to learn, making it an excellent choice for beginners and experienced developers.

Flask a preferred framework for startups, small projects, APIs, and microservices.

What is Flask?

Flask is a lightweight and open-source web framework written in Python. It helps developers build web applications quickly and efficiently.

Flask is based on the WSGI (Web Server Gateway Interface) toolkit and uses the Jinja2 template engine for rendering HTML pages.

Officially released in 2010, Flask was created by Armin Ronacher and has become one of the most widely used Python web frameworks.

Why Use Flask?

Flask offers several advantages that make it popular among developers.

  1. Simple and Easy to Learn
  2. Lightweight Framework
  3. Fast Development
  4. REST API Support
  5. Large Community Support

Flask Architecture

Flask follows a simple architecture.


Browser
   ↓
HTTP Request
   ↓
Flask Application
   ↓
Business Logic
   ↓
Database
   ↓
Response
   ↓
Browser

The application receives requests, processes them, and sends responses back to the user.

How to Install Flask?

Before using Flask, install it using pip.

Command


pip install flask

Verify Installation


pip show flask

Output:

Name: Flask
Version: 3.x.x

Create a First Flask Application

Create a file named:


app.py

Example:


from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
    return "Hello Flask!"
if __name__ == '__main__':
    app.run()

Explanation of the File Code

1. Import Flask


from flask import Flask

Imports the Flask class.

2. Create Application Object


app = Flask(__name__)

Creates a Flask application instance.

3. Create Route


@app.route('/')

Defines the URL endpoint.

4. View Function


def home():

Handles requests for the route.

5. Run Application


app.run()

Starts the Flask development server.

How to Run the Flask Application?

Execute:


python app.py

Output:

* Running on http://127.0.0.1:5000

Open the URL in your browser:


http://127.0.0.1:5000

Result:

Hello Flask!

How to Create Flask Routing?

Routing maps URLs to Python functions.

Example:


from flask import Flask
app = Flask(__name__)
@app.route('/about')
def about():
    return "About Page"
if __name__ == '__main__':
    app.run()

Visit:


http://127.0.0.1:5000/about

Output:

About Page

Multiple Routes Example


from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
    return "Home Page"
@app.route('/contact')
def contact():
    return "Contact Page"
if __name__ == '__main__':
    app.run()

Dynamic Routing

Flask allows dynamic URLs.

Example:


from flask import Flask
app = Flask(__name__)
@app.route('/user/')
def user(name):
    return f"Hello {name}"
if __name__ == '__main__':
    app.run()

Visit:


http://127.0.0.1:5000/user/John

Output:

Hello John

Flask Templates

Templates help generate dynamic HTML pages.

Flask uses the Jinja2 template engine.

Project Structure


project/
│
├── app.py
│
└── templates/
      index.html

HTML Template


<!DOCTYPE html>
<html>
<head>
    <title>Flask App</title>
</head>
<body>
<h1>Welcome to Flask</h1>
</body>
</html>

Render Template


from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def home():
    return render_template(
        'index.html'
    )
if __name__ == '__main__':
    app.run()

Handling Forms in Flask

Flask can process user input from forms.

HTML Form


<form method="POST">
<input type="text"
       name="username">
<input type="submit">
</form>

Python Code


from flask import request
name = request.form['username']

The submitted value can be processed on the server.

Flask Request Methods

Method Purpose
GET Retrieve Data
POST Submit Data
PUT Update Data
DELETE Remove Data

Flask Extensions

Flask can be extended using additional packages.

Popular extensions:

Extension Purpose
Flask-SQLAlchemy Database ORM
Flask-Login Authentication
Flask-Mail Email Support
Flask-WTF Form Handling
Flask-RESTful REST API Development

Flask vs Django

Feature Flask Django
Type Micro Framework Full Framework
Learning Curve Easy Moderate
Flexibility High Moderate
Built-in Features Fewer Many
Project Size Small to Medium Medium to Large

Common Mistakes

1. Forgetting to Install Flask

Error:

ModuleNotFoundError

Solution:


pip install flask

2. Missing Route Decorator

Incorrect:


def home():
    return "Home"

Correct:


@app.route('/')
def home():
    return "Home"

3. Incorrect Indentation

Python requires proper indentation.

Incorrect:


if True:
print("Hello")

Correct:


if True:
    print("Hello")

4. Running Wrong File

Ensure:


python app.py

is executed from the correct directory.

Conclusion

Flask is one of the most powerful and beginner-friendly Python web frameworks. Its lightweight architecture, flexibility, and simplicity make it an excellent choice for developing web applications, REST APIs, and microservices.

By learning Flask, developers gain a strong foundation in web development concepts such as routing, templates, forms, and request handling.

Related Python Tutorials