Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Flask Basics

Flask is a lightweight Python web framework used to create web applications and backend services.

What is Flask?

Flask is a lightweight and flexible web framework written in Python. It provides the basic tools required to build web applications.

Flask is easy to understand because it does not force a large project structure. Developers can start with a small application and add more features as the project grows.

Simple Definition

Flask is a Python framework used to create websites, web applications and APIs.

Features of Flask

Lightweight

Flask provides the basic components required for web development without unnecessary complexity.

Easy to Learn

Its simple structure makes Flask suitable for beginners.

Flexible

Developers can choose libraries and project structures according to requirements.

API Support

Flask can be used to create backend APIs and web services.

Flask Request Flow

A Flask application receives a request, matches it with a route, executes a Python function, and returns a response.

Browser

User sends a request to a URL.

→
Flask Route

Flask finds the matching route.

→
Python Function

The function processes the request and returns the response.

Installing Flask

Flask can be installed using Python's package manager, pip.

pip install flask
Note: Installing Flask inside a virtual environment is recommended for project-based development.

Creating Your First Flask Application

Create a Python file named app.py and write the following code:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from Flask"

if __name__ == "__main__":
    app.run(debug=True)

Understanding the Code

Flask Import
from flask import Flask

Imports the Flask class from the Flask package.

Application Object
app = Flask(__name__)

Creates the Flask application object.

Route
@app.route("/")

Connects the root URL with a Python function.

Run Application
app.run(debug=True)

Starts the Flask development server.

Creating a Basic Route

A route connects a URL to a Python function. When a user visits that URL, the function executes.

from flask import Flask

app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(debug=True)

Creating Multiple Routes

A Flask application can have multiple routes for different pages.

from flask import Flask

app = Flask(__name__)

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

@app.route("/about")
def about():
    return "About Page"

@app.route("/contact")
def contact():
    return "Contact Page"

if __name__ == "__main__":
    app.run(debug=True)

Understanding Flask Routes

URL Function Purpose
/ home() Displays the home page
/about about() Displays the about page
/contact contact() Displays the contact page

Running the Flask Application

Open the terminal inside the project folder and run:

python app.py

Flask starts a local development server.

http://127.0.0.1:5000/
Open the URL in a browser to view your Flask application.

Example: CIIT Training Institute 👀🤓

The following example creates simple routes for a training institute.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Welcome to CIIT Training Institute"

@app.route("/courses")
def courses():
    return "Python | .NET | Java | Data Science"

@app.route("/contact")
def contact():
    return "Contact CIIT Training Institute"

if __name__ == "__main__":
    app.run(debug=True)

Debug Mode

Debug mode is useful while developing a Flask application. It helps display development errors and reload the application when source code changes.

app.run(debug=True)
Important: Debug mode should be used for development and not treated as a production configuration.

Request and Response

Request

A request is sent by the browser or another client to the Flask server.

GET /about
Response

Flask processes the request and sends a response back to the client.

About Page

Important Flask Concepts

Application

Represents the Flask web application.

Route

Connects a URL to a Python function.

Response

Data returned to the browser or client.

Request

Data sent by a client to the server.

Server

Runs the backend Flask application.

URL

Identifies a route or resource in the application.

Best Practices

  • Use a virtual environment for Flask projects.
  • Use meaningful route and function names.
  • Keep application logic organized.
  • Validate user input before processing it.
  • Keep sensitive information outside source code.
  • Use debug mode only during development.

Common Mistakes

  • Flask package is not installed.
  • Wrong URL is used in the browser.
  • Incorrect route syntax.
  • Forgetting to run the Python application.
  • Incorrect indentation in Python code.
  • Using development settings carelessly for production.

Practice Exercise

Create a Flask application with these routes:

  1. / → "Welcome to Python"
  2. /student → "Student Page"
  3. /courses → "Python Courses"
  4. /contact → "Contact Page"

Run the application and test every route in the browser.

Summary

Flask is a lightweight Python web framework used to build web applications and APIs. Flask applications are created using the Flask class, and routes connect URLs with Python functions. A Flask application can contain multiple routes, while the app.run() method starts the development server. Debug mode is useful during development, and Flask provides a simple and flexible way to develop web applications.