Bird
Raised Fist0
Flaskframework~20 mins

Flask extensions directory - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of the Flask extensions directory?
easy
A. To list tools that add extra features to Flask apps
B. To provide Flask app deployment services
C. To store Flask app source code
D. To manage Flask app user authentication

Solution

  1. Step 1: Understand the role of the Flask extensions directory

    The directory lists various tools and libraries that can be added to Flask apps to extend their functionality.
  2. Step 2: Compare options with this purpose

    Only To list tools that add extra features to Flask apps correctly describes this purpose, while others describe unrelated services.
  3. Final Answer:

    To list tools that add extra features to Flask apps -> Option A
  4. Quick Check:

    Flask extensions directory = list of tools [OK]
Hint: Extensions add features; directory lists them [OK]
Common Mistakes:
  • Confusing extensions directory with deployment services
  • Thinking it stores app source code
  • Assuming it manages user authentication directly
2. Which command correctly installs a Flask extension named Flask-Login?
easy
A. pip flask install Flask-Login
B. pip install Flask-Login
C. flask install Flask-Login
D. install pip Flask-Login

Solution

  1. Step 1: Identify the standard way to install Python packages

    Python packages, including Flask extensions, are installed using the pip install command.
  2. Step 2: Check the options for correct syntax

    Only pip install Flask-Login uses the correct syntax: pip install Flask-Login. Others are invalid commands.
  3. Final Answer:

    pip install Flask-Login -> Option B
  4. Quick Check:

    Use pip install for Flask extensions [OK]
Hint: Use 'pip install' to add Flask extensions [OK]
Common Mistakes:
  • Using 'flask install' instead of 'pip install'
  • Mixing command order like 'pip flask install'
  • Omitting 'pip' or using wrong command words
3. Given this Flask app code snippet using an extension:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)

@app.route('/')
def home():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run()

What does SQLAlchemy(app) do here?
medium
A. Creates a database connection linked to the Flask app
B. Starts the Flask app server
C. Defines a new Flask route
D. Imports the Flask app configuration

Solution

  1. Step 1: Understand the role of SQLAlchemy in Flask

    SQLAlchemy is a Flask extension that manages database connections and operations.
  2. Step 2: Analyze the code db = SQLAlchemy(app)

    This line creates a database object linked to the Flask app, enabling database use within the app.
  3. Final Answer:

    Creates a database connection linked to the Flask app -> Option A
  4. Quick Check:

    SQLAlchemy(app) = database connection [OK]
Hint: SQLAlchemy(app) sets up database for Flask [OK]
Common Mistakes:
  • Thinking it starts the server
  • Confusing it with route definition
  • Assuming it imports config only
4. What is wrong with this Flask extension usage?
from flask import Flask
from flask_mail import Mail

app = Flask(__name__)
mail = Mail()

@app.route('/')
def index():
    return 'Welcome'

if __name__ == '__main__':
    app.run()

Why might this code fail to send emails using mail?
medium
A. The Flask app is missing the __name__ variable
B. The Mail extension is imported incorrectly
C. The route decorator is missing parentheses
D. The Mail extension is not initialized with the Flask app

Solution

  1. Step 1: Check how the Mail extension is initialized

    The Mail extension requires initialization with the Flask app, usually by passing app to Mail(app) or calling mail.init_app(app).
  2. Step 2: Identify missing initialization

    In the code, mail = Mail() is created but not linked to app, so it won't work properly.
  3. Final Answer:

    The Mail extension is not initialized with the Flask app -> Option D
  4. Quick Check:

    Extensions must be linked to app [OK]
Hint: Always initialize extensions with your Flask app [OK]
Common Mistakes:
  • Forgetting to pass app to extension
  • Confusing route decorator syntax
  • Incorrect import statements
5. You want to add user login support to your Flask app using an extension. Which steps correctly describe how to use the Flask-Login extension from the directory?

A) Install Flask-Login with pip
B) Import and create a LoginManager instance
C) Initialize LoginManager with your Flask app
D) Define user loader callback for session management

Which sequence is correct?
hard
A. A, C, B, D
B. B, A, D, C
C. A, B, C, D
D. D, A, B, C

Solution

  1. Step 1: Install the extension first

    You must install Flask-Login using pip before using it in code.
  2. Step 2: Import and create LoginManager instance

    After installation, import LoginManager and create its instance.
  3. Step 3: Initialize LoginManager with the Flask app

    Link the LoginManager instance to your Flask app for it to work.
  4. Step 4: Define user loader callback

    This callback tells Flask-Login how to load a user from session data.
  5. Final Answer:

    A, B, C, D -> Option C
  6. Quick Check:

    Install, create, init, define callback [OK]
Hint: Install first, then create and initialize extension [OK]
Common Mistakes:
  • Trying to use extension before installing
  • Initializing before creating instance
  • Skipping user loader callback
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