Freezes a Flask application into a set of static files.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Primary functionality for converting Flask applications to static sites. The Freezer class orchestrates URL discovery, file generation, and build management with support for incremental builds and comprehensive error handling.
Main class for converting Flask applications to static websites. Manages the entire freezing process including URL discovery, file generation, and build output.
class Freezer:
def __init__(self, app=None, with_static_files=True,
with_no_argument_rules=True, log_url_for=True):
"""
Initialize Flask app freezer.
Parameters:
- app: Flask application instance (optional, can use init_app later)
- with_static_files: Whether to automatically generate URLs for static files
- with_no_argument_rules: Whether to automatically generate URLs for no-argument rules
- log_url_for: Whether to log url_for calls for URL generation
"""Methods for connecting the freezer to Flask applications.
def init_app(self, app):
"""
Register a Flask app after Freezer initialization.
Parameters:
- app: Flask application instance
"""Core methods for executing the static site generation process.
def freeze(self):
"""
Clean destination and build all URLs from generators.
Returns:
set: Set of URLs that were frozen
"""
def freeze_yield(self):
"""
Like freeze() but yields Page namedtuples while processing.
Yields:
Page: namedtuple with 'url' and 'path' fields for progress tracking
"""
def all_urls(self):
"""
Run all generators and yield URLs relative to app root.
Returns:
generator: URLs from all registered generators (testing utility)
Note: Does not generate pages, so url_for logged URLs not included
"""Properties for accessing build configuration and output locations.
@property
def root(self):
"""
Absolute path to the directory Frozen-Flask writes to.
Returns:
Path: Resolved FREEZER_DESTINATION configuration path
"""Methods for converting between URL paths and file system paths.
def urlpath_to_filepath(self, path):
"""
Convert URL path like /admin/ to file path like admin/index.html.
Parameters:
- path: URL path string
Returns:
str: Relative file path for the URL
"""Methods for serving frozen content during development and testing.
def serve(self, **options):
"""
Run HTTP server on the build result.
Parameters:
- **options: Passed to Flask.run()
"""
def run(self, **options):
"""
Call freeze() then serve() for build-and-serve workflow.
Parameters:
- **options: Passed to Flask.run()
"""
def make_static_app(self):
"""
Return Flask application serving the build destination.
Returns:
Flask: Application configured to serve static files
"""Data structure returned by freeze_yield() for tracking build progress.
Page = namedtuple('Page', 'url path')Fields:
url: The URL that was frozenpath: Path to generated file (relative to build root)from flask import Flask
from flask_frozen import Freezer
app = Flask(__name__)
freezer = Freezer(app)
# Freeze application
urls = freezer.freeze()
print(f"Generated {len(urls)} pages")import click
# Track freezing progress with click
with click.progressbar(
freezer.freeze_yield(),
item_show_func=lambda p: p.url if p else 'Done!') as pages:
for page in pages:
# Processing happens automatically
pass# Create freezer without app
freezer = Freezer()
# Initialize later
app = Flask(__name__)
freezer.init_app(app)
# Now can freeze
freezer.freeze()# Freeze and immediately serve for testing
freezer.run(debug=True, host='0.0.0.0', port=8000)Install with Tessl CLI
npx tessl i tessl/pypi-frozen-flask