The websites we just built using HTML, CSS, and Bootstrap look great and showcase important information. But what if you want your website to do more? What if you want it to respond to user input, store data, or even make decisions? That's where Flask comes in!
Remember how we used Bootstrap as a front-end framework to speed up the development of our website's user interface? Flask serves a similar purpose, but for the back-end of our web applications. Let's break it down:
Flask is a micro web framework for Python. But what does that mean? Let's explore each part:
Web Framework
Just like Bootstrap helps you build user interfaces more quickly and consistently, a web framework helps you build the server-side of web applications more efficiently. It provides a structure and set of tools to:
Handle web requests and responses
Manage URLs (routing)
Interact with databases
Handle user sessions and security
This saves you from "reinventing the wheel" for common web development tasks.
Micro
Flask is called a "micro" framework because:
It's lightweight: It doesn't come with everything built-in, allowing you to choose which additional tools you need.
It's flexible: You have the freedom to structure your project how you want.
It's minimalist: It provides the essentials without enforcing too many decisions on you.
This "micro" nature makes Flask an excellent learning tool and gives you room to grow as you develop more complex applications.
For Python
Flask is written in Python and allows you to use Python to create your web applications. This means you can leverage:
The Python skills you've already developed
Python's extensive library ecosystem
Python's readability and ease of use in a web context
Content doesn't change unless you manually update the HTML
Same content for all users
Simpler to create and host
Limited interactivity
Content can change based on user input, database queries, etc
Can personalise content for each user
More powerful and interactive
Can handle forms, user accounts, and more
When you use Flask, your website operates on a client-server model:
The client (user's web browser) sends a request to your website
The server (where your Flask app runs) receives the request
Your Flask app processes the request, maybe interacts with a database
The server sends a response back to the client
The client's browser displays the result
Alex's browser requests the reviews page for "Python for Beginners" from BookReviews.com.
The BookReviews.com server receives this request and passes it to the Flask app.
The Flask app queries the database for all reviews of "Python for Beginners".
The server sends back an HTML page containing the book info and all its reviews.
Alex's browser displays the page showing all the reviews for "Python for Beginners".
Alex fills out the review form and presses the submit button. Alex's browser sends a POST request with the review text to BookReviews.com.
The BookReviews.com server receives this POST request and passes it to the Flask app.
The Flask app processes the review data and saves it to the database.
The server sends back a "Thank you for your review" HTML page.
Alex's browser displays the thank you message, confirming the review was submitted.