0
0
Flaskframework~20 mins

Flask-Compress for compression - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask-Compress Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What output does this Flask app produce with Flask-Compress?

Consider this Flask app using Flask-Compress. What will the client receive when requesting the '/' route with 'Accept-Encoding: gzip'?

Flask
from flask import Flask
from flask_compress import Compress

app = Flask(__name__)
Compress(app)

@app.route('/')
def index():
    return 'Hello, compressed world!'

# Client sends header: Accept-Encoding: gzip
AThe response body is compressed with gzip encoding.
BThe response body is compressed but with Brotli encoding.
CThe server returns a 404 error because the route is missing.
DThe response body is sent uncompressed because Flask-Compress is not enabled.
Attempts:
2 left
💡 Hint

Flask-Compress automatically compresses responses if the client supports it.

📝 Syntax
intermediate
1:30remaining
Which option correctly initializes Flask-Compress in this app?

Given a Flask app instance app, which code snippet correctly enables Flask-Compress?

Flask
from flask import Flask
from flask_compress import Compress

app = Flask(__name__)
# Which line correctly enables compression?
ACompress(app)
Bapp = Compress(app)
CCompress().init_app(app)
Dapp.compress = Compress(app)
Attempts:
2 left
💡 Hint

Flask-Compress can be initialized by passing the app instance directly.

🔧 Debug
advanced
2:30remaining
Why does Flask-Compress not compress this JSON response?

In this Flask app, the JSON response is not compressed even though Flask-Compress is enabled. Why?

Flask
from flask import Flask, jsonify
from flask_compress import Compress

app = Flask(__name__)
Compress(app)

@app.route('/data')
def data():
    response = jsonify({'key': 'value'})
    response.headers['Content-Encoding'] = 'identity'
    return response
ABecause Flask-Compress does not support JSON responses.
BBecause the response header 'Content-Encoding' is manually set to 'identity', preventing compression.
CBecause the route function returns a dictionary instead of a response object.
DBecause Flask-Compress requires the response to be a string, not JSON.
Attempts:
2 left
💡 Hint

Check if any headers might block compression.

state_output
advanced
2:00remaining
What is the effect of setting COMPRESS_LEVEL to 9 in Flask-Compress?

In Flask-Compress configuration, what does setting app.config['COMPRESS_LEVEL'] = 9 do?

Flask
from flask import Flask
from flask_compress import Compress

app = Flask(__name__)
app.config['COMPRESS_LEVEL'] = 9
Compress(app)
AIt sets the compression to the lowest level, making compression faster but less effective.
BIt disables compression entirely.
CIt causes a runtime error because 9 is an invalid compression level.
DIt sets the gzip compression to the highest level, increasing compression but using more CPU.
Attempts:
2 left
💡 Hint

Compression levels usually range from 1 to 9.

🧠 Conceptual
expert
1:30remaining
Which header must the client send to receive compressed responses from Flask-Compress?

To get compressed responses from a Flask app using Flask-Compress, which HTTP header should the client include?

ACache-Control: no-cache
BContent-Type: application/json
CAccept-Encoding: gzip, deflate, br
DContent-Encoding: gzip
Attempts:
2 left
💡 Hint

Think about how clients tell servers what compression they support.