Radically simplified static file serving for WSGI applications
WhiteNoise provides comprehensive file compression functionality supporting both gzip and Brotli formats. The compression system intelligently determines which files to compress and creates compressed variants that are automatically served based on client Accept-Encoding headers.
The main compression utility that handles both gzip and Brotli compression with intelligent compression decisions and concurrent processing.
class Compressor:
"""
File compression utility for static files.
Supports gzip and Brotli compression with intelligent effectiveness
testing and concurrent processing for performance.
"""
def __init__(
self,
extensions=None,
use_gzip=True,
use_brotli=True,
log=print,
quiet=False
):
"""
Initialize compressor.
Parameters:
- extensions: File extensions to skip (defaults to SKIP_COMPRESS_EXTENSIONS)
- use_gzip: Enable gzip compression
- use_brotli: Enable Brotli compression (requires brotli package)
- log: Logging function (defaults to print)
- quiet: Suppress logging output
"""
def should_compress(self, filename):
"""
Check if file should be compressed based on extension.
Parameters:
- filename: File name to check
Returns:
- bool: True if file should be compressed
"""
def compress(self, path):
"""
Compress file with available compression formats.
Parameters:
- path: Absolute path to file to compress
Returns:
- list[str]: List of created compressed file paths
Creates compressed variants (.gz, .br) only if compression
reduces file size by at least 5%.
"""
@staticmethod
def compress_gzip(data):
"""
Compress data using gzip (static method).
Parameters:
- data: bytes to compress
Returns:
- bytes: Gzip compressed data
Uses maximum compression level and removes timestamp for
deterministic output.
"""
@staticmethod
def compress_brotli(data):
"""
Compress data using Brotli (static method).
Parameters:
- data: bytes to compress
Returns:
- bytes: Brotli compressed data
Requires brotli package to be installed.
"""
def is_compressed_effectively(self, encoding_name, path, orig_size, data):
"""
Check if compression is effective (reduces size by >5%).
Parameters:
- encoding_name: Name of compression format for logging
- path: File path for logging
- orig_size: Original file size in bytes
- data: Compressed data
Returns:
- bool: True if compression is effective
"""
def write_data(self, path, data, suffix, stat_result):
"""
Write compressed data to file with preserved timestamps.
Parameters:
- path: Original file path
- data: Compressed data bytes
- suffix: File suffix (.gz or .br)
- stat_result: os.stat result for timestamp preservation
Returns:
- str: Path to created compressed file
"""
@staticmethod
def get_extension_re(extensions):
"""
Build regex pattern for file extension matching.
Parameters:
- extensions: Iterable of file extensions to match
Returns:
- re.Pattern: Compiled regex for extension matching
"""
# Default extensions to skip compression
SKIP_COMPRESS_EXTENSIONS: tuple[str, ...] = (
# Images
"jpg", "jpeg", "png", "gif", "webp",
# Compressed files
"zip", "gz", "tgz", "bz2", "tbz", "xz", "br",
# Flash
"swf", "flv",
# Fonts
"woff", "woff2",
# Video
"3gp", "3gpp", "asf", "avi", "m4v", "mov", "mp4", "mpeg", "mpg", "webm", "wmv"
)WhiteNoise provides a command-line tool for batch compression of static files.
def main(argv=None):
"""
Command-line interface for file compression.
Parameters:
- argv: Command line arguments (defaults to sys.argv)
Returns:
- int: Exit code (0 for success)
Usage:
python -m whitenoise.compress [options] <root> [extensions...]
Arguments:
- root: Directory to search for files
- extensions: File extensions to exclude (defaults to SKIP_COMPRESS_EXTENSIONS)
Options:
-q, --quiet: Don't produce log output
--no-gzip: Don't produce gzip '.gz' files
--no-brotli: Don't produce brotli '.br' files
"""from whitenoise.compress import Compressor
# Create compressor with default settings
compressor = Compressor()
# Compress a single file
compressed_files = compressor.compress('/path/to/style.css')
# Creates /path/to/style.css.gz and /path/to/style.css.br (if effective)
# Check if file should be compressed
if compressor.should_compress('script.js'):
compressed_files = compressor.compress('/path/to/script.js')# Skip additional extensions and disable Brotli
compressor = Compressor(
extensions=['jpg', 'png', 'pdf', 'doc'], # Custom skip list
use_gzip=True,
use_brotli=False, # Disable Brotli
quiet=True # Suppress logging
)
# Custom logging function
def custom_logger(message):
print(f"[COMPRESS] {message}")
compressor = Compressor(log=custom_logger)import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from whitenoise.compress import Compressor
def compress_directory(root_path):
"""Compress all eligible files in directory."""
compressor = Compressor()
with ThreadPoolExecutor() as executor:
futures = []
for dirpath, dirs, files in os.walk(root_path):
for filename in files:
if compressor.should_compress(filename):
filepath = os.path.join(dirpath, filename)
future = executor.submit(compressor.compress, filepath)
futures.append(future)
# Wait for completion and handle any errors
for future in as_completed(futures):
try:
compressed_files = future.result()
print(f"Compressed: {compressed_files}")
except Exception as e:
print(f"Compression error: {e}")# Compress all files in static directory
python -m whitenoise.compress /path/to/static
# Compress with custom excluded extensions
python -m whitenoise.compress /path/to/static pdf doc
# Quiet mode with only gzip
python -m whitenoise.compress --quiet --no-brotli /path/to/static
# Help
python -m whitenoise.compress --help# build.py - Build script integration
from whitenoise.compress import Compressor
import os
def build_static_files():
"""Build and compress static files."""
static_root = '/var/www/static'
# Run your static file collection/build process
os.system('npm run build') # or equivalent
# Compress generated files
compressor = Compressor(quiet=False)
for dirpath, dirs, files in os.walk(static_root):
for filename in files:
if compressor.should_compress(filename):
filepath = os.path.join(dirpath, filename)
compressed = compressor.compress(filepath)
if compressed:
print(f"Compressed {filename} -> {compressed}")
if __name__ == '__main__':
build_static_files()gzip modulebrotli package installationCompression is only applied if it reduces file size by more than 5%. This prevents:
Files are automatically excluded from compression if they have extensions indicating they're already compressed or binary formats that don't compress well:
from typing import Callable, Optional
import argparse
# Logging function type
LogFunction = Callable[[str], None]
# Command line arguments type
Args = argparse.Namespace
# Compression result type
CompressionResult = list[str] # List of compressed file pathsInstall with Tessl CLI
npx tessl i tessl/pypi-whitenoise