CHAPTER 04
Beginner
Creating Your First Flask Application
Updated: May 14, 2026
25 min read
# CHAPTER 4
Creating Your First Flask Application
1. Introduction
The theory is out of the way; it is time to write code. Unlike heavy frameworks that require massive configuration files just to print "Hello World", Flask requires only a single Python file. In this chapter, we will write our first Flask script, instantiate the application object, define a basic URL route, and launch the built-in development server.2. Learning Objectives
By the end of this chapter, you will be able to:- Instantiate a Flask application object.
-
Use the
@app.routedecorator to map a URL to a Python function.
- Start the Flask development server using the command line.
-
Enable
debugmode for automatic reloading.
3. Beginner-Friendly Explanation
Imagine setting up a new telephone switchboard. First, you have to buy the actual switchboard machine (Instantiating the Flask App). Next, you plug in cables. You say, "When someone dials Extension 1 (the/ URL), connect them to Alice's desk (the home() Python function)." This is called Routing.
Finally, you turn the power button on (Running the Server) so the machine starts listening for incoming phone calls.
4. Step 1: Writing the Code
Ensure your virtual environment is active(venv).
Create a new file in your project folder named app.py.
Open app.py in your code editor and write these exactly 7 lines:
python
5. Step 2: Code Breakdown
Let's analyze what we just wrote:-
Flask(_name): This creates the core application object.name_is a special Python variable that tells Flask where to look for hidden files like templates and static assets.
-
@app.route('/'): This is a Python Decorator. It tells Flask's traffic cop: "If a user visits the root homepage (/), execute the function directly below this line."
-
return "<h1>...": The string returned by the function is packaged into an HTTP Response and sent to the user's browser.
-
app.run(debug=True): This starts the local server. Settingdebug=Trueis critical for development; it provides helpful error screens and automatically restarts the server when you save changes to your code!
6. Step 3: Running the Server
Open your terminal (ensure(venv) is active, and you are in the same folder as app.py).
Type the following command:
bash
You should see output similar to this:
text
7. Step 4: Viewing Your App
Open Google Chrome or Firefox. In the address bar, type:http://127.0.0.1:5000/ (or http://localhost:5000/).
You will see your text: Hello, World! This is my first Flask app. displayed in big, bold <h1> letters!
8. Backend Workflow: Stopping and Restarting
Your terminal window is now "locked." It is actively listening to the internet port 5000. If you want to type new terminal commands, you must stop the server.- To stop the server, click into the terminal and press Ctrl + C.
-
Because we enabled
debug=True, if you change the text inapp.pyto "Welcome to Python" and hit Save, you do *not* need to restart the server manually. Flask detects the change, restarts itself, and you simply refresh your web browser!
9. Best Practices
-
The Application Name: While you can name your file anything, it is an industry standard to name your main entry point
app.pyorwsgi.py. Do not name your fileflask.py, as this will confuse Python's import system, causing it to import your local file instead of the actual Flask library, resulting in massive crash errors.
10. Common Mistakes
-
Port Already in Use: If you run
python app.pyand get a redOSError: [Errno 98] Address already in use, it means another application (or another instance of Flask you forgot to shut down) is currently using Port 5000. You must either kill the old process or tell Flask to run on a different port:app.run(port=8080).
11. Exercises
-
1.
Explain the purpose of the
@app.route('/')decorator. What happens if a user visits a URL that does not have a corresponding route defined in your code?
12. Coding Challenges
-
Challenge: Keep your server running. In
app.py, add a second decorator and function below the first one. Map the URL/contactto a function calledcontactpage(). Have it return the stringContact us at email@example.com. Save the file, navigate tohttp://127.0.0.1:5000/contactin your browser, and verify it works.
13. MCQs with Answers
Question 1
In the code app = Flask(name), what is the primary purpose of passing the special name_ variable?
Question 2
What is the benefit of including the debug=True argument when starting the Flask development server via app.run()?
14. Interview Questions
-
Q: Explain the concept of a Python "Decorator" in the context of Flask routing. How does
@app.routemap an HTTP request to a specific block of logic?
-
Q: Why must you absolutely ensure that
debug=Trueis removed or set toFalsebefore deploying a Flask application to a live, public-facing production server?
15. FAQs
Q: Can I return multiple HTML elements, like an<h1> and a <p> tag?
A: Yes. You can return an entire HTML document as a giant string: return "<h1>Title</h1><p>Paragraph</p>". However, writing HTML inside a Python string is terrible practice. We will solve this using Templates in Chapter 6.
16. Summary
In Chapter 4, we brought our environment to life. We wrote a minimal 7-line Python script that instantiated a Flask application object. We utilized the@app.route decorator to map the root URL to a Python View function, returning an HTTP response to the browser. Finally, we launched the built-in development server, enabling debug mode for a seamless developer experience.