colour-science / flask-compress

Compress responses of your Flask application.
MIT License
117 stars 27 forks source link

Disable compression inside view? #26

Closed mrx23dot closed 3 years ago

mrx23dot commented 3 years ago

Is there an easy way to conditional disable compression inside view?

(so I could serve local clients with uncompressed, and remote ones with compress data on the same view)

from flask_compress import Compress
# Compress this view specifically
@app.route("/test")
@Compress.compressed
def view():
   pass

Like:

def view():
   if 1==1:
      compression.disable()
alexprengere commented 3 years ago

Honestly, I do not seen an easy way to implement this. We would need to rely on a global state that we could change from within the view, and that would affect programs that run things concurrently. Perhaps someone else has a better idea.

mrx23dot commented 3 years ago

No worries, just close the ticket if there is no good solution. Cheers

mrx23dot commented 3 years ago

What about manually compressing return payload?


def view
  if 1:
    return compress(payload_str)
  else:
     return payload_str
alexprengere commented 3 years ago

I think a simpler way would be to add the per-view decorator following some test, but this would not be "inside the view"

from flask import Flask
from flask_compress import Compress

app = Flask(__name__)
app.config["COMPRESS_REGISTER"] = False  # disable default compression of all eligible requests
compress = Compress()
compress.init_app(app)

@app.route("/test")
def view():
   pass

if 1:
    view = compress.compressed()(view)