minimal flask application import
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Explanation:
from flask import Flask
: Imports theFlask
class from the Flask module, allowing us to create a Flask web application.app = Flask(__name__)
: Creates an instance of the Flask class. The__name__
variable represents the name of the current Python module. This is used by Flask to determine the root path of the application.@app.route('/')
: Decorator that creates a mapping between the URL ('/') and the function that handles requests to this URL.def hello():
: Defines a function namedhello
that will be called when the specified route ('/') is accessed.return 'Hello, World!'
: Returns the string 'Hello, World!' as the response when the '/' route is accessed.if __name__ == '__main__':
: Checks if the script is being run directly (not imported as a module).app.run()
: Starts the Flask development server to handle incoming requests. The server listens for requests on the default IP address (127.0.0.1) and port (5000).
This code sets up a basic Flask application that responds with 'Hello, World!' when the root URL ('/') is accessed in a web browser or through an HTTP request.