Bird
Raised Fist0
Flaskframework~20 mins

Flask-Compress for compression - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using Flask-Compress in a Flask application?
easy
A. To add database support to the Flask app
B. To compress HTTP responses and make the app faster
C. To handle user authentication automatically
D. To create HTML templates for the app

Solution

  1. Step 1: Understand Flask-Compress functionality

    Flask-Compress is designed to compress HTTP responses to reduce data size sent to clients.
  2. Step 2: Identify the main benefit

    Compressing responses makes the app faster by reducing load times and bandwidth.
  3. Final Answer:

    To compress HTTP responses and make the app faster -> Option B
  4. Quick Check:

    Compression improves speed = B [OK]
Hint: Flask-Compress speeds apps by shrinking response size [OK]
Common Mistakes:
  • Confusing compression with database or authentication features
  • Thinking it creates templates
  • Assuming it handles routing
2. Which of the following is the correct way to enable Flask-Compress in your Flask app?
easy
A. from flask_compress import Compress app = Flask(__name__) Compress(app)
B. import compress app = Flask() compress.enable(app)
C. from flask_compress import Compress app = Flask() app.compress = True
D. import FlaskCompress app = Flask(__name__) FlaskCompress.compress(app)

Solution

  1. Step 1: Check import and initialization syntax

    The official way is to import Compress from flask_compress and call Compress(app) after creating the Flask app.
  2. Step 2: Verify each option

    from flask_compress import Compress app = Flask(__name__) Compress(app) matches the correct import and usage. Others use wrong module names or incorrect method calls.
  3. Final Answer:

    from flask_compress import Compress app = Flask(__name__) Compress(app) -> Option A
  4. Quick Check:

    Correct import and call = A [OK]
Hint: Import Compress and call Compress(app) to enable [OK]
Common Mistakes:
  • Using wrong import names
  • Trying to set app.compress = True
  • Calling non-existent methods
3. Given this Flask app code snippet using Flask-Compress, what will be the effect on the HTTP response?
from flask import Flask
from flask_compress import Compress

app = Flask(__name__)
Compress(app)

@app.route('/')
def home():
    return 'Hello, world!'
medium
A. The response will be compressed if the client supports it
B. The response will always be uncompressed
C. The app will raise an error because Compress is not configured
D. The response will be compressed only for POST requests

Solution

  1. Step 1: Understand default behavior of Flask-Compress

    Flask-Compress automatically compresses responses if the client sends headers indicating support (like Accept-Encoding).
  2. Step 2: Analyze the code snippet

    The app initializes Compress with the app and returns a simple string. Compression will happen conditionally based on client support.
  3. Final Answer:

    The response will be compressed if the client supports it -> Option A
  4. Quick Check:

    Compression depends on client headers = A [OK]
Hint: Compression happens only if client supports it [OK]
Common Mistakes:
  • Assuming compression is always on
  • Thinking configuration is mandatory
  • Believing compression only works on POST
4. You added Flask-Compress to your Flask app but notice responses are not compressed. Which of these is the most likely cause?
medium
A. You forgot to call Compress(app) after creating the Flask app
B. You used Compress() without passing the app instance
C. The client does not send Accept-Encoding header
D. All of the above

Solution

  1. Step 1: Check Flask-Compress initialization

    If you don't call Compress(app) or call Compress() without app, compression won't activate.
  2. Step 2: Consider client headers

    Compression only happens if the client sends Accept-Encoding header indicating support.
  3. Step 3: Combine causes

    All these issues can cause no compression, so the correct answer is all of the above.
  4. Final Answer:

    All of the above -> Option D
  5. Quick Check:

    Initialization and client headers both matter = D [OK]
Hint: Check app init and client headers for compression issues [OK]
Common Mistakes:
  • Ignoring client headers
  • Not calling Compress(app)
  • Assuming compression works without setup
5. You want to compress only JSON responses in your Flask app using Flask-Compress. Which configuration change achieves this?
hard
A. Set app.config['COMPRESS_LEVEL'] = 'json' before calling Compress(app)
B. Call Compress(app, mime_types=['application/json']) after app creation
C. Set app.config['COMPRESS_MIMETYPES'] = ['application/json'] before calling Compress(app)
D. Use @compress.json_only decorator on JSON routes

Solution

  1. Step 1: Understand Flask-Compress configuration

    You can specify which MIME types to compress by setting COMPRESS_MIMETYPES in app config before initializing Compress.
  2. Step 2: Evaluate options

    Set app.config['COMPRESS_MIMETYPES'] = ['application/json'] before calling Compress(app) correctly sets COMPRESS_MIMETYPES to ['application/json'] before calling Compress(app). Call Compress(app, mime_types=['application/json']) after app creation is invalid because Compress does not accept mime_types parameter. Options A and D are incorrect configurations or decorators.
  3. Final Answer:

    Set app.config['COMPRESS_MIMETYPES'] = ['application/json'] before calling Compress(app) -> Option C
  4. Quick Check:

    Configure COMPRESS_MIMETYPES before Compress = C [OK]
Hint: Set COMPRESS_MIMETYPES in config before Compress(app) [OK]
Common Mistakes:
  • Trying to pass mime_types to Compress()
  • Using wrong config keys
  • Expecting decorators to control compression
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard