C. from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello World!'
D. from flask import Flask
app = Flask(__file__)
@app.route('/')
def home():
return 'Hello World!'
Solution
Step 1: Check Flask app initialization
Flask apps require passing __name__ to Flask() for correct setup.
Step 2: Verify route and function syntax
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello World!' correctly imports Flask, initializes app with __name__, and defines a route with a function returning a string.
Final Answer:
Correct Flask app start with __name__ -> Option C
Quick Check:
Flask(__name__) is correct app init [OK]
Hint: Flask app needs Flask(__name__) to start [OK]
Common Mistakes:
Using Flask() without __name__
Confusing Django import with Flask
Passing wrong argument like __file__
3. Given this Flask app code, what will be the output when accessing the root URL /?
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to Flask!'
medium
A. Welcome to Flask!
B. Empty page
C. 500 Internal Server Error
D. 404 Not Found error
Solution
Step 1: Analyze route decorator
The route '/' is defined and linked to the home() function.
Step 2: Check function return value
The home() function returns the string 'Welcome to Flask!'. Flask sends this as the response body.