Generate Static Html With Flask Frozen
October 19, 2020Note
If you already have a Flask project which you want to convert to static html, Flask-Frozen is up to the task.
If you just want to generate static HTML, refer to Which Static Site Generator To Use.
The latest version is Version 0.15
(Jun 27, 2017
), but it generally works well.
Another issues that it is pretty slow as it run in sequential (about 1 page per second) and hardly utilize a single CPU fully. It might be possible to hack the code to utilize multiple CPU.
Install & Setup
pip install Frozen-Flask
Create static.py
# import flask app from your existing application
from application import app
from flask_frozen import Freezer
# optional
# from flask_minify import minify
# minify(app=app, html=True, caching_limit=0) # , js=True, cssless=True
# default to application root directory (same level as "templates") named "build"
FREEZER_DESTINATION = '../build'
# MimetypeMismatchWarning: Filename extension of u'new-york' (type application/octet-stream) does not match Content-Type: text/html; charset=utf-8
# If the urls are '/new-york', it will warn of MimetypeMismatchWarning
# You could either disable warning, or change path to '/new-york/', which generate 'new-york/index.html'
FREEZER_IGNORE_MIMETYPE_WARNINGS = True
# If run the static generation for 30 minutes and then it crashed, this will only generate files which are not generated yet (basically continue from last run)
# If you change the page code and the content is different, it will not regenerate the page
FREEZER_SKIP_EXISTING = True
# This is useful during development as you might want to remove certain @freezer.register_generator to speed up the process, but don't want those generated files to be deleted
FREEZER_REMOVE_EXTRA_FILES = False
# load the above FREEZER_ configurations
app.config.from_object(__name__)
freezer = Freezer(app, with_static_files=True, log_url_for=False, with_no_argument_rules=False)
@freezer.register_generator
def url_generator():
yield '/'
yield '/about'
@freezer.register_generator
def venues_generator():
from application.models import Venue
items = Venue.query(Venue.is_active == True)
for _item in items:
# yield f"/venues/{_item.url_name}"
yield 'venue.show', {'url_name': _item.url_name}
if __name__ == '__main__':
freezer.freeze()
python static.py
Flask Routing
If the route is @app.route('/about')
, it will generate about
file.
If the route is @app.route('/about/')
, it will generate about/index.html
file.
If the route @app.route('/about/privacy')
, you must change @app.route('/about')
to @app.route('/about/')
, as generated about
must be a directory in order for privacy
to be generated within it.
└── about
├── index.html
└── privacy
NOTE: You can use @app.route('/about/', strict_slashes=False)
for optional trailing slashes, and still generate about/index.html
.
References:
If you can't, do send some 💖 to @d_luaz or help to share this article.
Pixtory App (Alpha) - easily organize photos on your phone into a blog.
暖心芽 (WIP) 🌞❤️🌱 - reminder of hope, warmth, thoughts and feelings (or just quotes).
LuaPass - offline password manager
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn