How to use Flask-Assets for static resource version control (How to use Flask-Assets for Static Resource Version Control)

How to use Flask-Assets for static resource version control When developing web applications, the version control of static resources (such as CSS and JavaScript files) is an important aspect.By controlling static resources, it can ensure that users always use new resource files after the website is updated without being affected by the old cache files.Flask-Assets is a powerful Python library that helps us manage and optimize static resources, and provide a simple way to perform version control. This article will introduce how to use Flask-Assets to control static resources. Install Flask-Assets First, you need to ensure that the Flask-Assets library has been installed.You can use the Python package management tool PIP to install Flask-Assets.Open the command line terminal and execute the following command: pip install flask-assets Configure Flask-Assets Before the beginning, Flask-Assets needs to be configured to adapt to our project.Add the following configuration to the configuration file of the Flask application: from flask_assets import Environment, Bundle # Create FLASK-AsSETS Environment assets = Environment(app) # Configure the output directory of static resources assets.output_folder = 'static/dist' # Declaration required the resources controlled by the version css = Bundle('css/style.css', filters='cssmin', output='dist/css/style.min.css') js = Bundle('js/main.js', filters='jsmin', output='dist/js/main.min.js') # Registered resource beam assets.register('css', css) assets.register('js', js) In the above configuration, the required classes and modules were first introduced, and a Flask-Assets object named Assets was created.Then set the output directory of static resources to 'STATIC/DIST'.Next, a bundle object called CSS and JS is used to declare CSS and JavaScript resources that require version control.By specifying the Filters parameter, compression can be processed (such as CSSMIN and JSmin).Finally, by calling the register method, the resource beam is registered in the Flask-Assets environment. In the place where static resources are used in the Flask application, you can use the following code to load the registered resources into the template: {% assets "css" %} <link rel="stylesheet" href="{{ ASSET_URL }}"> {% endassets %} {% assets "js" %} <script src="{{ ASSET_URL }}"></script> {% endassets %} In the above code, the { % assets %} tag is used to load CSS and JavaScript resources.According to the defined resource names (such as "CSS" and "JS"), Flask-Assets will automatically generate compressed files with a unique version number in Output_folder and store its path in variable asset_url.By assigning the generated path to HREF or SRC attributes, static resources controlled with versions can be loaded into the template. Through the above steps, you can use Flask-Assets to control static resources.Whenever static resources change, Flask-Assets automatically generates compressed resource files with a new version number.This ensures that when each application update occurs, users will get new resource files to avoid the problem of the browser cache old files. Complete example code from flask import Flask, render_template from flask_assets import Environment, Bundle app = Flask(__name__) app.config['SECRET_KEY'] = 'secretkey' # Create FLASK-AsSETS Environment assets = Environment(app) # Configure the output directory of static resources assets.output_folder = 'static/dist' # Declaration required the resources controlled by the version css = Bundle('css/style.css', filters='cssmin', output='dist/css/style.min.css') js = Bundle('js/main.js', filters='jsmin', output='dist/js/main.min.js') # Registered resource beam assets.register('css', css) assets.register('js', js) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True) In the above code, a simple FLASK application is created.Returns a template called 'Index.html' on the root routing ('/'), which can load static resources controlled by Flask-Assets version. in conclusion This article introduces how to use Flask-Assets for static resource version control.Through Flask-Assets, we can easily manage and optimize static resources and ensure that users always use the latest resource files.Through reasonable configuration Flask-Assets and loading the generated resource path in the template, the trouble of static resource version control can be eliminated.