0
0
Flaskframework~20 mins

Flask extensions directory - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Extensions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Flask-Login manage user sessions?

Consider a Flask app using Flask-Login. What happens when a user logs in successfully?

AUser credentials are stored in a global variable accessible across requests.
BThe user ID is stored in the session cookie, allowing Flask-Login to reload the user on each request.
CThe entire user object is stored in the session cookie for quick access.
DFlask-Login creates a new database entry for each login session.
Attempts:
2 left
💡 Hint

Think about how Flask-Login keeps track of the logged-in user between requests.

📝 Syntax
intermediate
2:00remaining
Correct way to initialize Flask-Migrate

Which code snippet correctly initializes Flask-Migrate in a Flask app?

Flask
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
db = SQLAlchemy(app)

# Initialize Flask-Migrate here
Amigrate = Migrate(app)
Bmigrate = Migrate(db, app)
Cmigrate = Migrate(app, db)
Dmigrate = Migrate(db)
Attempts:
2 left
💡 Hint

Check the order of arguments required by Flask-Migrate's constructor.

🔧 Debug
advanced
2:00remaining
Why does Flask-Mail fail to send emails?

Given this Flask-Mail setup, emails are not sent and no error is shown. What is the likely cause?

Flask
from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp.example.com',
    MAIL_PORT=587,
    MAIL_USE_TLS=True,
    MAIL_USERNAME='user@example.com',
    MAIL_PASSWORD='password'
)
mail = Mail(app)

@app.route('/send')
def send_email():
    msg = Message('Hello', sender='user@example.com', recipients=['friend@example.com'])
    msg.body = 'This is a test email.'
    mail.send(msg)
    return 'Email sent!'
AThe MAIL_PASSWORD is incorrect, but Flask-Mail does not raise errors.
BThe MAIL_USE_TLS should be False when using port 587.
CThe MAIL_DEFAULT_SENDER is not set, causing the email to fail silently.
DThe Flask app is missing app.app_context() when sending the email.
Attempts:
2 left
💡 Hint

Consider Flask's application context when sending emails outside request handling.

state_output
advanced
2:00remaining
What is the output of Flask-Caching with simple cache?

Consider this Flask app using Flask-Caching with SimpleCache. What will be printed on the second request to '/'?

Flask
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
app.config['CACHE_TYPE'] = 'SimpleCache'
cache = Cache(app)

@app.route('/')
@cache.cached(timeout=60)
def index():
    print('Function called')
    return 'Hello World'

# First request: prints 'Function called' and returns 'Hello World'
# Second request within 60 seconds: ?
APrints nothing and returns 'Hello World' from cache.
BPrints 'Function called' again and returns 'Hello World'.
CRaises a runtime error because SimpleCache is not persistent.
DReturns an empty response with status 200.
Attempts:
2 left
💡 Hint

Think about what caching does to function calls.

🧠 Conceptual
expert
2:00remaining
Why use Flask-Babel for localization?

Which of the following best explains why Flask-Babel is preferred for adding localization support in Flask apps?

AIt integrates with Flask's request context to select languages dynamically and supports date/time formatting and translations.
BIt automatically translates all text in templates without any extra setup.
CIt replaces Flask's routing system to handle language-specific URLs.
DIt stores all translations in the database for faster access.
Attempts:
2 left
💡 Hint

Consider how localization libraries handle language selection and formatting.