The integration practice of Flask-Assets and Flask-Cache
Integration practice of Flask-Assets and Flask-Cache
When developing web applications, it is often necessary to handle the management and cache of static resources.Flask-Assets and Flask-Cache are two very useful Flask extensions that can help us manage static resources and cache data.This article will introduce how to integrate these two extensions into Flask applications and give a complete programming code and related configuration.
First, we need to install Flask-Assets and Flask-Cache extensions.Use the following commands to install them:
shell
$ pip install Flask-Assets Flask-Cache
After the installation is completed, we can start configured these two extensions.
Flask-Assets configuration:
In the configuration of the Flask application, we need to add the following configuration items to configure Flask-Assets:
python
# FLASK-AsSETS Configuration
Assets_debug = false # Whether to turn on the debug mode
Assets_auto_build = False # Whether to automatically build resources automatically
Assets_files = ['STATIC/Style.css', 'Static/Scripts.js'] # Resource file list
# Resource bundles configuration
ASSETS_BUNDLES = {
'css_bundle': Bundle(
'css/bootstrap.min.css',
'css/style.css',
filters='cssmin',
output='gen/packed.min.css'
),
'js_bundle': Bundle(
'js/jquery.min.js',
'js/scripts.js',
filters='jsmin',
output='gen/packed.min.js'
)
}
In the above configuration, `Assets_debug` is used to set whether to turn on the debug mode.When the debugging mode is turned on, each resource will be loaded separately for easy debugging.In the production environment, we usually set it to false.When the assets_auto_build` is set to true, Flask-Assets will automatically build resources in each request and apply to the development environment.`Assets_files` is used to specify a list of static resource files that need to be managed.
`Assets_bundles' configuration items are used to define the resource Bundles.Each Bundle is a set of related resource files that will be merged and compressed and output to the specified file (output).As an example, we define a bundle called `css_bundle` and a bundle called` js_bundle`, including a set of CSS files and a set of JS files, respectively.We can quote them through the name of Bundle.
Flask-Cache configuration:
The configuration Flask-Cache is relatively simple.We only need to add one to the configuration:
python
# Flash-cache configuration
Cache_type = 'Simple' # cache type, here uses SIMPLE type
In this example, we set the cache type to `Simple`, which is a simple memory cache implementation.
Next, we need to initialize the two extensions in our Flask application.
python
from flask import Flask
from flask_assets import Environment
from flask_cache import Cache
app = Flask(__name__)
assets = Environment(app)
cache = Cache(app)
In the above code, we introduced Flask-Assets and Flask-Cache extensions.We then created ASSETS and Cache objects for applications.In this way, we can use these two objects to manage static resources and cache data.
Next, we can write some code to use Flask-Assets and Flask-Cache.
This is an example of using Flask-Assets for loading CSS and JS files:
html
<!DOCTYPE html>
<html>
<head>
{% assets "css_bundle" %}
<link href="{{ ASSET_URL }}" rel="stylesheet">
{% endassets %}
{% assets "js_bundle" %}
<script src="{{ ASSET_URL }}"></script>
{% endassets %}
</head>
<body>
<!-page content->
</body>
</html>
In this example, we use the `Assets` template label provided by Flask-Assets to load CSS and JS files.By specifying the name of the bundle, Flask-Assets will inject the corresponding merger and compressed file path (`asset_url`) into the template.
Below is an example of using Flask-Cache to return the result of the cache function:
python
from flask import render_template
from flask_cache import Cache
@app.route('/')
@cache.cached(timeout=60)
def index():
# Some computing dense operations
return render_template('index.html')
In this example, we use the back result of the return result of `@cache.cached` to cache the back of the` index` function.The `timeout` parameter specifies the cache expiration time, which is set to 60 seconds here.Within 60 seconds, if the `Index` function is requested again, Flask-Cache will directly return the result of the cache, thereby increasing the response speed.
So far, we have completed the integration of Flask-Assets and Flask-Cache.By managing static resources and cache data reasonably, we can improve the performance and user experience of Web applications.
I hope this article will help you understand the integration practice of Flask-Assets and Flask-Cache.If you want to know more about these two extensions, please refer to the official documentation.