Understanding Python Flask

Sumit Singh on August 28, 2023

Understanding Python Flask

Introduction

When it comes to Python web frameworks, Flask is one of the most popular ones.
So, what is Flask? What is it used for? And why should you be familiar with it? You must have these questions popping into your mind, but don’t worry. This detailed beginner guide will cover everything and all things Flask.
Let’s begin!

What is a Web Framework?

In the world of computers and websites, a “web framework” is like a pre-set kit for building websites and web applications. It’s a set of pre-made tools and rules that help programmers create websites more easily and quickly.

web application framework definition

A web framework provides tools for making web pages, handling user input, and doing other common tasks. Django, Express, Flask and Laravel are some of the most popular and used web application frameworks.

Now that you know what a web framework is, let’s dig deeper into Python Flask!

What is Flask?

Simply put, Flask is a web application framework written in Python that lets you develop web applications easily.

flask logo

Did you know that the Flask project initially started as an April Fool’s prank? Armin Ronacher, the developer behind it, later recognized that his idea could turn into a functional framework for constructing web applications. The name “Flask” is a playful reference to the earlier web framework called Bottle.

Flask is a microframework with a small and easy-to-extend core. This might be the number one reason of its popularity. You might wonder what micro-framework means, so let’s discuss it.

What is a Microframework?

A microframework is a web framework that provides the core essentials for building web applications without any unnecessary features. It’s like having a small set of tools specifically designed to help you get started quickly and easily.

Microframeworks are minimalistic, lightweight, and flexible, allowing you to add only the components you need for your projects.

Examples of microframeworks include Flask (for Python), Sinatra (for Ruby), and Express (for JavaScript/Node.js)

So, whenever you use Flask, you’ll often come across the phrase “WSGI framework.” Flask is known as a WSGI framework and it heavily relies on it. So, what even is a WSGI framework?

What is a WSGI Framework?

Let’s imagine you’re the conductor of a magical orchestra. Each musician in the orchestra plays a different instrument, and together, they create beautiful music. But to make this orchestra play harmoniously, you need a set of rules and a conductor to guide them.

In our context, a WSGI framework has a similar job to that of the conductor of that orchestra. WSGI stands for “Web Server Gateway Interface,” and it’s like the set of rules that help different parts of a website work together smoothly.

diagram of how WSGI works

It ensures that when someone visits your website, all these different parts understand their roles and work together in harmony. Well, you should know that all this doesn’t magically happen with just a few clicks, there’s an entire backend working to make Flask work.

How Flask Works?

In this part, we will discuss how flask actually works and what different components are needed to make it work!
1. Web Server: You might already know that servers are the heart of web development. They listen for requests from web browsers and communicate to routers accordingly.

2. WSGI Interface: As we have learnt above, Flask uses the WSGI standard to communicate with the web server. Think of it like the party organizer (Flask) speaking the same language as the venue (webserver).

3. Routes and URL Mapping: Flask sets up routes like directions for the organizer to guide guests to different parts of the party. Each route corresponds to a specific URL. For example, “/about” could lead to the “About Us” section of your website.

4. Request Handling: When a user enters a URL in their browser and hits Enter, the browser sends a request to the web server. The web server passes this request to Flask.

5. View Functions: Flask uses view functions to handle requests. These functions are very handy; for example, a view function might serve a webpage, show an image, or process a form submission.

6. Templates: View functions often use templates to create dynamic web pages. Templates are like placeholders to display and retain data. This allows you to create consistent and dynamic content.

7. Response Generation: Once the view function does its work (fetching data, processing forms, etc.), it generates a response. This response contains the content the guest asked for. The response might be HTML for a webpage, JSON for an API, or even an image.

8. Sending the Response: Flask takes the response from the view function and sends it back to the web server. The web server then delivers this response to the user’s browser.

9. User Interaction: The user’s browser receives the response and displays the content. If the user interacts with the page (clicks a link, submits a form), the process repeats—another request goes to the server, Flask handles it, and a new response is generated.

10. Backend Logic: Behind the scenes, Flask can interact with databases, process data, and perform calculations. It acts as the brain of the operation, ensuring that the right data is presented to the user.

And this is a brief operation cycle for a Flask request! Interesting, isn’t it?

Now that you have a basic understanding of Python Flask, let’s discuss its pros and cons!

Flask: Pros and Cons

Flask is an out-of-the-box framework; it’s only 13 years old yet it is one of the most successful frameworks to ever exist. Why is that?

Pros

1. Simplicity: It is easy to understand and use, making it great for beginners and quick project setups.
2. Lightweight and Flexible: As we have already discussed above, Flask doesn’t come with unnecessary features, allowing you to add only what your project needs, making it versatile.
3. Easy to Learn: Flask’s minimalistic features and clear documentation makes it an excellent starting point if you are new to web development.
4. Highly Customizable: You have the freedom to choose libraries and tools that best suit your project, giving you more control over your application’s architecture.
5. Large Community and Collaborative Development: A strong community means plenty of resources, tutorials, and support, along with ongoing improvements to the framework.
6. Versatility: Flask can be used to create a wide range of applications, from small personal projects to larger ones.
7. Ideal for Prototyping: Its simplicity and quick setup make it perfect for building prototypes and small-scale applications.
8. Integrates Well with Other Libraries: Flask can be seamlessly integrated with other tools and libraries in Python, enhancing its functionality.

Cons

1. Limited Built-in Features: Although, Flask’s minimalistic design is a plus point but it also means it lacks certain built-in features found in larger frameworks.
2. Not Suitable for Large-Scale Apps: Due to its simplicity, Flask might not be the best choice for your complex, large-scale applications.
3. Lack of Built-in Database Support: Unlike some frameworks, Flask doesn’t come with its own built-in database support, requiring you to integrate external database systems.
4. Learning Curve for Complex Features: While Flask is easy to start with, more advanced features might require a steeper learning curve.
5. Limited Default Security Features: Flask offers basic security features, but you might need to add additional layers of security for more robust protection.
6. Might Require Familiarity with Python: A basic understanding of Python programming is necessary for you to work effectively with Flask.

Great job so far! Let’s jump into the application and implementation of Flask.

Application of Flask

Let’s look at a few popular websites and applications that use Flask as their web framework.

1. Pinterest: We all know what Pinterest is and how it has become a go-to digital bulletin board for all our ideas. But did you know that Pinterest used Flask in its early stages to develop and prototype features due to its simplicity and quick development capabilities?
2. LinkedIn’s DataHub: LinkedIn’s DataHub, a platform for managing metadata about data resources, has been using Flask for its lightweight and flexible approach to handling web applications.
3. Uber’s Marketplace: Uber’s a platform connecting riders and drivers with third-party services. It is built using Flask to handle various aspects of the user experience.
4. Twilio (TwilioQuest): Twilio, a cloud communications platform, developed “TwilioQuest,” a game designed to teach coding. The game’s web application is built with Flask.

These examples highlight the versatility of Flask and its applicability to different types of projects, from social media platforms to internal tools and educational games.

How to use Flask as a Toolkit?

Using Flask as a toolkit to build web applications involves a few steps and components. If you are looking forward to starting your Flask journey, then here are some essentials:
• Python installed on your computer.
• Flask installed using pip.
• A text editor or integrated development environment (IDE) to write your Python code.
• A basic understanding of Python programming.
• Creativity and a project idea to start building your web application.
We wish you good luck if you pursue your exciting and rewarding journey of learning Flask framework!


Conclusion

Congratulation! Today you have covered an important framework of Python programming. We know that after reading this article, you are excited to start learning more about Flask and the web development arena. Feel free to start your web development journey with us by signing up for our comprehensive courses!

Also, you can take the time to master the fundamentals of Python by taking our awesome Python courses. Let’s learn and program together!

Best Coding Course for K12 Kids

Sign up to our newsletter

Get the latest blogs, articles, and updates delivered straight to your inbox.

Share with your friends

Try a free class