0
0
Flaskframework~10 mins

Migrating to async Flask - Interactive Code Practice

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

Complete the code to define an async route handler in Flask.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
async def [1]():
    return 'Hello, async Flask!'
Drag options to blanks, or click blank then click option'
Ahome
Bindex
Cmain
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using a synchronous function instead of async
Naming the function something unrelated
2fill in blank
medium

Complete the code to await an async function inside an async Flask route.

Flask
import asyncio
from flask import Flask
app = Flask(__name__)

async def fetch_data():
    await asyncio.sleep(1)
    return 'data'

@app.route('/data')
async def get_data():
    result = await [1]()
    return result
Drag options to blanks, or click blank then click option'
Afetch
Bget_data
Cfetch_data
Ddata_fetch
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use await
Calling a wrong function name
3fill in blank
hard

Fix the error in the async Flask route by completing the decorator.

Flask
from flask import Flask
app = Flask(__name__)

[1]('/async')
async def async_route():
    return 'Async route works!'
Drag options to blanks, or click blank then click option'
A@app.route
B@app.async_route
C@app.async
D@app.get
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent decorator like @app.async
Using @app.async_route which is invalid
4fill in blank
hard

Fill both blanks to create an async Flask route that returns JSON asynchronously.

Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/json')
async def json_route():
    data = await [1]()
    return [2](data)
Drag options to blanks, or click blank then click option'
Afetch_data
Bjsonify
Cjson
Dget_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using json instead of jsonify
Not awaiting the async function
5fill in blank
hard

Fill all three blanks to define an async Flask route that calls two async functions and returns combined results.

Flask
from flask import Flask, jsonify
app = Flask(__name__)

async def fetch_user():
    return {'user': 'Alice'}

async def fetch_status():
    return {'status': 'active'}

@app.route('/combined')
async def combined_route():
    user = await [1]()
    status = await [2]()
    result = {**user, **[3]
    return jsonify(result)
Drag options to blanks, or click blank then click option'
Afetch_user
Bfetch_status
Cstatus
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up variable names in the merge
Not awaiting both async functions