Flask is often described as a micro-framework. What does this mean about Flask's design and features?
Think about what 'micro' means in terms of included features versus flexibility.
Flask is called a micro-framework because it provides only the essential tools for web development. It does not include many built-in features like database management or form validation. Instead, it allows developers to add only the extensions they need, keeping the core simple and flexible.
Consider this Flask code snippet:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome!'What happens when you visit the root URL '/' in a browser?
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome!'
Flask routes URLs to functions that return responses directly.
Flask uses the @app.route decorator to connect a URL path to a Python function. When the root URL '/' is visited, Flask calls the home() function, which returns the string 'Welcome!'. This string is sent as the response and displayed in the browser.
When you run a Flask app with app.run(debug=True), what is a key behavior Flask adds during development?
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello!' if __name__ == '__main__': app.run(debug=True)
Think about how developers want quick feedback during coding.
Setting debug=True enables the debugger and automatic reloader. This means Flask watches your code files and restarts the server when you save changes. It also shows detailed error pages to help fix bugs faster.
Look at this Flask route code:
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return 'Hi there!'What is the syntax error here?
from flask import Flask app = Flask(__name__) @app.route('/hello') def hello(): return 'Hi there!'
Check Python indentation rules inside functions.
In Python, the code inside a function must be indented. The return statement here is not indented, causing a syntax error. The other options are incorrect because the decorator syntax is correct, function names can be anything valid, and quotes can be single or double.
Consider this Flask app code:
from flask import Flask
app = Flask(__name__)
@app.route('/greet/')
def greet():
return f'Hello, {name}!'
if __name__ == '__main__':
app.run() What error will occur when accessing a URL like '/greet/Alice' and why?
from flask import Flask app = Flask(__name__) @app.route('/greet/<name>') def greet(name): return f'Hello, {name}!' if __name__ == '__main__': app.run()
Check the function parameters matching the route variables.
The route '/greet/<name>' expects a variable 'name' to be passed to the function. The greet() function lacks a 'name' parameter, so Flask raises a TypeError about missing arguments when calling it.