Flask-Minify


NameFlask-Minify JSON
Version 0.42 PyPI version JSON
download
home_pagehttps://github.com/mrf345/flask_minify/
SummaryFlask extension to minify html, css, js and less.
upload_time2023-05-07 11:32:41
maintainer
docs_urlNone
authorMohamed Feddad
requires_python
licenseMIT
keywords flask extension minifer htmlmin lesscpy jsmin html js less css
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <h1 align='center'> flask_minify </h1>
<p align='center'>
<a href='https://pypi.org/project/Flask-Minify/'>
    <img src='https://img.shields.io/github/v/tag/mrf345/flask_minify' alt='Latest Release' />
</a>
<a href='https://github.com/mrf345/flask_minify/actions/workflows/ci.yml'>
  <img src='https://github.com/mrf345/flask_minify/workflows/Build/badge.svg'>
</a>
<br />
<img src='https://img.shields.io/pypi/pyversions/flask_minify' alt='Supported versions' />
<br />
<a href='https://github.com/mrf345/flask_minify/actions/workflows/ci.yml'>
  <img src='https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/mrf345/bc746d7bfe356b54fbb93b2ea5d0d2a4/raw/flask_minify__heads_master.json' alt='Coverage Percentage' />
</a>
<a href='https://github.com/PyCQA/bandit'>
  <img src='https://img.shields.io/badge/security-bandit-yellow.svg' alt='security: bandit' />
</a>
<a href='https://github.com/psf/black'>
    <img src='https://img.shields.io/badge/style-black-000000.svg' alt='Code Style Black' />
</a>
<br />
</p>

<h3 align='center'>Flask extension to parse and minify html, javascript, css and less.</h3>

## Install:

With **pip**

- `pip install Flask-Minify`

*Or* from the source

- `git clone https://github.com/mrf345/flask_minify.git`
- `cd flask_minify`
- `python setup.py install`

## Setup:

In this example the  extension will minify every HTML request, unless it's explicitly bypassed.

```python
from flask import Flask
from flask_minify import Minify

app = Flask(__name__)
Minify(app=app, html=True, js=True, cssless=True)
```

Another approach is using **decorators**, you can set the extension to be `passive` so will only minify the decorated routes

```python
from flask import Flask
from flask_minify import Minify, decorators as minify_decorators

app = Flask(__name__)
Minify(app=app, passive=True)

@app.route('/')
@minify_decorators.minify(html=True, js=True, cssless=True)
def example():
  return '<h1>Example...</h1>'
```

## Options:


Option             | type     | Description
-------------------|----------|-------------
 app               | `object` | `Flask` app instance to be passed (default: `None`)
 html              | `bool`   | minify HTML (default: `True`)
 js                | `bool`   | minify JavaScript output (default: `True`)
 cssless           | `bool`   | minify CSS or Less. (default: `True`)
 fail_safe         | `bool`   | avoid raising error while minifying (default: `True`)
 bypass            | `list`   | endpoints to bypass minifying for, supports `Regex` (default: `[]`)
 bypass_caching    | `list`   | endpoints to bypass caching for, supports `Regex` (default: `[]`)
 caching_limit     | `int`    | limit the number of cached response variations (default: `2`).
 passive           | `bool`   | disable active minifying, to use *decorators* instead (default: `False`)
 static            | `bool`   | enable minifying static files css, less and js (default: `True`)
 script_types      | `list`   | script types to limit js minification to (default: `[]`)
 parsers           | `dict`   | parsers to handle minifying specific tags, mainly for advanced customization (default: `{}`)


#### - `bypass` and `bypass_caching`

`endpoint` in this context is the name of the function decorated with `@app.route`
so in the following example the endpoint will be `root`:

```python
@app.route('/root/<id>')
def root(id):
    return id
```

both options can handle regex patterns as input so for example, if you want to bypass all routes on a certain blueprint
you can just pass the pattern as such:

```python
Minify(app, bypass=['blueprint_name.*'])
```

#### - `caching_limit`

if the option is set to `0`, we'll not cache any response, so if you want to **disable caching** just do that.


#### - `script_types`

when using the option include `''` (empty string) in the list to include script blocks which are missing the `type` attribute.

#### - `parsers`

allows passing tag specific options to the module responsible for the minification, as well as replacing the default parser with another included option or your own custom one.

In the following example will replace the default `style` (handles CSS) parser `rcssmin` with `lesscpy`:

```python
from flask_minify import Minify, parsers as minify_parsers

parsers = {'style': minify_parsers.Lesscpy}

Minify(app=app, parsers=parsers)
```

you can override the default parser runtime options as well, as shown in the following example:

```python
from flask_minify import Minify, parsers as minify_parsers

class CustomCssParser(minify_parsers.Lesscpy):
    runtime_options = {
        **minify_parsers.Lesscpy.runtime_options,
        "xminify": False,
    }

parsers = {'style': CustomCssParser}

Minify(app=app, parsers=parsers)
```

the **default** parsers are set to `{"html": Html, "script": Jsmin, "style": Rcssmin}` check out [the code](https://github.com/mrf345/flask_minify/blob/master/flask_minify/parsers.py) for more insight.


## Development:

- *Tests*: `make test`
- *Style check*: `make lint`
- *Format code*: `make format`

## Breaking changes

#### `0.40`

Due to a future deprecation in Flask 2.3, the extension is no longer going to fallback to `Flask._app_ctx_stack`, it will raise an exception instead (`flask_minify.exceptions.MissingApp`)

#### `0.33`

introduces a breaking change to the expected output, in this release `lesscpy` will be replaced by `cssmin` as
the default css minifier so no more `less` compiling by default. in case you don't want that, follow [this example](https://github.com/mrf345/flask_minify#--parsers). 

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mrf345/flask_minify/",
    "name": "Flask-Minify",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "flask,extension,minifer,htmlmin,lesscpy,jsmin,html,js,less,css",
    "author": "Mohamed Feddad",
    "author_email": "mrf345@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/10/f2/c44fbffa87124ae6f69100f4b289b22473ad58b2f86471732409eb727765/Flask-Minify-0.42.tar.gz",
    "platform": "any",
    "description": "<h1 align='center'> flask_minify </h1>\n<p align='center'>\n<a href='https://pypi.org/project/Flask-Minify/'>\n    <img src='https://img.shields.io/github/v/tag/mrf345/flask_minify' alt='Latest Release' />\n</a>\n<a href='https://github.com/mrf345/flask_minify/actions/workflows/ci.yml'>\n  <img src='https://github.com/mrf345/flask_minify/workflows/Build/badge.svg'>\n</a>\n<br />\n<img src='https://img.shields.io/pypi/pyversions/flask_minify' alt='Supported versions' />\n<br />\n<a href='https://github.com/mrf345/flask_minify/actions/workflows/ci.yml'>\n  <img src='https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/mrf345/bc746d7bfe356b54fbb93b2ea5d0d2a4/raw/flask_minify__heads_master.json' alt='Coverage Percentage' />\n</a>\n<a href='https://github.com/PyCQA/bandit'>\n  <img src='https://img.shields.io/badge/security-bandit-yellow.svg' alt='security: bandit' />\n</a>\n<a href='https://github.com/psf/black'>\n    <img src='https://img.shields.io/badge/style-black-000000.svg' alt='Code Style Black' />\n</a>\n<br />\n</p>\n\n<h3 align='center'>Flask extension to parse and minify html, javascript, css and less.</h3>\n\n## Install:\n\nWith **pip**\n\n- `pip install Flask-Minify`\n\n*Or* from the source\n\n- `git clone https://github.com/mrf345/flask_minify.git`\n- `cd flask_minify`\n- `python setup.py install`\n\n## Setup:\n\nIn this example the  extension will minify every HTML request, unless it's explicitly bypassed.\n\n```python\nfrom flask import Flask\nfrom flask_minify import Minify\n\napp = Flask(__name__)\nMinify(app=app, html=True, js=True, cssless=True)\n```\n\nAnother approach is using **decorators**, you can set the extension to be `passive` so will only minify the decorated routes\n\n```python\nfrom flask import Flask\nfrom flask_minify import Minify, decorators as minify_decorators\n\napp = Flask(__name__)\nMinify(app=app, passive=True)\n\n@app.route('/')\n@minify_decorators.minify(html=True, js=True, cssless=True)\ndef example():\n  return '<h1>Example...</h1>'\n```\n\n## Options:\n\n\nOption             | type     | Description\n-------------------|----------|-------------\n app               | `object` | `Flask` app instance to be passed (default: `None`)\n html              | `bool`   | minify HTML (default: `True`)\n js                | `bool`   | minify JavaScript output (default: `True`)\n cssless           | `bool`   | minify CSS or Less. (default: `True`)\n fail_safe         | `bool`   | avoid raising error while minifying (default: `True`)\n bypass            | `list`   | endpoints to bypass minifying for, supports `Regex` (default: `[]`)\n bypass_caching    | `list`   | endpoints to bypass caching for, supports `Regex` (default: `[]`)\n caching_limit     | `int`    | limit the number of cached response variations (default: `2`).\n passive           | `bool`   | disable active minifying, to use *decorators* instead (default: `False`)\n static            | `bool`   | enable minifying static files css, less and js (default: `True`)\n script_types      | `list`   | script types to limit js minification to (default: `[]`)\n parsers           | `dict`   | parsers to handle minifying specific tags, mainly for advanced customization (default: `{}`)\n\n\n#### - `bypass` and `bypass_caching`\n\n`endpoint` in this context is the name of the function decorated with `@app.route`\nso in the following example the endpoint will be `root`:\n\n```python\n@app.route('/root/<id>')\ndef root(id):\n    return id\n```\n\nboth options can handle regex patterns as input so for example, if you want to bypass all routes on a certain blueprint\nyou can just pass the pattern as such:\n\n```python\nMinify(app, bypass=['blueprint_name.*'])\n```\n\n#### - `caching_limit`\n\nif the option is set to `0`, we'll not cache any response, so if you want to **disable caching** just do that.\n\n\n#### - `script_types`\n\nwhen using the option include `''` (empty string) in the list to include script blocks which are missing the `type` attribute.\n\n#### - `parsers`\n\nallows passing tag specific options to the module responsible for the minification, as well as replacing the default parser with another included option or your own custom one.\n\nIn the following example will replace the default `style` (handles CSS) parser `rcssmin` with `lesscpy`:\n\n```python\nfrom flask_minify import Minify, parsers as minify_parsers\n\nparsers = {'style': minify_parsers.Lesscpy}\n\nMinify(app=app, parsers=parsers)\n```\n\nyou can override the default parser runtime options as well, as shown in the following example:\n\n```python\nfrom flask_minify import Minify, parsers as minify_parsers\n\nclass CustomCssParser(minify_parsers.Lesscpy):\n    runtime_options = {\n        **minify_parsers.Lesscpy.runtime_options,\n        \"xminify\": False,\n    }\n\nparsers = {'style': CustomCssParser}\n\nMinify(app=app, parsers=parsers)\n```\n\nthe **default** parsers are set to `{\"html\": Html, \"script\": Jsmin, \"style\": Rcssmin}` check out [the code](https://github.com/mrf345/flask_minify/blob/master/flask_minify/parsers.py) for more insight.\n\n\n## Development:\n\n- *Tests*: `make test`\n- *Style check*: `make lint`\n- *Format code*: `make format`\n\n## Breaking changes\n\n#### `0.40`\n\nDue to a future deprecation in Flask 2.3, the extension is no longer going to fallback to `Flask._app_ctx_stack`, it will raise an exception instead (`flask_minify.exceptions.MissingApp`)\n\n#### `0.33`\n\nintroduces a breaking change to the expected output, in this release `lesscpy` will be replaced by `cssmin` as\nthe default css minifier so no more `less` compiling by default. in case you don't want that, follow [this example](https://github.com/mrf345/flask_minify#--parsers). \n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Flask extension to minify html, css, js and less.",
    "version": "0.42",
    "project_urls": {
        "Download": "https://github.com/mrf345/flask_minify/archive/0.42.tar.gz",
        "Homepage": "https://github.com/mrf345/flask_minify/"
    },
    "split_keywords": [
        "flask",
        "extension",
        "minifer",
        "htmlmin",
        "lesscpy",
        "jsmin",
        "html",
        "js",
        "less",
        "css"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1aad05b98fb0b1798b7f0b948613821a09674940a280b4b16c1142319b67b627",
                "md5": "ed1c55cda42529496476b919b837cac9",
                "sha256": "54211e09e0d1596d2e2897bbb4ffd8c96bb269fa959d6ae5be83fc0da14f4358"
            },
            "downloads": -1,
            "filename": "Flask_Minify-0.42-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ed1c55cda42529496476b919b837cac9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 10944,
            "upload_time": "2023-05-07T11:32:38",
            "upload_time_iso_8601": "2023-05-07T11:32:38.347614Z",
            "url": "https://files.pythonhosted.org/packages/1a/ad/05b98fb0b1798b7f0b948613821a09674940a280b4b16c1142319b67b627/Flask_Minify-0.42-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "10f2c44fbffa87124ae6f69100f4b289b22473ad58b2f86471732409eb727765",
                "md5": "8cee888c5168838933c267063f69af68",
                "sha256": "cdae9bd492b4200ebbf9fd0b08878452274ddca454f3eb1dff8b900629538c8b"
            },
            "downloads": -1,
            "filename": "Flask-Minify-0.42.tar.gz",
            "has_sig": false,
            "md5_digest": "8cee888c5168838933c267063f69af68",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 8990,
            "upload_time": "2023-05-07T11:32:41",
            "upload_time_iso_8601": "2023-05-07T11:32:41.021721Z",
            "url": "https://files.pythonhosted.org/packages/10/f2/c44fbffa87124ae6f69100f4b289b22473ad58b2f86471732409eb727765/Flask-Minify-0.42.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-07 11:32:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mrf345",
    "github_project": "flask_minify",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "flask-minify"
}
        
Elapsed time: 0.06992s