Flask First Application
In the previous chapter we successfully installed Flask; next we can create a simple Flask application.
First, create a file named app.py and add the following content:
Example ( app.py )
from
flask
import
Flask
app
=
Flask
(
name
)
@
app.
route
(
'/'
)
def
hello_world
(
)
:
return
'Hello, World!'
if
name
==
'main'
:
app.
run
(
debug
=
True
)
Run the Flask application on the command line:
python app.py
You will see the Flask development server start up and display something similar to the following:
...
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 977-918-914
...
Open a browser and visit http://127.0.0.1:5000/. You should see the message "Hello, World!", indicating that Flask has been installed and is running successfully.

Never name the file flask.py, or it will conflict with the Flask library itself and cause an import error.
Code analysis:
-
from flask import Flask: This line of code imports from
flaskproperty from theFlaskclass.FlaskThe class is the core of the Flask framework and is used to create Flask application instances. -
app = Flask(__name__): This line of code creates a Flask application instance.
__name__is a special Python variable; it is'__main__'when the module is run directly, and is the module's name when it is imported by other modules. Passing__name__AddingFlaskto the constructor allows the Flask application to find and load the configuration file. -
@app.route('/'): This is a decorator that tells Flask which URL should trigger the function below. In this example, it specifies the root URL (i.e., the site's homepage).
-
def hello_world():: This defines a function named
hello_worldwhich will be called when a user visits the root URL. -
return 'Hello, World!': This line of code is
hello_worldthe return value of the function. When a user visits the root URL, this string will be sent back to the user's browser. -
if __name__ == '__main__':: This line is a conditional check to see whether this module is being run directly rather than imported by another module. If it is run directly, the following code block will be executed.
-
app.run(debug=True): This line calls the
runmethod of the Flask application instance, starting Flask's built-in development server.debug=TrueThe parameter enables debug mode, which means the application automatically reloads when the code changes and provides a debugger when an error occurs.
If you have installed python-dotenv, you can create a .flaskenv file in the project directory to simplify the command:
# File path: .flaskenv
FLASK_APP=app
FLASK_DEBUG=True

Once configured, simply run flask run without having to specify the --app parameter each time:
(.venv) $ flask run
* Serving Flask app 'app'
* Debug mode: on
* Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
If you name the file app.py or wsgi.py, Flask can discover it automatically, and even --app can be omitted. However, specifying it explicitly is a good habit.
Debug mode
It is recommended to enable Debug mode during development; it provides two important features:
- Automatic reload: After code is modified and saved, the server restarts automatically with no manual action required
- Interactive debugger: When the code raises an error, the browser displays detailed error information and a call stack
How to enable it:
(.venv) $ flask --app app run --debug
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 123-456-789
Security Warning: The interactive debugger in Debug mode allows arbitrary Python code to be executed in the browser. Although it is protected by a PIN, never enable Debug mode in a production environment。
Comparison of running methods
Besides the flask run command, you will see another way to run the application:
Example
Not recommended: Calling app.run() in the code
if
name
==
"main"
:
app.
run
(
debug
=
True
)
Then run it with python app.py.
| How to run | Advantages | Disadvantages |
|---|---|---|
| flask run | Supports command-line arguments (--host, --port, --debug) for flexible configuration; automatically loads .flaskenv | Requires remembering command-line arguments |
| python app.py | Simple and intuitive | Inflexible; debug mode requires changing the code; officially discouraged by Flask |
The Flask team officially recommends using the flask run command. If you write app.run() in your code and also use flask run, Flask will intelligently ignore the app.run() call and display a yellow warning.
Adding more content to the response
A view function can return not only a simple string but also HTML content:
Example
File path: app.py
from
flask
import
Flask
app
=
Flask
(
name
)
@
app.
route
(
"/"
)
def
index
(
)
:
When returning multiple lines of HTML, Flask automatically sets the Content-Type to text/html
return
"""
Welcome to the RUNOOB Flask Tutorial
This is a Web application built with Flask.
- Learn about the routing system
- Learn about template rendering
- Learn about database operations
"""
Visit http://127.0.0.1:5000/, and the output is as follows:

Of course, writing HTML directly in Python code quickly becomes hard to maintain; later chapters will show how to use templates to separate HTML from Python code.