← Back to Chapters

Flask Tutorial

? Flask Tutorial

? Quick Overview

Flask is a lightweight Python web framework used to build web applications quickly. It provides the essential tools, libraries, and features you need to create simple single-page apps or larger, more complex web applications.

Flask is often called a micro-framework because it keeps the core minimal and lets you add only the components you need, making it easy to learn and flexible to use.

? Key Concepts

  • Flask application object – created using Flask(__name__).
  • Routes – URLs in your app mapped to Python functions using @app.route().
  • View functions – Python functions that return what the user sees in the browser.
  • Development server – built-in server started using app.run().
  • Debug mode – use debug=True for automatic reload and better error messages during development.
  • Virtual environment – recommended to isolate dependencies per project.

? Installing Flask

Install Flask using pip from your terminal or command prompt:

? View Installation Command
# Install Flask using pip
pip install Flask

? Syntax / Theory

At the core of a Flask app, you usually have these pieces:

  • Create a Flask app object using the current module name: app = Flask(__name__).
  • Use the @app.route() decorator to connect a URL (like '/') to a Python function.
  • The function (called a view) returns a response, often a string or HTML.
  • Use the if __name__ == '__main__': block to run the app when the file is executed directly.
  • app.run(debug=True) starts the development server with debug mode enabled.

?‍? Creating a Basic Flask App

Here is a minimal Flask application that returns a simple message at the home page:

? View Code Example
# Import the Flask class
from flask import Flask

# Create the Flask application instance
app = Flask(__name__)

# Define a route for the homepage
@app.route('/')
def home():
    # Return a simple response
    return "Hello, Flask!"

# Run the app only if this file is executed directly
if __name__ == '__main__':
    # Start the development server in debug mode
    app.run(debug=True)

? Running the App

Save the file as something like app.py, then run it from your terminal:

? View Run Command
# Run the Flask app
python app.py

After running, Flask starts a development server (by default on port 5000). Open your browser and visit:

http://127.0.0.1:5000/

? Adding More Routes

You can define additional routes to serve different pages, like an About page:

? View Route Example
# Define a new route for the About page
@app.route('/about')
def about():
    # Return a simple text response for this route
    return "This is the About page."

Now, visiting http://127.0.0.1:5000/about will show the text from the about() function.

? Live Output / Explanation

? What You Will See in the Browser

  • When you open http://127.0.0.1:5000/, the browser displays: Hello, Flask!
  • When you open http://127.0.0.1:5000/about, the browser displays: This is the About page.

Each route corresponds to a Python function. Flask calls the function when the URL is requested and sends its return value back to the browser as the response.

? Use Cases / When to Use Flask

  • Building small to medium-sized web applications and APIs.
  • Creating quick prototypes for ideas or projects.
  • Building RESTful APIs for frontend frameworks or mobile apps.
  • Learning web development concepts with minimal boilerplate.

? Tips & Best Practices

  • Use virtual environments to manage Flask and other project-specific dependencies.
  • Enable debug=True only in development, not in production.
  • Keep routes and view functions small and focused for cleaner, more maintainable code.
  • Use meaningful route names and URLs to make your app easier to understand.
  • Organize larger apps using Blueprints and separate files as the project grows.

? Try It Yourself / Practice Tasks

  • Create a Flask app with multiple routes like /contact and /services.
  • Modify your routes to return simple HTML content (e.g., <h1>Welcome</h1>) instead of plain text.
  • Experiment with URL parameters (e.g., /hello/<name>) and display the name in the response.
  • Add a new route that returns information about your favorite programming language.