Python Create a Flask or Django Application
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:
from flask import Flask: Import the Flask class, which is the core class of the Flask framework.app = Flask(__name__): Create a Flask application instance.__name__parameter is used to determine the root directory of the application.@app.route('/'): Define a route. When the user visits the root URL (the home page), the followinghomefunction in Vue 3.def home():: Define a view function that will be executed when the route is accessed.return "Hello, World!": The view function returns the string "Hello, World!", which will be displayed in the user's browser.if __name__ == '__main__':: Ensure that the Flask development server only starts when the script is run directly.app.run(debug=True): Start the Flask development server,debug=Trueenables 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.
Python3 Examples