Flask, Web development, one drop at a time.
Intro
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.
Components
- Werkzeug,
- Jinja,
- MarkupSafe,
- ItsDangerous
Features
- Development server and debugger,
- Integrated support for unit testing,
- RESTful request dispatching,
- Uses Jinja templating,
- Support for secure cookies (client side sessions),
- 100% WSGI 1.0 compliant,
- Unicode-based,
- Extensive documentation,
- Google App Engine compatibility,
- Extensions available to enhance features desired
Advantages of Flask
Higher compatibility with latest technologies
Technical experimentation
Easier to use for simple cases
Codebase size is relatively smaller
High scalability for simple applications
Easy to build a quick prototype
Routing URL is easy
Easy to develop and maintain applications
Database integration is easy
Small core and easily extensible
Minimal yet powerful platform
Lots of resources available online especially on GitHub
Why Flask
What's special about Flask?
Flask is a simple and minimalist web framework written in Python. It has no database abstraction layer, form validation, or other components that a web app might require. However, Flask can be enhanced with extensions that can add application features as if they were implemented in Flask itself. It's open source under a BSD license.
Flask is easy to set up and learn. One can write a Flask app in as few as seven lines of code and extend it to thousands. Among the big companies that use Flask are LinkedIn, Pinterest, Reddit, and many more.
Flask along with Bootstrap and SQLite can be used to easily develop full-functioning web apps.
Flask Components
Components
Werkzeug
Werkzeug (German for "tool") is a utility library for the Python programming language, in other words a toolkit for Web Server Gateway Interface (WSGI) applications, and is licensed under a BSD License. Werkzeug can realize software objects for request, response, and utility functions. It can be used to build a custom software framework on top of it and supports Python 2.7 and 3.5 and later.
Also...
Werkzeug is a comprehensive WSGI web application library. It began as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility libraries.
Werkzeug doesn’t enforce any dependencies. It is up to the developer to choose a template engine, database adapter, and even how to handle requests.
Jinja
Jinja, also by Ronacher, is a template engine for the Python programming language and is licensed under a BSD License. Similar to the Django web framework, it handles templates in a sandbox.
Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document.
And Powerful
Jinja2 is one of the most used template engines for Python. It is inspired by Django's templating system but extends it with an expressive language that gives template authors a more powerful set of tools. On top of that it adds sandboxed execution and optional automatic escaping for applications where security is important.
It is internally based on Unicode and runs on a wide range of Python versions from 2.5 to current versions including Python 3. Visite the website for more info
MarkupSafe
MarkupSafe is a string handling library for the Python programming language, licensed under a BSD license. The eponymous MarkupSafe type extends the Python string type and marks its contents as "safe"; combining MarkupSafe with regular strings automatically escapes the unmarked strings, while avoiding double escaping of already marked strings.
MarkupSafe escapes characters so text is safe to use in HTML and XML. Characters that have special meanings are replaced so that they display as the actual characters. This mitigates injection attacks, meaning untrusted user input can safely be displayed on a page.
Well Maintained
We found that MarkupSafe demonstrated a healthy version release cadence and project activity. It has a community of 40 open source contributors collaborating on the project.
ItsDangerous
ItsDangerous is a safe data serialization library for the Python programming language, licensed under a BSD license. It is used to store the session of a Flask application in a cookie without allowing users to tamper with the session contents.
Various helpers to pass data to untrusted environments and to get it back safe and
sound. Data is cryptographically signed to ensure that a token has not been tampered
with.
It’s possible to customize how data is serialized. Data is compressed
as needed. A
timestamp can be added and verified automatically while loading a token.
Many more...
The receiver can see the data, but they can not modify it unless they also have your key. So if you keep the key secret and complex, you will be fine.
Performance Optimization
When Flask app runs slow we need to identify what is the bottleneck. It can be an overloaded database, unresponsive external API, or heavy, CPU-intensive computation. This is the whole recipe on how to speed up Flask - find the source of sluggish performance. After the bottleneck is identified you can fight an underlying cause.
Getting slow SQL queries
Since now we know that querying the database slows us down let’s investigate what queries we execute. There is an SQLAlchemy feature that allows us to get all the queries executed during the request.
Flask-DebugToolbar
More high-level tools can profile Flask. One of them is a flask-debugtoolbar.
This extension adds a toolbar overlay to Flask applications containing useful information for debugging.
The toolbar will automatically be injected into Jinja templates when debug mode is on.
Getting slow SQL queries
Since now we know that querying the database slows us down let’s investigate what queries we
execute.
There is an SQLAlchemy feature that allows us to get all the queries executed
during
the request.
DB Queries
In case if slow DB queries - use EXPLAIN to see if the server uses the index for querying the data.
Or you may spin up a read-replica DB to steer read requests to it if you’ve reached a limit with vertical scaling of the DB server.
If it’s a slow API or CPU intensive job - consider putting it in an async background job.
View dependencies and service relationships across your systems
If your Flask applications are underperforming, you need to know what’s causing the problem right away. The AppOptics visual service map auto-detects the relationships and dependencies between your services, databases, domains, and cache, making it easier to locate unexpected resource drains.
Customize your monitoring to better align with the particulars of your application
Flask extensions allow you to create highly customized applications, and to ensure your applications are operating efficiently, you need an application performance monitoring (APM) tool designed to support the needs of your specific deployment.