Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask First Application

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:

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:

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.


   
    """

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.