Skip to main content

What is Flask? A Micro Web Framework

Now that we understand the basic concepts of how the web works, it's time to choose a tool to help us build web applications in Python. There are many choices, but one of the most popular, especially for beginners and for building smaller applications and APIs, is Flask.

Flask is a micro web framework. This doesn't mean it's less powerful than other frameworks, but it does mean that it aims to keep its own core simple and extensible.


πŸ“š Prerequisites​

You should understand the basic web concepts of HTTP, URLs, and the request/response cycle.


🎯 Article Outline: What You'll Master​

In this article, you will learn:

  • βœ… What "Micro" Means: Understand Flask's philosophy of having a simple core and using extensions for everything else.
  • βœ… Core Features of Flask: Learn about the main tools Flask provides out of the box, like routing and a development server.
  • βœ… Why Choose Flask: See the main advantages of Flask, including its simplicity and flexibility.
  • βœ… The "Hello, World!" of Flask: A sneak peek at what a minimal Flask application looks like.

🧠 Section 1: The "Micro" Philosophy​

The term "micro" in "micro web framework" can be misleading. It does not mean that your entire application has to fit into a single file (though it can!). It means that Flask itself provides only the essential, core components needed for web development.

These core components are:

  1. Routing: A system to map incoming URL paths to your Python functions.
  2. Request & Response Objects: Tools to handle incoming request data and formulate outgoing responses.
  3. A Development Server: A simple, built-in server for running your app during development.
  4. Templating: Integration with the Jinja2 templating engine to generate dynamic HTML.

For everything elseβ€”database integration, web forms, user authentication, etc.β€”Flask relies on extensions. There is a rich ecosystem of third-party packages that plug into Flask to provide additional functionality. This approach gives the developer complete freedom to choose the tools and libraries that are best for their specific project.

This contrasts with larger "batteries-included" frameworks like Django, which come with a pre-selected set of tools for most common web development tasks (like an ORM for databases and an admin panel).


πŸ’» Section 2: Core Features of Flask​

Let's look at the main feature you'll interact with constantly in Flask: routing.

Flask uses decorators to handle routing. You write a standard Python function and then "decorate" it with a route that specifies which URL path should trigger that function.

A Sneak Peek at Flask Code:

from flask import Flask

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

# Use a decorator to map the URL path "/" to this function
@app.route("/")
def hello_world():
# The return value is what gets sent back to the browser
return "<h1>Hello, World!</h1>"

# This function is mapped to the "/about" URL path
@app.route("/about")
def about_page():
return "This is the about page."

This decorator-based approach is considered very "Pythonic." It's clean, readable, and keeps the routing logic right next to the function that handles it.


πŸ› οΈ Section 3: Why Choose Flask?​

Developers choose Flask for several key reasons:

  • Simplicity and Ease of Use: Flask is famously easy to learn. You can write a complete web application in just a few lines of code, making it perfect for beginners.
  • Flexibility: Flask doesn't make decisions for you. You are free to choose your own database library, your own form validation library, and structure your project however you see fit. This gives you immense control.
  • Extensibility: While the core is small, the Flask ecosystem is huge. There are extensions for almost any functionality you can imagine, allowing you to scale your application's complexity as needed.
  • Great for APIs and Small Services: Because it's so lightweight, Flask is an excellent choice for building APIs, microservices, or simple web applications that don't need the full feature set of a larger framework.

✨ Conclusion & Key Takeaways​

Flask is a powerful, flexible, and Pythonic web framework that provides the essential tools for web development without imposing a rigid structure or a specific set of technologies. Its "micro" nature makes it easy to get started with but powerful enough to build complex applications.

Let's summarize the key takeaways:

  • Flask is a micro framework: It has a simple core and relies on extensions for advanced functionality.
  • It uses decorators for routing: The @app.route() decorator maps a URL to a Python function.
  • It's flexible: You choose the tools and libraries you want to use.
  • It's easy to learn: Flask is a great starting point for anyone new to web development in Python.

➑️ Next Steps​

We've learned what Flask is conceptually. In the next article, it's time to get our hands dirty. We'll walk through "Setting up a Flask Environment: Installation and project structure" and prepare to write our first real Flask application.

Happy coding!