from flask import Flask
# Create a Flask application
app = Flask(__name__)
# Define a route and its corresponding function
@app.route('/')
def hello():
return 'Hello, World!'
# Run the Flask application
if __name__ == '__main__':
app.run(debug=True)
- Import the Flask class from the
flask
module. - Create a Flask application instance by initializing
app = Flask(__name__)
. - Define a route using the
@app.route()
decorator. In this example, the route '/'
represents the root URL. - Define a function that will be executed when the defined route is accessed. This function returns the string
'Hello, World!'
. - Start the Flask application by using
app.run(debug=True)
. This method runs the application on a development server. - The condition
if __name__ == '__main__':
ensures that the app is executed only if it's run directly, not if it's imported as a module in another file.