tdewolff Minify

raw JSON →
2.24.12 verified Fri May 01 auth: no python

Python bindings for the Go tdewolff/minify library, providing fast minifiers for HTML, CSS, JS, JSON, SVG, and XML. Current version 2.24.12, released monthly.

pip install tdewolff-minify
error ModuleNotFoundError: No module named 'minify'
cause Using incorrect import path.
fix
Use 'from tdewolff_minify import minify' instead of 'import minify'.
error AttributeError: module 'tdewolff_minify' has no attribute 'CSSOptions'
cause Old version or misspelling. Ensure version >=2.24.0 and correct casing.
fix
pip install --upgrade tdewolff-minify and verify import: from tdewolff_minify import CSSOptions
error tdewolff_minify._minify.MinifyError: unsupported media type
cause Unrecognized mimetype or missing options for that type.
fix
Use one of: 'text/html', 'text/css', 'text/javascript', 'image/svg+xml', 'application/json', 'application/xml'.
deprecated CSSOptions.KeepCSS2 has been removed in v2.24.8; use CSSOptions.Version=2 instead.
fix Replace 'KeepCSS2=True' with 'Version=2' in CSSOptions.
gotcha The module name uses underscore: 'tdewolff_minify', not 'tdewolff-minify'.
fix Use 'pip install tdewolff-minify' for installation, but 'from tdewolff_minify import ...' in code.
breaking SVG options changed: SVG.Inline is now private; use CSSOptions for inline SVG.
fix Set CSSOptions.KeepInlineSVG=True instead of using SVG.Inline.

Basic CSS minification. Use 'text/html', 'text/javascript', etc. for other types.

import os
from tdewolff_minify import minify, CSSOptions

# Minify a CSS string
css_input = 'body { color: red; }'
options = CSSOptions()
result = minify('text/css', css_input, options=options)
print(result)
# Output: 'body{color:red}'