-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable compression per view #12
Comments
Hi @mik-laj, Sounds good to me and if you have the cycles to implement the feature, it would be awesome! I'm overwhelmed and won't be able to touch as that for the foreseeable future. Cheers, Thomas |
It is actually possible to do this client side, without updating the library. Here is an example using an API inspired by Flask-Caching: import functools
from flask_compress import Compress
class CustomCompress(Compress):
def compressed(self):
def decorator(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
@after_this_request
def compressor(response):
return self.after_request(response)
return f(*args, **kwargs)
return decorated_function
return decorator
# app is the Flask app
app.config["COMPRESS_REGISTER"] = False # disable default compression of all eligible requests
compress = CustomCompress()
compress.init_app(app)
# Compress this view
@app.route("/test")
@compress.compressed()
def view():
pass I implemented this in the per_view branch, I could merge this once you have tested it, to make sure it fits your use case. |
@alexprengere I have to think about it, so I will look at it at the weekend. |
@mik-laj I plan to merge the PR in a few days, once rfc7232 is merged as well, before making a new release. If you have any comments, now is the time 😉 |
This looks fine, but I would personally create a static method to limit the side effects. Now no global object is used and no need to initialize the global object first. from flask_compress import Compress
class CustomCompress(Compress):
def compressed(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
@after_this_request
def compressor(response):
compress = current_app.compress
return compress.after_request(response)
return f(*args, **kwargs)
return decorated_function Usage: from flask_compress import Compress
# Compress this view specifically
@app.route("/test")
@Compress.compressed
def view():
pass |
I think you mean
Nonetheless, I tried this idea, but the tests are failing, it is possible that A downside of that approach is that it makes configuration harder, because compression behavior is driven by the values in |
I prepared small POC: #16 |
Thanks a lot for the POC, but it does not really answer my concern. The reason why this works, is because you have those two lines. If you remove them, the test fails. So effectively the implementation does not satisfy your point above: "no global object is used and no need to initialize the global object first." The way I see it, we cannot avoid the |
https://github.com/mik-laj/flask-compress/blob/14--per_view-alternative/tests/test_flask_compress.py#L337-L338
When used in a view, it does not refer to global variables, but to the routes = Blueprint('routes', __name__)
@routes.route('/route1/')
def view_1():
return render_template('large.html')
@routes.route('/route2/')
@Compress.compressed
def view_2():
return render_template('large.html') This doesn't really matter in most cases, but can cause problems if you use the factory method to create your application instance. Then you have to use either the imports in the function or keep an eye on the order of the imports. However, I can solve this problem on my side, so if you feel that you want to merge your idea then go ahead. |
Hello,
It would be fantastic if this design only allows compression to be enabled for selected views. This could be done with a decorator.
https://github.com/apache/airflow/blob/master/airflow/www/decorators.py#L67
This will allow this library to be used in my project - Apache Airflow.
Yours faithfully.
The text was updated successfully, but these errors were encountered: