0
# Core Freezing Operations
1
2
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.
3
4
## Capabilities
5
6
### Freezer Class
7
8
Main class for converting Flask applications to static websites. Manages the entire freezing process including URL discovery, file generation, and build output.
9
10
```python { .api }
11
class Freezer:
12
def __init__(self, app=None, with_static_files=True,
13
with_no_argument_rules=True, log_url_for=True):
14
"""
15
Initialize Flask app freezer.
16
17
Parameters:
18
- app: Flask application instance (optional, can use init_app later)
19
- with_static_files: Whether to automatically generate URLs for static files
20
- with_no_argument_rules: Whether to automatically generate URLs for no-argument rules
21
- log_url_for: Whether to log url_for calls for URL generation
22
"""
23
```
24
25
### Application Initialization
26
27
Methods for connecting the freezer to Flask applications.
28
29
```python { .api }
30
def init_app(self, app):
31
"""
32
Register a Flask app after Freezer initialization.
33
34
Parameters:
35
- app: Flask application instance
36
"""
37
```
38
39
### Freezing Operations
40
41
Core methods for executing the static site generation process.
42
43
```python { .api }
44
def freeze(self):
45
"""
46
Clean destination and build all URLs from generators.
47
48
Returns:
49
set: Set of URLs that were frozen
50
"""
51
52
def freeze_yield(self):
53
"""
54
Like freeze() but yields Page namedtuples while processing.
55
56
Yields:
57
Page: namedtuple with 'url' and 'path' fields for progress tracking
58
"""
59
60
def all_urls(self):
61
"""
62
Run all generators and yield URLs relative to app root.
63
64
Returns:
65
generator: URLs from all registered generators (testing utility)
66
67
Note: Does not generate pages, so url_for logged URLs not included
68
"""
69
```
70
71
### Build Properties
72
73
Properties for accessing build configuration and output locations.
74
75
```python { .api }
76
@property
77
def root(self):
78
"""
79
Absolute path to the directory Frozen-Flask writes to.
80
81
Returns:
82
Path: Resolved FREEZER_DESTINATION configuration path
83
"""
84
```
85
86
### File Path Operations
87
88
Methods for converting between URL paths and file system paths.
89
90
```python { .api }
91
def urlpath_to_filepath(self, path):
92
"""
93
Convert URL path like /admin/ to file path like admin/index.html.
94
95
Parameters:
96
- path: URL path string
97
98
Returns:
99
str: Relative file path for the URL
100
"""
101
```
102
103
### Development Server
104
105
Methods for serving frozen content during development and testing.
106
107
```python { .api }
108
def serve(self, **options):
109
"""
110
Run HTTP server on the build result.
111
112
Parameters:
113
- **options: Passed to Flask.run()
114
"""
115
116
def run(self, **options):
117
"""
118
Call freeze() then serve() for build-and-serve workflow.
119
120
Parameters:
121
- **options: Passed to Flask.run()
122
"""
123
124
def make_static_app(self):
125
"""
126
Return Flask application serving the build destination.
127
128
Returns:
129
Flask: Application configured to serve static files
130
"""
131
```
132
133
## Data Types
134
135
### Page Namedtuple
136
137
Data structure returned by `freeze_yield()` for tracking build progress.
138
139
```python { .api }
140
Page = namedtuple('Page', 'url path')
141
```
142
143
Fields:
144
- `url`: The URL that was frozen
145
- `path`: Path to generated file (relative to build root)
146
147
## Usage Examples
148
149
### Basic Freezing
150
151
```python
152
from flask import Flask
153
from flask_frozen import Freezer
154
155
app = Flask(__name__)
156
freezer = Freezer(app)
157
158
# Freeze application
159
urls = freezer.freeze()
160
print(f"Generated {len(urls)} pages")
161
```
162
163
### Progress Tracking
164
165
```python
166
import click
167
168
# Track freezing progress with click
169
with click.progressbar(
170
freezer.freeze_yield(),
171
item_show_func=lambda p: p.url if p else 'Done!') as pages:
172
for page in pages:
173
# Processing happens automatically
174
pass
175
```
176
177
### Deferred Initialization
178
179
```python
180
# Create freezer without app
181
freezer = Freezer()
182
183
# Initialize later
184
app = Flask(__name__)
185
freezer.init_app(app)
186
187
# Now can freeze
188
freezer.freeze()
189
```
190
191
### Build and Serve
192
193
```python
194
# Freeze and immediately serve for testing
195
freezer.run(debug=True, host='0.0.0.0', port=8000)
196
```