Challenge - 5 Problems
Flask Namespace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask namespace route?
Given this Flask-RESTPlus namespace setup, what will be the response when accessing
/api/v1/hello?Flask
from flask import Flask from flask_restx import Api, Namespace, Resource app = Flask(__name__) api = Api(app) ns = Namespace('v1', path='/v1', description='Version 1 API') @ns.route('/hello') class HelloWorld(Resource): def get(self): return {'message': 'Hello from v1'} api.add_namespace(ns, path='/api') if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Check the namespace path and route combined to form the full URL.
✗ Incorrect
The namespace v1 is added with the base path /api. The route /hello inside the namespace becomes /api/v1/hello. Accessing this URL returns the JSON message defined in the HelloWorld resource.
❓ state_output
intermediate1:30remaining
What is the value of
api.namespaces after adding namespaces?Consider this Flask-RESTX code adding two namespaces. What will
len(api.namespaces) return?Flask
from flask import Flask from flask_restx import Api, Namespace app = Flask(__name__) api = Api(app) ns1 = Namespace('users') ns2 = Namespace('products') api.add_namespace(ns1) api.add_namespace(ns2) count = len(api.namespaces)
Attempts:
2 left
💡 Hint
Each call to add_namespace adds one namespace to the API.
✗ Incorrect
The api.namespaces list holds all namespaces added. Since two namespaces are added, its length is 2.
📝 Syntax
advanced1:30remaining
Which option causes a syntax error in Flask namespace definition?
Identify the code snippet that will cause a syntax error when defining a Flask-RESTX namespace.
Attempts:
2 left
💡 Hint
Look for missing commas between arguments.
✗ Incorrect
Option A is missing a comma between the first and second argument, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this Flask namespace route return 404?
Given this code, why does accessing
/api/v2/hello return 404 Not Found?Flask
from flask import Flask from flask_restx import Api, Namespace, Resource app = Flask(__name__) api = Api(app) ns = Namespace('v1') @ns.route('/hello') class Hello(Resource): def get(self): return {'msg': 'Hello v1'} api.add_namespace(ns, path='/api/v1') if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Check the namespace path and the URL you are accessing.
✗ Incorrect
The namespace is added with path '/api/v1'. Accessing '/api/v2/hello' does not match any route, so Flask returns 404.
🧠 Conceptual
expert2:30remaining
How does Flask-RESTX namespace help organize large APIs?
Which statement best explains the main benefit of using namespaces in Flask-RESTX?
Attempts:
2 left
💡 Hint
Think about how namespaces help with API organization and docs.
✗ Incorrect
Namespaces group related API endpoints and models, making the API easier to maintain and document. They do not handle caching or replace blueprints.