When it comes to Python web frameworks, Django and Flask are two of the most popular. We’ve already created a whole beginner’s guide to the Django framework, so now let’s take a look at its tiny (but mighty) sibling.
So, what is Flask? Flask is a microframework for developers, designed to enable them to create and scale web apps quickly and simply. Explaining how they go about doing that will take a little more unraveling.
First, we’ll briefly go over the main ways in which web developers use Python in digital systems, then we’ll introduce the Flask microframework itself. Next, we’ll take a look at some of the pros and cons of using it, and compare Flask with its more well-known stablemate, Django.
All set? If you’d prefer to skip ahead to one of the sections in the article, simply use the table of contents:
- How do web developers use Python?
- What is the Flask framework?
- Advantages and disadvantages of Flask
- Flask vs Django frameworks
- How to learn Python for web development
1. How do web developers use Python?
The coding language Python is not simply the domain of data scientists and analysts. Its flexibility is one of the key reasons why web developers are learning it in huge numbers, and professionally utilizing it for all kinds of projects.
Primarily they use it to create powerful backend web applications which can handle data quickly. They can also employ it for activities as varied as data scraping, machine learning, and artificial intelligence.
Developers love Python’s ease of use and efficiency, as it produces websites which are quick-loading and secure. Even before the roll-out stage—Python is ideally suited for designing and testing prototypes as well, allowing them to iterate and develop, reaching the finished product quickly.
If you’re looking for more specific instances, we’ve explored some of the most well-known Python examples from the likes of Netflix, Uber, and Spotify.
In fact, there are so many advantages of learning and using Python for web development. When it comes to developing a web app in Python, there’s a whole host of options available, but up there at the top of the list are Django and Flask.
For now though, let’s go ahead and learn a bit more about the second of those two, Flask.
2. What is the Flask framework?
Flask is the sixth most wanted web framework according to the 2021 Stack Overflow Survey—not bad for what’s referred to as a microframework. Why this name tag? Essentially because it doesn’t rely on other tools or libraries in order to function. At just over 11 years old, Flask is roughly the same age as the more famous Python web framework, Django.
In keeping with the Python language being named after the comedy troupe Monty Python, the entire Flask project itself was originally meant to be an April Fool’s joke. Armin Ronacher, the creator, realized that what he had been joking about could actually make it as a proper framework to build web applications. Its name is a play on a slightly earlier web framework, Bottle.
Flask is what’s known as a WSGI framework. Mercifully pronounced “whiskey,” this stands for Web Server Gateway Interface. Essentially, this is a way for web servers to pass requests to web applications or frameworks. Flask relies on the WSGI external library to function, as well as the Jinja2 template engine.
It’s an out-of-the-box framework, meaning that you can jump straight into it—one of its major advantages.
So, now we know what Flask is, it’s time to see how developers use it.
3. Advantages and disadvantages of Flask
Advantages of Flask
Size is everything, and Flask’s status as a microframework means that you can use it to grow a tech project such as a web app incredibly quickly. If you want to make an app that starts small, but has the potential to grow quickly and in directions you haven’t completely worked out yet, then it’s an ideal choice. Its simplicity of use and few dependencies enable it to run smoothly even as it scales up and up.
This is the core feature of Flask, and one of its biggest advantages. To paraphrase one of the principles of the Zen of Python, simplicity is better than complexity, because it can be easily rearranged and moved around.
Not only is this helpful in terms of allowing your project to move in another direction easily, it also makes sure that the structure won’t collapse when a part is altered. The minimal nature of Flask and its aptitude for developing smaller web apps means that it’s even more flexible than Django itself.
Easy to negotiate
Like Django, being able to find your way around easily is key for allowing web developers to concentrate on just coding quickly, without getting bogged down. At its core, the microframework is easy to understand for web developers, not just saving them time and effort but also giving them more control over their code and what is possible.
When we use this term in relation to a tool or framework, we’re talking about the design of it—there are few constituent parts that need to be assembled and reassembled, and it doesn’t rely on a large number of extensions to function. This design gives web developers a certain level of control.
Flask also supports modular programming, which is where its functionality can be split into several interchangeable modules. Each module acts as an independent building block, which can execute one part of the functionality. Together this means that the whole constituent parts of the structure are flexible, moveable, and testable on their own.
Following the creator’s own theory that “nice documentation design makes you actually write documentation,” Flask users will find a healthy number of examples and tips arranged in a structured manner. This encourages developers to use the framework, as they can easily get introduced to the different aspects and capabilities of the tool. You’ll find the Flask documentation on their official website.
Disadvantages of Flask
Not a lot of tools
Inevitably there are some downsides to this microframework’s lightweight nature. Chief among them is that unlike Django, Flask lacks a large toolbox. This means that developers will have to manually add extensions such as libraries. And, if you add a huge number of extensions, it may start to slow down the app itself due to a multitude of requests.
Difficult to get familiar with a larger Flask app
Because of the fact that development of a web app using Flask can take a variety of twists and turns, a web developer arriving to the project mid-way through can struggle to come to terms with how it’s been designed. The modular nature of the microframework that we mentioned earlier can come back to haunt coders, who will have to familiarize themselves with each constituent part.
Because it is so versatile in terms of which technologies it can interface with, quite often a company using Flask will incur extra costs of supporting those technologies. For example, if a technology interfacing with your Flask app becomes obsolete or is discontinued, then the company will have to scramble to find a new compatible one. The more complicated the app becomes, the higher the potential maintenance and implementation costs.
4. Flask vs Django frameworks
You’ve most likely gotten the sense by now that there are a lot of similarities between these two. It’s increasingly often that they are mentioned in the same breath. Both Flask and Django are designed to allow developers to get started easily with projects, as well as to scale them up quickly into applications. Both are easy to deploy, both make unit testing incredibly easy, and come with healthy documentation.
Allowing web developers to be more productive coding in Python than in other languages goes a long way to explaining this appeal and popularity. In fact, the 2021 JetBrains survey of Python developers had Django and Flask deadlocked at the top in terms of popularity. But how do they differ?
A good rule of thumb for knowing which to employ when, is that Django is suitable for big projects or small ones that will stay small, whereas Flask is best for small apps that could get big quickly.
Django is a monolith, which means that it tries to be a complete, one-stop shop for your needs. By its own demanding nature, this can prove suboptimal for some developers. Alternatively, as a microframework, Flask can interact with other tools easily and flexibly, even the ones you didn’t plan on working with when you set out. With Django, you’ll have to define the scope of your project before starting.
Flask has a much smaller and less organized community compared to Django, so if you’re stuck on something, you’re less likely to find a fix on a forum, and there are fewer active users to ask for help.
An easy way to check this is on the mother of all web forums—Stack Overflow. A check of questions showed at the time of writing 46,966 questions with the Flask tag, a figure dwarfed by the 273,775 Django questions on the forum. This is not to suggest that Flask’s community is completely dead—it’s a slightly younger framework, and the community with it.
While Django boasts excellent authentification and login functionality for users, Flask is not able to provide this. However, the microframework does have support for secure cookies client-side, though. In general, because it’s considered a “batteries included” framework, Django tends to be thought of as more secure.
Another key difference for developers working with databases is how much support Django and Flask provide for that, chiefly ORM. Object Relational Management allows the API to access data easily, without having to write SQL commands.
Django supports ORM, allowing complex queries to be written easily. Flask developers will unfortunately have to write all of their own SQL statements by themselves, which can add a lot of extra work.
Flask is more flexible than Django, due to its relative nimbleness, whereas Django overall is seen as cheaper to maintain from a business costs point of view. As well as needing more support due to the range of technologies it can work with, payroll-wise it’s easier to find developers experienced in Django than Flask.
In terms of comparing the two and deciding as a business or as a tech team which to use for which project, that really depends on the nature and scope of the project itself. But if you’re a web developer who works in Python and is deciding which framework to familiarize themselves with, why not both?
Knowledge and experience of rolling out projects using both Django and Flask can seriously add value to your skillset (and potentially boost your full-stack developer salary, too).
However, if you haven’t already added Python to your web development skillset, let’s take a look at how you can get started.
5. How to Learn Python for web development
There are many ways to learn one of the most popular coding languages in the world—online and offline, free and paid. For those starting from the beginning, we recommend using a mix of resources.
That way, you’ll be able to work out which style of learning suits you best, as well as to get a broad overview of all the language offers, and how to make the most of it as a web developer. We’ve covered this in more depth in our step-by-step guide to learning Python.
In terms of learning Flask, it should not take you more than a few weeks at most to get a handle on the microframework, depending on your other commitments and your learning goals. There are a large number of handy video tutorials on YouTube and Udemy, as well as simple example projects provided by RealPython.
So there you have it: an introduction to the Flask microframework, including how to get to grips with it, and its pros and cons. Crucially, it’s important that tech teams learn which projects Flask is best suited to and which ones are a job for the larger Django web framework. Either way, any Python web developer needs to know their way around at least one of these two popular tools.
If you’re interested in learning more about the world of web development, these articles may interest you: