0
0
Flaskframework~10 mins

Why patterns improve code quality in Flask - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Flask app instance.

Flask
from flask import Flask
app = Flask([1])
Drag options to blanks, or click blank then click option'
A__name__
BNone
CFlask
Dapp
Attempts:
3 left
💡 Hint
Common Mistakes
Using the app variable name instead of __name__
Passing the class Flask instead of a string
Passing None which causes errors
2fill in blank
medium

Complete the code to define a route that returns 'Hello World!'.

Flask
@app.route([1])
def hello():
    return 'Hello World!'
Drag options to blanks, or click blank then click option'
A'/home'
B'/hello'
C'/'
D'/index'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a route other than '/' for the home page
Forgetting to put quotes around the route string
3fill in blank
hard

Fix the error in the code to run the Flask app only when the script is executed directly.

Flask
if [1] == '__main__':
    app.run(debug=True)
Drag options to blanks, or click blank then click option'
A__name__
Bapp.name
Cmain
Dapp.__name__
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.name instead of __name__
Comparing to 'main' without underscores
Using app.__name__ which is incorrect
4fill in blank
hard

Fill both blanks to create a route that accepts a username parameter and returns a greeting.

Flask
@app.route('/user/<[1]>')
def greet([2]):
    return f'Hello, {{ [2] }}!'
Drag options to blanks, or click blank then click option'
Ausername
Bname
Cuser
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in the route and function parameter
Using generic names like 'id' that don't match the route
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps route names to their functions if the function name starts with 'view_'.

Flask
routes = { [1]: [2] for [3] in dir(app) if [1].startswith('view_') }
Drag options to blanks, or click blank then click option'
Afunc_name
Bgetattr(app, func_name)
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently
Not using getattr to access the function
Filtering on the wrong variable