Registry / serialization / minify-html

minify-html

JSON →
library0.18.1pypypi✓ verified 23d ago

minify-html is a Python binding for an extremely fast and smart HTML + JS + CSS minifier written in Rust. It offers advanced minification strategies that outperform other minifiers in both speed and effectiveness, handling invalid HTML and templating syntax. The library is actively maintained with frequent minor releases, often multiple times a month, and is currently at version 0.18.1.

pip install minify-html
INSTALL
IMPORT
SIG · MINIFY-HTML
M
minify-html
serializationpythonv0.18.1
Install
1.7s avg
Import
10ms
Disk
33MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.18.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 35.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.010s · 35MB
33MB installed
● package 33MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

minify
from minify_html import minify
from minify_html import MinifyHtml
The Python binding exposes a direct `minify` function, unlike some other language bindings that might use a class or differently cased function name.

Demonstrates how to minify an HTML string, including inline CSS and JavaScript, while configuring options like comment removal and doctype preservation.

from minify_html import minify html_input = """ <!DOCTYPE html> <html> <head> <title> Test Page </title> <style> body { background-color: #f0f0f0; } h1 { color: blue; } </style> </head> <body> <!-- Main Content --> <h1> Hello, World! </h1> <p> This is a <span> test </span> paragraph. </p> <script> // Some JavaScript function greet() { console.log("Welcome!"); } greet(); </script> </body> </html> """ minified_html = minify( html_input, keep_comments=False, minify_css=True, minify_js=True, do_not_minify_doctype=True ) print(minified_html)
Debug
Known issues
gotchaWhen configuring minification options, the Python binding uses `snake_case` keyword arguments (e.g., `keep_comments=False`). Users accustomed to other language bindings (like Java's `setKeepComments(true)`) or older documentation might incorrectly try `camelCase` or `set_` prefixed methods, which will result in a `TypeError`. Always refer to the Python API documentation for the exact parameter names.
fix
Use `snake_case` keyword arguments directly when calling the `minify` function (e.g., `minify(html, keep_comments=False)`).
affects: All versions
gotchaEnabling JavaScript (`minify_js=True`) or CSS (`minify_css=True`) minification, particularly when building the package from source or in specific environments, might require the Go compiler to be installed. This is because the underlying Rust library leverages `esbuild` for JS/CSS minification via its `js-esbuild` feature. Pre-built wheels typically include this functionality, but custom installations might fail if Go is not present in the environment.
fix
Ensure the Go compiler is installed in your environment if you encounter issues with JS/CSS minification during a custom build or non-standard installation. For typical usage with pre-built wheels, this is generally not an issue.
affects: All versions (especially for source builds)
gotchaThe `minify-html` library employs an advanced, context-aware whitespace minification strategy. While this is generally highly effective, in specific edge cases where precise whitespace rendering is critical (e.g., between certain inline elements or custom components), it might remove more whitespace than anticipated.
fix
Review the available configuration options such as `keep_spaces_between_attributes` or `keep_html_and_head_opening_tags` to fine-tune whitespace preservation if unexpected rendering issues occur. Test minified output thoroughly in your target browsers.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'minify_html'
The 'minify-html' library is not installed in the Python environment where the code is being executed.
fix
Install the library using pip: `pip install minify-html`
AttributeError: module 'minify_html' has no attribute 'minify'
The `minify` function is being called incorrectly. The module itself is imported as `minify_html`, and the minification function is a method of this module, not the module itself directly callable.
fix
Ensure you are calling the `minify` method on the imported module: `import minify_html; minified_html = minify_html.minify(html_content_string)`
AssertionError: Cargo, the Rust package manager, is not installed or is not on PATH.
The `minify-html` library is a Python binding for a Rust library and requires the Rust toolchain (including Cargo) to be installed and available in the system's PATH during its installation process.
fix
Install Rust and Cargo using the official `rustup` script (`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`) or your system's package manager, then retry `pip install minify-html`.
Minified HTML renders incorrectly / Error: No space between attributes.
Aggressive minification settings, particularly those related to whitespace removal between attributes or entity encoding, can sometimes lead to HTML output that, while often tolerated by browsers, might be flagged by validators or cause subtle rendering issues.
fix
Adjust minification options to be less aggressive. For example, explicitly keep spaces between attributes or disable optimal entity encoding: `minify_html.minify(html_string, keep_spaces_between_attributes=True, allow_optimal_entities=False)`
Upgrade
Version history
0.18.1latest on PyPI · released Oct 25, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
minify-html — pip install minify-html · libregistry