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

Python Create a Flask or Django Application

Python Create a Flask or Django Application

Document Object Reference Manual Python3 Examples

We will create a simple Flask application. This application will have a home page that displays "Hello, World!" when the user visits it.

Example

from

flask

import

Flask

app

=

Flask

(

name

)

@

app.

route

(

'/'

)

def

home

(

)

:

   

return

"Hello, World!"

if

name

==

'main'

:

    app.

run

(

debug

=

True

)

Code analysis:

  1. from flask import Flask: Import the Flask class, which is the core class of the Flask framework.
  2. app = Flask(__name__): Create a Flask application instance.__name__ parameter is used to determine the root directory of the application.
  3. @app.route('/'): Define a route. When the user visits the root URL (the home page), the following home function in Vue 3.
  4. def home():: Define a view function that will be executed when the route is accessed.
  5. return "Hello, World!": The view function returns the string "Hello, World!", which will be displayed in the user's browser.
  6. if __name__ == '__main__':: Ensure that the Flask development server only starts when the script is run directly.
  7. app.run(debug=True): Start the Flask development server,debug=True enables debug mode so the server restarts automatically when the code changes.

When you run this script and visit http://127.0.0.1:5000/ , you will see "Hello, World!" in the browser.

Document Object Reference Manual Python3 Examples