basic-hello-world
Learn how to create a basic
Flask Hello World Example
Basic Flask Application
This section demonstrates a fundamental "hello world" application using the Flask microframework in Python. It's a common starting point for learning web development with Flask.
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return 'hello world'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=False)
Understanding the Code
Flask Initialization
The line app = Flask(__name__)
initializes a new Flask application instance. The __name__
argument helps Flask determine the root path of the application.
Defining a Route
The @app.route('/', methods=['GET'])
decorator registers the home
function to handle requests to the root URL ('/') using the GET method. When a user visits the root of the web server, this function will be executed.
Route Handler Function
The home()
function is a simple Python function that returns the string 'hello world'. This string will be displayed in the user's browser.
Running the Application
The if __name__ == '__main__':
block ensures that the app.run()
method is called only when the script is executed directly (not imported as a module). app.run(host='0.0.0.0', port=5000, debug=False)
starts the development server, making the application accessible on your local network.
Further Learning
To expand your knowledge of Flask, consider exploring: