First Flask App & Debug Mode
Getting started with Flask is incredibly fast. With just five lines of code, you can have a local development server running.
1 Writing Your First App
Python — app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello, World!"
if __name__ == "__main__":
# Run server locally on port 5000 in debug mode
app.run(debug=True)
2 Why Enable Debug Mode?
- Auto-Reload: The server automatically restarts whenever you save changes to your code.
- Interactive Debugger: If an exception occurs, an interactive debugger will display in the browser, letting you execute code dynamically at the error line.
Warning: Never enable debug mode in a production environment, as it allows arbitrary code execution on your server.
3 Code Challenge
Challenge: Install flask with
pip install flask, create your first app.py script, and run it. Confirm that you can see "Hello, World!" by navigating to http://127.0.0.1:5000.