Flask-Assets

2.1.0 · active · verified Thu Apr 16

Flask-Assets (current version 2.1.0) provides asset management for Flask applications, integrating the `webassets` library to efficiently merge, minify, and compile CSS and JavaScript files. It simplifies frontend workflow by handling static resource optimization, with new versions released periodically to maintain compatibility with Flask and other dependencies.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `Flask-Assets` to manage CSS and JavaScript files. It initializes an `Environment`, defines `Bundle` objects for minifying and combining static files, registers them, and shows how to include these bundles in a Jinja2 template. Run the script, then navigate to `http://127.0.0.1:5000/` in your browser. The static files will be automatically generated in `static/gen`.

from flask import Flask, render_template
from flask_assets import Environment, Bundle
import os

app = Flask(__name__)

# Configure Flask for assets
app.config['ASSETS_DEBUG'] = True # Set to False in production

# Initialize Flask-Assets
assets = Environment(app)

# Define asset bundles
js_bundle = Bundle(
    'js/main.js',
    'js/utils.js',
    filters='jsmin',
    output='gen/packed.js'
)
css_bundle = Bundle(
    'css/style.css',
    'css/theme.css',
    filters='cssmin',
    output='gen/packed.css'
)

# Register bundles
assets.register('main_js', js_bundle)
assets.register('main_css', css_bundle)

@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    # Create static/js and static/css directories for the example
    os.makedirs(os.path.join(app.static_folder, 'js'), exist_ok=True)
    os.makedirs(os.path.join(app.static_folder, 'css'), exist_ok=True)
    
    # Create dummy asset files
    with open(os.path.join(app.static_folder, 'js', 'main.js'), 'w') as f:
        f.write('console.log("Main JS loaded");')
    with open(os.path.join(app.static_folder, 'js', 'utils.js'), 'w') as f:
        f.write('function utility() { return "utility"; }')
    with open(os.path.join(app.static_folder, 'css', 'style.css'), 'w') as f:
        f.write('body { font-family: sans-serif; }')
    with open(os.path.join(app.static_folder, 'css', 'theme.css'), 'w') as f:
        f.write('h1 { color: #333; }')

    # Create a simple template (templates/index.html)
    template_content = '''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flask-Assets Example</title>
    {% assets "main_css" %}
        <link rel="stylesheet" type="text/css" href="{{ ASSET_URL }}">
    {% endassets %}
</head>
<body>
    <h1>Hello from Flask-Assets!</h1>
    {% assets "main_js" %}
        <script type="text/javascript" src="{{ ASSET_URL }}"></script>
    {% endassets %}
</body>
</html>
    '''
    os.makedirs('templates', exist_ok=True)
    with open('templates/index.html', 'w') as f:
        f.write(template_content)

    app.run(debug=True)

view raw JSON →