htmlmin2
An HTML Minifier, `htmlmin2` is a configurable HTML minifier with safety features. It is a fork of the `htmlmin` library, created to address compatibility issues with modern Python versions, particularly Python 3.13+. The current version is 0.1.13, released in March 2023. While functional and widely used, its release cadence is currently stalled.
Warnings
- gotcha The package is installed via `pip install htmlmin2`, but the main functionality is imported from the `htmlmin` module (e.g., `from htmlmin import minify`). This is due to `htmlmin2` being a fork that retained the original module name.
- breaking Users on Python 3.13 or newer *must* use `htmlmin2` because the original `htmlmin` library relies on the deprecated `cgi` module which was removed in Python 3.13. Attempting to use the original `htmlmin` will result in import errors.
- gotcha Aggressive whitespace removal options, such as `remove_empty_space` and `remove_all_empty_space`, can lead to unintended changes in HTML rendering or JavaScript behavior, especially with inline elements or if client-side scripts depend on specific whitespace patterns. Use with caution.
- gotcha While `htmlmin2` addresses compatibility issues with modern Python, its PyPI metadata indicates a 'Stalled' release cadence. The `Programming Language :: Python :: 3.2` classifier on PyPI is outdated; `htmlmin2` is actively used and intended for newer Python versions (3.10+).
Install
-
pip install htmlmin2
Imports
- minify
from htmlmin import minify
Quickstart
from htmlmin import minify
html_input = """
<html>
<head>
<title> Hello, World! </title>
</head>
<body>
<p> How are <em>you</em> doing? </p>
</body>
</html>
"""
minified_html = minify(html_input)
print(minified_html)