mrf345 / flask_minify Goto Github PK
View Code? Open in Web Editor NEWA Flask extension to minify request's response for html, js, css and less.
License: MIT License
A Flask extension to minify request's response for html, js, css and less.
License: MIT License
So I'm using Flask-wtf's recaptcha field
from flask_wtf import RecaptchaField
And then I'm rendering the recaptcha field using Jinja2
{{ form.recaptcha }}
Which renders the following html:
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<div class="g-recaptcha" data-sitekey="6LernpkUAAAAAANMNwUpf3RKluSCzaZ_ll16zVZ4"></div>
For some odd reason, my page looks bad, some content is deleted. When I remove the Recaptcha everything works fine and if I don't minify the page where the Recaptcha is being rendered, everything also works fine.
I doubt this is a problem with the Google Recaptcha or Flask-wtf itself. I think this resides in how the HTML is being minified by the htmlmin library that Flask-Minify uses.
Maybe the Recaptcha HTML code is simply not robust enough to be minified or something like that.
For now, I'm just bypassing the route that renders the page with the Recaptcha field in it.
Hi! I'm using Flask-Minify 0.32 which works like a charm. I've found an issue with certain CSS values:
.div2 { grid-area: 2 / 2 / 3 / 4; }
.div3 { grid-area: 1 / 2 / 2 / 3; }
these are minified into
div2{grid-area:0.08333333333333;}
for example, which breaks the grid layout. I've bypassed the routes to work around this, but I thought I'd submit this issue.
Cheers
The description for the go
option states
prefer go minifier, if optional go dependency is installed (default:
True
)
This suggests that if the go dependency is not installed, this option will do nothing. Instead, leaving this option as True causes the parsers to raise an exception from here:
flask_minify/flask_minify/parsers.py
Lines 134 to 142 in b7ce727
If a user has a development environment that does not have the extension installed (for example, developing on windows) but still wishes to use the go extension in production, they must explicitly toggle this option based on the code environment, which isn't ideal.
Instead it would be nice if this option was ignored if the go dependency isn't found, as its description suggests it is.
In case it helps anyone else, my current workaround is:
from flask_minify import Minify, parsers
Minify(app, go=bool(parsers.minify_go))
But if it suits your deploy process, you could also do the much simpler:
Minify(app, go=not app.debug)
#!/usr/bin/env python3
from flask import Flask
from flask_minify import minify
app = Flask(__name__)
minify(app=app, js=True)
@app.route('/')
def index():
return '<script>const x = 42; document.write(`foo ${x} bar`)</script>'
This produces the following response on GET /
:
<script>const x=42;document.write(`foo ${x}bar`)</script>
The output is missing a space character after ${x}
. The error occurs only if x
is enclosed by curly brackets.
This is my setup:
minify(app=app, bypass=[r'users\.register', r'users\.contact'])
If I don't supply an argument to the bypass parameter no errors appear.
When I hit ctrl + u
Chrome takes me to the source code of a specific page.
For example I'm currently on the home page (http://127.0.0.1:5000/home)
Now I press ctrl + u
and it takes me to the source code of the home page
(view-source:http://127.0.0.1:5000/home)
When I do that I'm receiving the following errors:
Traceback (most recent call last): File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2463, in __call__ return self.wsgi_app(environ, start_response) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2449, in wsgi_app response = self.handle_exception(e) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 1866, in handle_exception reraise(exc_type, exc_value, tb) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request return self.finalize_request(rv) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 1969, in finalize_request response = self.process_response(response) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2266, in process_response response = handler(response) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask_minify\main.py", line 151, in main should_bypass = self.get_endpoint_matches(self.bypass) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask_minify\main.py", line 117, in get_endpoint_matches [compile_re(pattern) for pattern in patterns] File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask_minify\main.py", line 118, in <listcomp> if compiled_pattern.match(self.endpoint)] TypeError: expected string or bytes-like object
Inside the get_endpoint_matches method (def get_endpoint_matches(self, patterns
)
self.endpoint
returns None
when the URL is: view-source:http://127.0.0.1:5000/blabla...
The method should probably just ignore endpoints that are None
, and that is exactly my current workaround for now.
original:
matches = [compiled_pattern for compiled_pattern in
[compile_re(pattern) for pattern in patterns]
if compiled_pattern.match(self.endpoint)]
workaround:
matches = [compiled_pattern for compiled_pattern in
[compile_re(pattern) for pattern in patterns]
if compiled_pattern.match(self.endpoint)] if self.endpoint else []
I think that endpoints for static files are not being handled properly by Flask-Minify.
Hi,
I followed the process to install and use flask-minify on PyPi website - https://pypi.org/project/Flask-Minify/
It did not minify any of these when I checked by running my app on my local machine.
I am using - Python 3.6.8
I think this may be the same issue reported in #47, but it was self-closed and I didn't find the author's solution satisfying.
Basically, I'm seeing confusing behavior about how line breaks are minified. For example, below is the content of my Jinja2 HTML template (leading spaces intentionally kept in to reflect how it looks in my code):
<nav class="navbar bg-primary fixed-top navbar-expand-md">
<div class="container-fluid">
<button
class="navbar-toggler"
type="button"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="nav-collapse">
It got minified to the following, with spaces between the tags:
<nav class="navbar bg-primary fixed-top navbar-expand-md"> <div class="container-fluid"> <button class="navbar-toggler" type="button"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="nav-collapse">
It's not consistent, though. At the top of that same file (my top-level layout template), I have:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="manifest" href="/manifest.json" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="theme-color" content="#2c3e50" />
And that correctly minifies to:
<!DOCTYPE html><html lang="en"> <head><link rel="manifest" href="/manifest.json"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#2c3e50">
Any ideas what's going on? It's not the end of the world but as long as I'm saving bytes, I want to save ALL the bytes. Thanks!
A color: rgba(202,242,255,1);
directive seems to be converted to a hex value like #caf2ff01, which is not correct, it should be #caf2ffff
Found an invariant condition in decorators.minify
which returns an invalid response whenever caching is enabled
tests/integration.py:166:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:1006: in get
return self.open(*args, **kw)
.nox/test-3-7/lib/python3.7/site-packages/flask/testing.py:227: in open
follow_redirects=follow_redirects,
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:970: in open
response = self.run_wsgi_app(environ.copy(), buffered=buffered)
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:861: in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:1096: in run_wsgi_app
app_rv = app(environ, start_response)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2464: in __call__
return self.wsgi_app(environ, start_response)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2450: in wsgi_app
response = self.handle_exception(e)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1867: in handle_exception
reraise(exc_type, exc_value, tb)
.nox/test-3-7/lib/python3.7/site-packages/flask/_compat.py:39: in reraise
raise value
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2447: in wsgi_app
response = self.full_dispatch_request()
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1953: in full_dispatch_request
return self.finalize_request(rv)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1968: in finalize_request
response = self.make_response(rv)
Files placed in subdirectories are ignored.
Example:
Processed: /app/blueprint/static/css/mycss.css
Ignored: /app/blueprint/static/css/ext/extcss.css
Not necessarily an issue, rather an improvement.
I'm using a for
loop that iterates through all of my resources:
def make_sender(path, dir):
def f():
mimetype = magic.from_file(os.path.join(app.root_path, 'game/'+dir+path), mime=True)
if "js" in path:
mimetype = "application/javascript"
if "css" in path:
mimetype = "text/css"
return send_from_directory(os.path.join(app.root_path, 'game/'+dir),
path.replace("/", ""), mimetype=mimetype)
return f
for file in ['/welcome.css', '/install.js']:
app.add_url_rule(file, file, make_sender(file, "related"))
It doesn't look like Flask-Minify
is working there, though. Is there a way to do that there?
Bump into the following error
File ".../python2.7/site-packages/Flask_Minify-0.27-py2.7.egg/flask_minify/main.py", line 227, in get_minified_or_cached
key = hashing(content).hexdigest()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 42768: ordinal not in range(128)
I believe the solution should be
key = hashing(content.encode('utf-8')).hexdigest()
Due the lack of cgi module in the standard library, htmlmin is not compatible with py3.13 therefore flask-minify.
htmlmin seems to be unmantained.
It import cgi in its main.py file but never use it. It is the only incompatibility that I found
i have the minifier running on production but not dev. i noticed that in production all occurrences of line breaks do not apply. when they are templated by jinja2, the response html text in a p element literally is on a new line to reflect it. white-space: pre-line;
needs to be applied to take effect. the solution is not to ignore \n's in text elements but to replace them with html <br>
's. ill get back to this when i find my solution.
I might be missing something, but pyyaml is a dependency that I don't see used anywhere. Could it be removed from requirements, just to trim one dependency?
following the documentation, only the html portion was minified. my javascript code was still the same even setting js =True and my javascript isnt working properly anymore.
So I'm getting these errors when running on the server (not locally)
Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/env/lib/python3.7/site-packages/flask/app.py", line 1835, in full_dispatch_request
return self.finalize_request(rv)
File "/env/lib/python3.7/site-packages/flask/app.py", line 1852, in finalize_request
response = self.process_response(response)
File "/env/lib/python3.7/site-packages/flask/app.py", line 2131, in process_response
response = handler(response)
File "/env/lib/python3.7/site-packages/flask_minify/init.py", line 65, in toLoopTag
request.url_rule.rule in self.bypass
AttributeError: 'NoneType' object has no attribute 'rule'
And I think it's because Flask-Minify extension isn't getting properly registered with the app as all the other extensions are. The extension needs to have an "init_app" method or something like that.
by the way, I'm using blueprints
In my code, I've been using flask_minify
, and I've been disabling the cache (for dynamic reasons). When I saw this PR from Dependabot on my repo, I saw that the checks passed, so I merged it in. Whenever I tried to connect to the server, though, it gave me this exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
response = self.process_response(response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
response = handler(response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 273, in main
'style'))
File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
limit_reached and self.cache[self.endpoint].popitem()
KeyError: 'something'
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
return self.finalize_request(rv)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
response = self.process_response(response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
response = handler(response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 273, in main
'style'))
File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
limit_reached and self.cache[self.endpoint].popitem()
KeyError: 'something'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
response = self.process_response(response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
response = handler(response)
File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 266, in main
self.html))
File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
limit_reached and self.cache[self.endpoint].popitem()
KeyError: 'something'
What's going on here? How can I fix it?
The refactoring has resulted in applications that use init_app
to throw flask_minify.exceptions.MissingApp
, as the Minify
object only saves app
in __init__
, but not init_app
, and the self.app
@Property method no longer has the _app_ctx_stack
to fall back to. Fix it by not saving any specific app in self._app
and always fall back to Flask.current_app
in the self.app
@Property.
How to integrate https://github.com/tdewolff/minify into flask_minify?
I have an app which builds js assets at runtime. It would be very useful if I could apply it to a single endpoint that generates those, rather than the whole app and then try and regex all the other endpoints of the app.
Either a decorator to apply to an endpoint or a positive list vs only a negative one would be great.
Flask-Minify requires PyYaml 5.3.1, which contains a security vulnerability.
I tried using flask_minify, got an error ... help.
Thanks,
Evan
Seems so easy to use ... but not sure what the error is about ...
I am using pyenv with Python 2.7
pip install Flask-Minify
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting Flask-Minify
Using cached Flask-Minify-0.27.tar.gz (6.8 kB)
ERROR: Command errored out with exit status 1:
command: /xxx/.pyenv/versions/2.7.18/envs/food2/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-CtdDDJ/flask-minify/setup.py'"'"'; __file__='"'"'/tmp/pip-install-CtdDDJ/flask-minify/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-Uffpej
cwd: /tmp/pip-install-CtdDDJ/flask-minify/
Complete output (5 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-CtdDDJ/flask-minify/setup.py", line 18, in <module>
with open(path.join(requirements_path, 'main.txt')) as f:
IOError: [Errno 2] No such file or directory: '/tmp/pip-install-CtdDDJ/flask-minify/requirements/main.txt'
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
I am not familiar with python package, but seems like setup.py
try to access requirements/*
which is not packaged in Flask-Minify-0.28.tar.gz
.
It works fine if I install from the source.
Hi there,
I'm using flask_minify
for an AMP project. AMP requires you to tag style blocks with an HTML attribute like so: <style amp-custom>
Unfortunately this no longer matches with the <style>
regex.
Pulling this out of #27 (comment)
WARNING: flake8 setuptools integration is deprecated and scheduled for removal in 4.x. For more information, see https://gitlab.com/pycqa/flake8/issues/544
Your code breaks JS code https://github.com/mrf345/flask_minify/blob/master/flask_minify/main.py#L161
Never do this! You can use new lines in the middle of the code to follow max length line limitation or just for pretty formatting.
Please address this deprecation warning in next release.
/venv/lib/python3.9/site-packages/flask_minify/main.py:122: DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.3. Use 'g' to store data, or 'app_ctx' to access the current context.
return self._app or (_app_ctx_stack.top and _app_ctx_stack.top.app)
The HTML
gets minified which was the initial goal. But somehow the CSS
and JS
which are included on the JINJA2
templates are not being minified.
minify = Minify()
minify.fail_safe = False
and then after app created on app factory then minify.init_app(app)
Is this the expected result, that just html and not the CSS
+ JS
if included on JINJA2
are not minified?
HTML inner tags caching is ignored which's causing reduction in performance.
Bypassing static files is ignored because self.endpoint
return request.endpoint
and that's always static
when it's a static file. we should return request.path
instead.
I noticed that for some reason, the CSS wasn't minifying. It was auto-added with app.add_url_rule
. Any reasons, solutions, or stuff I could do to help debug?
Oddly enough I had an issue doing
pip install flask-minify
while
pip install Flask-Minify
worked
I had thought case should not matter
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.