Media asset management for Python, with glue code for various web frameworks
npx @tessl/cli install tessl/pypi-webassets@3.0.0A comprehensive media asset management system for Python web development that enables developers to merge, compress, and optimize JavaScript and CSS files. WebAssets offers a flexible bundle-based architecture for defining asset groups, supports numerous built-in filters for processing assets, and integrates seamlessly with popular web frameworks through dedicated adapters.
pip install webassetspyyaml>=6.0.2, zope-dottedname>=6.0from webassets import Bundle, Environment
from webassets.filter import get_filter, register_filter
from webassets.loaders import YAMLLoader, PythonLoader, GlobLoaderFor command-line usage:
webassets buildfrom webassets import Environment, Bundle
# Create an environment (manages bundles and configuration)
env = Environment('./static', '/static')
# Define a JavaScript bundle
js_bundle = Bundle(
'jquery.js',
'app.js',
'utils.js',
filters='jsmin',
output='gen/packed.js'
)
# Define a CSS bundle
css_bundle = Bundle(
'style.css',
'layout.css',
filters='cssmin',
output='gen/packed.css'
)
# Register bundles with the environment
env.register('js_all', js_bundle)
env.register('css_all', css_bundle)
# Build bundles (creates optimized output files)
js_bundle.build()
css_bundle.build()
# Get URLs for templates
js_urls = js_bundle.urls()
css_urls = css_bundle.urls()WebAssets follows a modular architecture centered around three core concepts:
This design enables flexible asset pipelines where bundles can be nested arbitrarily, filters can be chained for complex transformations, and the entire system integrates cleanly with web frameworks through environment adapters.
Core functionality for creating, configuring, and managing asset bundles that group related files together for processing.
class Bundle:
def __init__(self, *contents, **options): ...
def build(self, force=None, output=None, disable_cache=None): ...
def urls(self, *args, **kwargs): ...
def bind(self, env): ...
def resolve_contents(self, ctx=None, force=False): ...
def resolve_output(self, ctx=None, version=None): ...
def get_version(self, ctx=None, refresh=False): ...
def id(self): ...
def iterbuild(self, ctx): ...
def get_all_bundle_files(bundle, ctx=None): ...Environment management for configuring asset processing, including directory paths, URL mapping, caching, and version management.
class Environment:
def __init__(self, directory=None, url=None, **more_config): ...
def register(self, name, *args, **kwargs): ...
def add(self, *bundles): ...
def append_path(self, path, url=None): ...
# Properties
@property
def load_path(self): ...
@property
def resolver(self): ...
@property
def updater(self): ...
@property
def versioner(self): ...
@property
def manifest(self): ...
@property
def cache(self): ...Comprehensive filter ecosystem for asset transformation including minification, compilation, preprocessing, and optimization.
class Filter:
def input(self, _in, out, **kw): ...
def output(self, _in, out, **kw): ...
def setup(self): ...
def get_filter(f, *args, **kwargs): ...
def register_filter(f): ...Built-in filters include JavaScript minifiers (JSMin, UglifyJS, Closure), CSS processors (CSSMin, CleanCSS, AutoPrefixer), and preprocessors (Sass, Less, TypeScript, Babel).
Flexible configuration loading from YAML files, Python modules, and programmatic sources.
class YAMLLoader:
def __init__(self, file_or_filename): ...
def load_bundles(self): ...
def load_environment(self): ...
class PythonLoader: ...
class GlobLoader: ...Advanced caching and versioning system for optimizing build performance and managing asset updates.
class FilesystemCache: ...
class MemoryCache: ...
def get_cache(cache_option, env=None): ...
class TimestampVersion: ...
class HashVersion: ...
class FileManifest: ...Comprehensive command-line tools for building assets, managing bundles, and integrating with deployment workflows.
class CommandLineEnvironment: ...
class BuildCommand: ...
def main(argv=None): ...Command-line usage:
webassets build
webassets watch
webassets cleanExtensions and adapters for integrating with popular Python web frameworks.
class AssetsExtension: ... # Jinja2 integration
class Jinja2Loader: ...
def assets(): ... # Template functionAuto-rebuild system that determines when bundles need rebuilding based on timestamps, bundle definitions, and dependencies.
class BaseUpdater:
def needs_rebuild(self, bundle, ctx): ...
def build_done(self, bundle, ctx): ...
class TimestampUpdater(BaseUpdater):
id = 'timestamp'
def check_timestamps(self, bundle, ctx, o_modified=None): ...
class AlwaysUpdater(BaseUpdater):
id = 'always'
def get_updater(updater_id): ...Core functionality for managing asset content through data abstractions (hunks) and filter application tools.
class BaseHunk:
def data(self): ...
def save(self, filename): ...
class FileHunk(BaseHunk): ...
class UrlHunk(BaseHunk): ...
class MemoryHunk(BaseHunk): ...
class FilterTool:
def apply(self, hunk, filters, type, kwargs=None): ...
def apply_func(self, filters, type, args, kwargs=None, cache_key=None): ...
def merge(hunks, separator=None): ...
def merge_filters(filters1, filters2): ...Essential utility functions including path operations, context managers, object resolution, and security features.
def hash_func(data): ...
def common_path_prefix(paths, sep=os.path.sep): ...
def is_url(s): ...
@contextlib.contextmanager
def working_directory(directory=None, filename=None): ...
def make_option_resolver(clazz=None, attribute=None, classes=None, allow_none=True, desc=None): ...
def RegistryMetaclass(clazz=None, attribute=None, allow_none=True, desc=None): ...
def cmp_debug_levels(level1, level2): ...
def calculate_sri(data): ...
def calculate_sri_on_file(file_name): ...class BundleError(Exception): ...
class BuildError(BundleError): ...
class FilterError(BuildError): ...
class RegisterError(Exception): ...
class EnvironmentError(Exception): ...
class VersionIndeterminableError(Exception): ...
class LoaderError(Exception): ...
class CommandError(Exception): ...
class ImminentDeprecationWarning(Warning): ...