Meritshot Tutorials

  1. Home
  2. »
  3. Basics of Flask

Flask Tutorial

Basics of Flask

2.1 "Hello, World!" in Flask

Creating a “Hello, World!” application is the first step to understanding how Flask works. This simple example demonstrates how to set up a Flask application, define routes, and run the application on a local server.

Step 1: Create the Project Directory

  1. Open your terminal or command prompt.
  2. Create a new directory for your project:

mkdir flask_basics

cd flask_basics

  1. (Optional but recommended) Create a virtual environment:

python -m venv venv

    • Activate the virtual environment:

venv\Scripts\activate

      • On macOS/Linux:

source venv/bin/activate

    • Install Flask in the virtual environment:

pip install flask

Step 2: Create the Flask Application

  1. Create a file named app.py in the project directory. This file will contain your Flask application.
  2. Write the following code in app.py:

from flask import Flask

# Create a Flask app instance

app = Flask(__name__)

# Define a route for the root URL (‘/’)

@app.route(‘/’)

def hello_world():

    return “Hello, World!”

# Run the app

if __name__ == ‘__main__’:

    app.run(debug=True)

Explanation of the Code

  1. Import Flask:
    from flask import Flask imports the Flask library to create the application.
  2. Create the App Instance:
    app = Flask(__name__) creates a Flask application instance. The __name__ argument helps Flask locate resources and determine the application’s root path.
  3. Define a Route:
    • @app.route(‘/’) is a decorator that maps the URL ‘/’ (the root URL) to the hello_world function.
    • The function hello_world() returns the response “Hello, World!”, which will be displayed in the browser.
  4. Run the App:
    • app.run(debug=True) starts the Flask development server.

The debug=True option enables automatic reloading and provides detailed error messages for debugging.

Step 3: Run the Flask Application

  1. Open the terminal and navigate to the directory containing app.py:

cd flask_basics

  1. Run the application:

python app.py

  1. You’ll see an output similar to this:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

* Restarting with stat

* Debugger is active!

  1. Open a web browser and go to http://127.0.0.1:5000/.
    You’ll see the message “Hello, World!” displayed.

Key Notes

  • Dynamic Reloading: With debug=True, any changes made to your code will automatically reload the server.
  • Stopping the Server: Press CTRL+C in the terminal to stop the Flask development server.

Frequently Asked Questions

  1. Q: What is Flask?
    A: Flask is a lightweight Python web framework that allows developers to build web applications and APIs quickly and easily. It is known for its simplicity and flexibility.
  2. Q: Why is the “Hello, World!” example important?
    A: This example helps beginners understand the basics of Flask, such as creating an app instance, defining routes, and running a local server.
  3. Q: What does @app.route(‘/’) do?
    A: This decorator maps the root URL (/) of the application to the hello_world function. When a user visits the root URL, the function is executed, and its return value is displayed in the browser.
  4. Q: Why do I need to use if __name__ == ‘__main__’:?
    A: This ensures that the Flask app runs only when the script is executed directly, not when it is imported as a module in another script.
  5. Q: What is the purpose of debug=True?
    A: The debug=True flag enables the debug mode, which automatically reloads the server when code changes are detected and provides detailed error messages for troubleshooting.
  6. Q: How can I access the Flask app from another device on the same network?
    A: Instead of 127.0.0.1, run the app with app.run(host=’0.0.0.0′) to allow access from other devices on the same network. Use your machine’s local IP address to access the app.