Flask extension: a tool to simplify development and a list and feature summary of 26 third-party modules (libraries/plug-ins) for efficient daily development

Table of contents

Find extension

Use extensions

Create extension

26 commonly used Flask extension modules

Summarize


picture

Original text: Flask extension: a powerful tool to simplify development and a list and feature summary of 26 third-party modules (libraries/plug-ins) for daily efficient development (qq.com)

Flask is a lightweight Python web framework that provides a concise and flexible way to build web applications. Although Flask itself is already very powerful, its real advantage lies in its rich extension modules. These extension modules provide developers with a variety of rich functions and tools, which can greatly simplify the development process of web applications and improve development efficiency.

For more technical points related to Flask, please pay attention to the public account: CTO Plus’s subsequent posts. If you have any questions, please leave a message in the background for communication.

picture

Extensions refer to packages that add functionality to Flask applications to help complete common tasks, such as adding functions for sending emails or connecting to databases. Flask-SQLAlchemy provides support for easily using SQLAlchemy in Flask. Some extensions also help add entirely new frameworks to your application, such as REST APIs.

In this article, we will introduce some commonly used Flask extension modules and their functions, including Flask-WTF, Flask-SQLAlchemy, Flask-Login, Flask-Mail and Flask-RESTful. These extension modules provide functions such as form processing, database operations, user authentication, email sending, and API development, allowing developers to build powerful web applications more efficiently.

I hope this article can help you understand the importance and role of Flask extension modules and play a role in actual projects. I wish you success in using Flask extension modules!

Find extension

Flask extensions are usually named "Flask-Foo" or "Foo-Flask". You can search PyPI for extension packages tagged Framework::Flask.

picture

Use extensions

We can refer to each extension's documentation for installation, configuration, and usage instructions. Generally speaking, an extension obtains its own configuration from app.config and passes it to the application instance during initialization. For example, an extension called "Flask-Foo" is used as follows:

from flask_foo import Foo

foo = Foo()

app = Flask(__name__)
app.config.update(
    FOO_BAR='baz',
    FOO_SPAM='eggs',
)

foo.init_app(app)

Or you can follow the public account CTO Plus. 26 articles will be used later to introduce the detailed use of these 26 Flask extensions. The articles are as follows:

  1. 1. Flask’s date and time processing library Flask-Moment, and detailed explanations of practical code cases

  2. 2. Flask's backend management library Flask-Admin, and detailed explanations of practical code cases

  3. 3. Build Flask’s RESTful API library Flask-RESTX, and explain the code practical cases in detail

  4. 4. Detailed introduction to Flask’s user authentication and session management library flask-login, as well as detailed explanations of practical code cases

  5. 5. Detailed introduction to Flask’s roles, permissions and authorization library flask-principal, as well as detailed explanations of practical code cases

  6. 6. Detailed introduction to Flask’s user authentication and authorization extension library Flask-Security, as well as detailed explanations of practical code cases

  7. 7. Detailed introduction to Flask's library Flask-marshmallow, as well as detailed explanations of practical code cases

  8. 8. Detailed introduction to Flask's library Flask-PyMongo, as well as detailed explanations of practical code cases

  9. 9. Detailed introduction to Flask's library Flask-DebugToolbar, and detailed explanations of practical code cases

  10. 10. Detailed introduction to Flask's library Flask-Assets, as well as detailed explanations of practical code cases

  11. 11. Detailed introduction to Flask’s library Flask-Uploads, as well as detailed explanations of practical code cases

  12. 12. Detailed introduction to Flask’s library Flask-Cors, and detailed explanations of practical code cases

  13. 13. Detailed introduction to Flask's library Flask-SocketIO, and detailed explanation of code practical cases

  14. 14. Detailed introduction to Connexion, a library for building and managing Flask RESTful APIs

  15. 15. Flask-Classful, a Flask library that helps us build and manage class views more conveniently

  16. 16. Detailed explanation of the use of Flask's database operation library Flask-SQLalchemy

  17. 17. Detailed introduction to the extension library Flask-script for managing and running Flask applications

  18. 18. Detailed introduction to the use of the function library for Flask application database migration: Flask-migrate

  19. 19. Flask library that handles user session (Session) management: Flask-Session

  20. 20. Practical project implementation of form validation using Flask-WTF—Form validation library for Flask applications: Flask-WTF

  21. 21. Detailed introduction to Flask-Mail: a Flask extension library for sending emails

  22. 22. Multi-language and internationalization processing library for the Flask project: Flask-Bable

  23. 23. Library to handle user authentication and login in Flask applications: Flask-Login

  24. 24. Detailed introduction to Flask-OpenID, a library that integrates OpenID to implement authentication in Flask applications.

  25. 25. Detailed introduction to Flask library Flask-RESTful for building RESTful API

  26. 26. Detailed introduction to the Flask-Bootstrap library that integrates the front-end framework Bootstrap in the Flask project

  27. 27. Detailed introduction to caching the flask-caching library, one of the ways to speed up your application

And these extension modules are used in my self-developed products "Enterprise Terminal Asset Security Assessment and Management System" and "Cloud Asset Management System". I will introduce them in detail later using several practical articles on the project.

  • "Asset management system based on Python3.11+Flask+MongoDB+Docker+JQuery"

  • "Asset monitoring system based on Python3.11+Flask+MongoDB+Docker+JQuery"

  • "Asset Monitoring System Based on Python: Agent Development"

  • "Asset monitoring system based on Python3.11+Flask+MongoDB+Docker+JQuery"

  • "Asset Security Assessment System Based on Python3.11+Flask+MongoDB+Docker+JQuery"

Create extension

Although PyPI already includes many Flask extensions, if you can't find a suitable one, you can create your own and publish it for others to use. How to create your own extension, please follow the article of the public account CTO Plus: "Flask Advanced: Flask Extension Development-Custom Extension".

26 commonly used Flask extension modules

In Flask applications, extension modules can help us implement certain functions or solve certain problems more conveniently. The following introduces some commonly used Flask extension modules.

1. Connexion

Build a nice REST API with interactive documentation.

The main goal of Connexion is to make the API development process easier and faster. It provides a way to define and describe API interfaces and parameters based on the OpenAPI specification. The OpenAPI specification is a standard for describing and defining RESTful APIs. It uses JSON or YAML format to represent the interface and parameter information of the API.

Connexion is a Flask-based library for building and managing RESTful APIs. It provides a simple and easy-to-use way to define API routes, operations, and parameters, and automatically generates corresponding Flask applications.

For specific usage, please pay attention to the detailed introduction of the article "Connexion, a library for building and managing Flask RESTful API" at the back of the public account CTO Plus.

2. Flask-Classful

Flask-Classful is a class-based view. Class view is a way of processing HTTP requests. It encapsulates the relevant request processing logic in a class. Using class views, we can associate different HTTP methods (such as GET, POST, PUT, etc.) and URL rules (such as /users, /users/{id}, etc.) with methods in the class to process requests. .

The main goal of Flask-Classful is to make the class view development process simpler and more elegant. It provides a class-based way to define and organize class views, making the code clearer and easier to maintain.

Flask-Classful is a Flask-based library for building class-based views. It provides a concise and elegant way to define and organize view functions, and makes the code structure clearer and easier to maintain.

For specific usage, please pay attention to the detailed introduction of the article "Flask-Classful, a Flask library that helps us build and manage class views more conveniently" on the official account CTO Plus.

3. Flask-SQLalchemy

Flask-SQLAlchemy is a Flask extension of SQLAlchemy that provides support for relational databases, such as MySQL, PostgreSQL, and SQLite.

SQLAlchemy is a powerful Python ORM library for handling database operations.

Flask-SQLAlchemy simplifies the process of using SQLAlchemy in Flask applications, providing database session management and model definition integrated with the Flask context.

Flask-SQLAlchemy A library for connecting to SQL databases (interacting with SQLAlchemy). Using Flask-SQLAlchemy can help you easily operate the database and define the database model, and provides many useful functions, such as creating tables, inserting data, querying data, updating, deleting, model mapping and transaction database operations.

The main goal of Flask-SQLAlchemy is to simplify the process of database operations so that developers can focus more on the implementation of business logic. It provides a way to integrate database models with Flask applications, making database operations simpler and more intuitive.

from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(120), nullable=False)
 
 

For specific usage, please pay attention to the detailed introduction of the article "Detailed explanation of the use of Flask's database operation library Flask-SQLalchemy" behind the public account CTO Plus.

4. Flask-script

Flask-Script is an extension library for managing and running Flask applications. It provides a set of command line tools that can help us manage various aspects of the application more conveniently, such as running development servers, database migrations, command line scripts, etc.

The main goal of Flask-Script is to simplify the management and deployment process of Flask applications. It provides an integrated way to manage various tasks of the application, allowing developers to easily perform various tasks through the command line, such as database migration, data import, scheduled tasks, etc., allowing developers to focus more on Implementation of business logic.

For specific usage, please pay attention to the detailed introduction of the article "Details of the extension library Flask-script for managing and running Flask applications" at the end of the public account CTO Plus.

5. Flask-migrate

Flask-Migrate is a Flask extension based on Alembic. It can help developers manage changes in database schema and provide database migration support (manage migration database). During the development process, we often encounter situations where we need to modify the database structure, such as adding new tables, modifying table fields, etc. If you directly modify the database structure manually, it may cause data loss or inconsistency. Using Flask-Migrate, you can achieve safe and controllable database structure changes by writing database migration scripts.

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to the use of the function library for Flask application database migration: Flask-migrate" behind the public account CTO Plus.

6. Flask-Session

Flask-Session is an extension library of the Flask framework, used to handle user session (Session) management and add session support (Session storage method specification) to Flask applications. In web applications, a session is a mechanism for storing user state information. It can maintain the user's login status across requests, store the user's temporary data, etc. Flask-Session provides a simple and powerful API, making session management more convenient and flexible.

For specific usage, please pay attention to the detailed introduction of the article "Flask Library for Handling User Session (Session) Management: Flask-Session" behind the official account CTO Plus.

picture

7. Flask-WTF

Flask-WTF is a Flask extension of WTForms (a library for form generation and form processing), which provides Flask applications with more convenient form processing and CSRF protection. Using Flask-WTF can greatly simplify form processing code, providing a series of form components and validators, while improving application security.

It uses simple Python classes to define forms and provides functionality for validating, rendering, and processing form data. Using Flask-WTF, developers can easily create and process forms without having to manually handle the complex logic of form validation and data processing. It provides a variety of form field types and validators to make form processing simple and intuitive.

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired

class LoginForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
 
 

For specific usage, please pay attention to the detailed introduction of the article "Using Flask-WTF to Implement Form Validation Projects - Form Validation Library for Flask Applications: Flask-WTF" at the back of the public account CTO Plus.

8. Flask-Mail

Extension library for sending email, with support for multiple mail services including SMTP, Gmail, and Amazon SES.

Flask-Mail is a Flask extension for sending emails. It provides the function of sending emails and can easily send emails to users or administrators. Using Flask-Mail can greatly simplify the code for sending emails while improving the interactivity and user experience of your application.

Flask-Mail is an extension for sending emails. It provides the functionality to send text and HTML emails and integrates very conveniently with Flask. Using Flask-Mail, developers can easily implement the function of sending emails, such as registration confirmation emails, password reset emails, etc. It provides various methods and configuration options for sending emails, making email sending simple and reliable.

Flask-Mail is an extension for sending emails. It provides the functionality to send text and HTML emails and integrates very conveniently with Flask. Using Flask-Mail, developers can easily implement the function of sending emails, such as registration confirmation emails, password reset emails, etc.

from flask_mail import Mail, Message

app.config['MAIL_SERVER'] = 'smtp.example.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'username'
app.config['MAIL_PASSWORD'] = 'password'

mail = Mail(app)

@app.route('/send_email')
def send_email():
    msg = Message('Hello', sender='[email protected]', recipients=['[email protected]'])
    msg.body = 'This is a test email'
    mail.send(msg)
 
 

For specific usage, please pay attention to the detailed introduction of the article "Detailed Introduction to Flask-Mail: Flask Extension Library for Sending Emails" behind the public account CTO Plus.

9. Flask-Bable

Flask-Babel is an extension library of the Flask framework, used to handle multi-language, internationalization and localization functions. In web applications, multilingualism and internationalization are very important. It can help us localize the application so that users can access the application in the language they are familiar with. Flask-Babel provides a simple and powerful API, making multi-language and internationalization more convenient and flexible.

For specific usage, please pay attention to the detailed introduction of the article "Flask Project's Multilingual and International Processing Library: Flask-Bable" at the back of the public account CTO Plus.

10. Flask-Login

Flask-Login is a Flask extension for managing user sessions, user authentication (user authentication) and management, and provides many useful features, such as it provides redirection, request interception, user management, adding user authentication and sessions It supports functions such as (authentication of user status), user login and logout (logout), and can also easily manage information such as user status and permissions.

Very tightly integrated with Flask, it provides various methods and decorators for user authentication, making user authentication simple and secure. Using Flask-Login, developers can greatly simplify user authentication and management code, easily implement user authentication and protect pages that require login, and at the same time improve application security.

from flask_login import LoginManager, UserMixin, login_required

login_manager = LoginManager(app)
login_manager.login_view = 'login'

class User(UserMixin, db.Model):
    # ...

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

@app.route('/dashboard')
@login_required
def dashboard():
    # ...
 
 

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's user authentication and session management library flask-login, as well as detailed explanation of practical code cases" at the back of the public account CTO Plus.

11. Flask-OpenID

Flask-OpenID is a library for integrating OpenID authentication in Flask applications. OpenID is an authentication protocol that allows users to log in to different websites using an OpenID identity provider (such as Google, Facebook, etc.).

For specific usage, please pay attention to the detailed introduction of the article "Flask-OpenID, a library integrating OpenID for authentication in Flask applications" at the back of the public account CTO Plus.

12. Flask-RESTful

Flask-RESTful is an extension for building RESTful APIs and provides many useful functions such as request parsing, data validation, exception handling, etc. It provides a simple yet powerful set of tools for defining and working with API resources. Using Flask-RESTful, developers can easily create and manage API resources, handle HTTP requests and responses, and perform authentication and permission control. It provides various decorators and classes to make API development simple and efficient.

For specific usage, please pay attention to the detailed introduction of the article "Detailed Introduction to Flask Library Flask-RESTful for Building RESTful API" behind the public account CTO Plus.

13. Flask-Bootstrap

Flask-Bootstrap is a library for integrating the front-end Twitter Bootstrap framework in Flask applications. Bootstrap is a popular front-end framework that provides a set of easy-to-use and flexible CSS and JavaScript components for building responsive, modern web interfaces.

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to the library Flask-Bootstrap that integrates the front-end framework Bootstrap in the Flask project" behind the public account CTO Plus.

picture

14. Flask-Moment

Flask-Moment is an extension library for Flask that provides date and time processing functions for Flask applications. Flask-Moment is based on the Moment.js library, which helps us easily handle date and time on the front end. It provides some simple and easy-to-use functions such as formatting date and time, relative time and localization.

For specific usage, please pay attention to the detailed introduction of the article "Flask's date and time processing library Flask-Moment, and code practical cases" at the back of the public account CTO Plus.

15. Flask-Admin

The goal of Flask-Admin is to enable developers to quickly build powerful management interfaces. A library for generating backend management interfaces that can help you manage data and users easily without writing a lot of code. It provides some predefined views and operations, such as list view, edit view, delete view, filter view, etc.

In addition, Flask-Admin also supports custom views and operations to meet the needs of different applications. Provides a simple and extensible management interface framework, providing a web-based application management interface.

For specific usage, please pay attention to the detailed introduction of the article "Flask's background management library Flask-Admin, and code practical cases" at the back of the public account CTO Plus.

16. Flask-RESTX

Flask-RESTX is a Flask extension library for building RESTful APIs. It is an upgraded version of Flask-RESTful, providing more features and a better development experience. Flask-RESTX can help us quickly build an API that conforms to the RESTful design style, and provides some useful functions, such as request parameter parsing, response formatting, error handling, etc.

Flask-restfull is a framework for flask to develop API interfaces. Flask-RESTPlus is an upgraded version of Flask-restfull and can generate swagger online documentation. However, this project is no longer maintained and has been migrated to Flask-RESTX.

Flask-RESTX remains 100% compatible with the API of Flask-RESTPlus.

For specific usage, please pay attention to the detailed introduction of the article "Building Flask's RESTful API library Flask-RESTX, and code practical cases" at the back of the public account CTO Plus.

17. Flask-Principal

Flask-Principal is a Flask extension for role and permission management. It provides a simple and flexible way to manage users' roles and permissions, as well as permission control within applications. Flask-Principal is tightly integrated with the Flask-Login extension and can be used together with it to implement complete user authentication and authorization functions.

Flask-Principal provides the following functions and features:

1. Role management: You can define and manage user roles, including creating, modifying and deleting roles. Each role can have different permissions.

2. Permission management: User permissions can be defined and managed, including creation, modification and deletion permissions. Each permission can be associated with one or more roles.

3. Association of roles and permissions: Roles and permissions can be associated to facilitate permission control in applications. Whether a user can perform an operation can be determined based on the user's role and permissions.

4. Decorator support: Flask-Principal provides some decorators to easily protect view functions that require specific roles or permissions. Only users with appropriate roles or permissions can access protected pages.

5. Context management: Flask-Principal uses context management to manage user roles and permissions. The current user's roles and permissions can be obtained in the request context and processed accordingly.

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's roles, permissions and authorization library flask-principal, and detailed explanation of practical code cases" at the back of the public account CTO Plus.

18. Flask-Security

Flask-Security is a Flask-based user authentication and authorization extension library that simplifies tasks such as user authentication, role management, and permission control. Using Flask-Security, developers can easily add functions such as user registration, login, and password reset. They can also define user roles and permissions to achieve flexible permission control.

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's user authentication and authorization extension library Flask-Security, as well as detailed explanation of practical code cases" at the back of the public account CTO Plus.

19. Flask-marshmallow

Flask-Marshmallow is a library of Flask, a lightweight data format conversion module, serialization and deserialization module.

Used to simplify the process of data serialization and deserialization. It combines two powerful libraries, Flask and Marshmallow, to provide a simple and flexible way to handle data validation, serialization and deserialization.

It can be integrated into the project by installing flask-sqlalchemy and marshmallow-sqlalchemy, which are often used to convert complex ORM model objects to Python native data types.

For official documentation, please refer to: https://marshmallow.readthedocs.io/en/latest/

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-marshmallow, and detailed explanation of code practical cases" behind the public account CTO Plus.

20. Flask-PyMongo

Flask-PyMongo is a library for Flask that is used to connect to MongoDB databases and simplify the process of using MongoDB databases in Flask applications. It provides a simple and flexible way to connect and operate MongoDB database, and integrates seamlessly with Flask.

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-PyMongo, and detailed explanation of code practical cases" behind the public account CTO Plus.

picture

21. Flask-Caching

When your application slows down, consider adding caching. At least this is the easiest way to speed things up. What is the use of caching? Suppose there is a function that takes a long time, but the result returned by this function five minutes ago is still correct. Then we can consider storing the results of this function in the cache for a period of time.

Flask itself does not provide caching, but the Flask-Caching extension does. Flask-Caching supports a variety of backends, such as Redis, Memcached, etc., and can even support backends developed by you.

Flask-Cache is a caching Flask extension that provides caching functionality, which can help us easily add caching in Flask applications to improve application performance and response speed. Some calculation results in an application can be cached to reduce calculation volume and response time. Using Flask-Cache can improve the performance and responsiveness of your application.

For specific usage, please pay attention to the detailed introduction of the article "Cache the flask-caching library, one of the ways to speed up your application" behind the official account CTO Plus.

The basic use of Redis has been introduced in detail in the previous articles of the public account CTO Plus. For details, please refer to the article:

22. Flask-DebugToolbar

Flask-DebugToolbar is a toolbar library for Flask application debugging and optimization. It provides a visual interface for viewing and debugging all aspects of your application, including request and response details, database queries, template rendering, route matching, and more.

In this article, we will introduce the functions and usage of Flask-DebugToolbar in detail, and demonstrate its use through a practical code case. For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-DebugToolbar, and detailed explanation of practical code cases" behind the public account CTO Plus.

23. Flask-Assets

Flask-Assets is a library for managing and optimizing static assets such as CSS and JavaScript files. It can help developers better organize and handle static resources in Flask applications, improving web page loading speed and user experience.

In this article, we will introduce the functions and usage of Flask-Assets in detail, and demonstrate its use through a practical code case. For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-Assets, and detailed explanation of code practical cases" behind the public account CTO Plus.

24. Flask-Uploads

Flask-Uploads is a library for handling file uploads that can help developers easily handle file upload operations in Flask applications. It provides simple yet powerful functions such as file upload, file storage, and file download.

For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-Uploads, and detailed explanation of code practical cases" behind the public account CTO Plus.

25. Flask-Cors

Flask-Cors is a library for handling cross-origin resource sharing (CORS), which can help developers easily handle cross-origin requests in Flask applications. It provides simple yet powerful features such as configuration and handling of cross-domain requests.

In this article, we will introduce the functions and usage of Flask-Cors in detail, and demonstrate its use through a practical code case. For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-Cors, and detailed explanation of practical code cases" at the back of the public account CTO Plus.

26. Flask-SocketIO

Flask-SocketIO is a library for enabling real-time two-way communication in Flask applications. It is based on the WebSocket protocol and can help developers easily implement real-time update and push data functions in Flask applications.

In this article, we will introduce the functions and usage of Flask-SocketIO in detail, and demonstrate its use through a practical code case. For specific usage, please pay attention to the detailed introduction of the article "Detailed introduction to Flask's library Flask-SocketIO, and detailed explanation of code practical cases" behind the public account CTO Plus.

picture

The above are some commonly used Flask extension modules, which can help us implement certain functions or solve certain problems more conveniently. Of course, there are many other Flask extension modules that can be selected and used according to actual needs.

Summarize

Flask extensions provide developers with a wealth of features and tools that can greatly simplify the web application development process. In this article, we introduce some commonly used Flask extensions, including Flask-WTF, Flask-SQLAlchemy, Flask-Login, and Flask-Mail. These extensions provide functions such as form processing, database operations, user authentication, and email sending.

And Flask-PyMongo provides the function of using MongoDB database in Flask applications; Flask-Caching provides support for caching functions; Flask-DebugToolbar provides a convenient debugging toolbar; Flask-Assets provides management and management of static resources. Optimization function; Flask-Uploads provides support for file upload function; Flask-Cors provides support for cross-domain resource sharing; Flask-SocketIO provides support for WebSocket protocol. Enable developers to build powerful web applications more efficiently.

These extensions can help developers implement various functions more conveniently and improve application performance and user experience.

I hope this article can help you understand the importance and usage of Flask extensions, and play a role in actual projects. I wish you success in using Flask extensions!

If you have any other useful and practical Flask development extensions, please leave a message in the background to share.

For more exciting news, follow my official account and learn and grow together.

picture

 

Recommended reading:

 

Guess you like

Origin blog.csdn.net/zhouruifu2015/article/details/133387505