📖 Scenario: You are building a simple web service that provides information about fruits. This service will have an API endpoint that returns fruit data in JSON format.
🎯 Goal: Create a Flask application with a route /api/fruits that returns a JSON response containing a list of fruits and their colors.
📋 What You'll Learn
Create a dictionary called fruits with fruit names as keys and their colors as values
Create a Flask app instance named app
Define a route /api/fruits using the @app.route decorator
Return the fruits dictionary as a JSON response using jsonify
💡 Why This Matters
🌍 Real World
APIs like this let apps and websites get data from servers in a simple, standard way.
💼 Career
Knowing how to create API endpoints with Flask is useful for backend web development jobs.
Progress0 / 4 steps
1
Create the fruit data dictionary
Create a dictionary called fruits with these exact entries: 'apple': 'red', 'banana': 'yellow', and 'grape': 'purple'.
Flask
Hint
Use curly braces {} to create a dictionary with keys and values.
2
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Hint
Use app = Flask(__name__) to create the app instance.
3
Define the API route
Use the @app.route decorator with the path /api/fruits to define a function called get_fruits.
Flask
Hint
Use @app.route('/api/fruits') above the function definition.
4
Return JSON response with fruit data
Import jsonify from flask and return jsonify(fruits) inside the get_fruits function.
Flask
Hint
Use return jsonify(fruits) to send the dictionary as JSON.
Practice
(1/5)
1. What does a Flask route do in a web application?
easy
A. It styles the webpage with CSS.
B. It stores data in a database automatically.
C. It compiles Python code into machine code.
D. It defines a URL where the app listens for requests.
Solution
Step 1: Understand the role of a route
A Flask route sets a specific web address that the app listens to for incoming requests.
Step 2: Identify what routes do not do
Routes do not handle styling, database storage automatically, or code compilation.
Final Answer:
It defines a URL where the app listens for requests. -> Option D
Quick Check:
Flask route = URL listener [OK]
Hint: Routes map URLs to functions handling requests [OK]
Common Mistakes:
Thinking routes handle styling or database storage
Confusing routes with frontend design
Assuming routes compile code
2. Which of the following is the correct syntax to create a Flask route for the URL '/api/data'?
easy
A. @app.route('/api/data')\ndef get_data():\n return 'data'
B. @app.route('/api/data')\nfunction get_data() { return 'data'; }
C. app.route('/api/data')\ndef get_data():\n return 'data'
D. @app.route('/api/data')\ndef get_data():\nprint('data')
Solution
Step 1: Check Flask route decorator syntax
The correct syntax uses @app.route('/api/data') above a Python function definition.
Step 2: Verify function and return statement
The function must be defined with def and return a value; print does not send a response.
Final Answer:
@app.route('/api/data')\ndef get_data():\n return 'data' -> Option A
Quick Check:
Python route decorator + def + return = D [OK]
Hint: Use @app.route decorator and def function with return [OK]
Common Mistakes:
Using JavaScript syntax inside Flask route
Missing @ before app.route
Using print instead of return in route function
3. What will be the output when accessing the '/hello' route in this Flask app?
B. json is not imported or defined; should use jsonify
C. Function name 'data' is invalid
D. Route URL must start with '/api/'
Solution
Step 1: Check function return statement
The code uses json({'key': 'value'}) but json is not imported or defined.
Step 2: Identify correct function for JSON response
Flask provides jsonify to return JSON responses, which is imported here.
Final Answer:
json is not imported or defined; should use jsonify -> Option B
Quick Check:
Use jsonify, not json, for JSON response [OK]
Hint: Use jsonify, not json, to return JSON in Flask [OK]
Common Mistakes:
Using json instead of jsonify
Forgetting to import jsonify
Assuming function names must follow special rules
5. You want to create a Flask API endpoint '/user/' that returns JSON with the user's id and a greeting message. Which code correctly implements this?
hard
A. @app.route('/user/')\ndef user():\n return jsonify({'id': id, 'message': 'Hello user'})
B. @app.route('/user/<id>')\ndef user(id):\n return {'id': id, 'message': 'Hello user ' + id}
C. @app.route('/user/')\ndef user(id):\n return jsonify({'id': id, 'message': f'Hello user {id}'})
D. @app.route('/user/')\ndef user(id):\n print({'id': id, 'message': f'Hello user {id}'})
Solution
Step 1: Check route URL and parameter type
The route uses '/user/' which correctly captures an integer parameter named id.
Step 2: Verify function parameters and return
The function accepts id and returns a JSON response using jsonify with the correct message format.
Step 3: Identify errors in other options
@app.route('/user/<id>')\ndef user(id):\n return {'id': id, 'message': 'Hello user ' + id} misses int type and returns a dict without jsonify; @app.route('/user/')\ndef user():\n return jsonify({'id': id, 'message': 'Hello user'}) misses id parameter; @app.route('/user/')\ndef user(id):\n print({'id': id, 'message': f'Hello user {id}'}) uses print instead of return.
Final Answer:
@app.route('/user/')\ndef user(id):\n return jsonify({'id': id, 'message': f'Hello user {id}'}) -> Option C
Quick Check:
Route with int param + jsonify return = A [OK]
Hint: Use in route and return jsonify with id [OK]
Common Mistakes:
Not specifying for integer parameter
Returning dict without jsonify
Using print instead of return in route
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.