First Flask App
Now you will write your first Flask application – the classic "Hello, World!" It will run on your local computer and be accessible via a web browser.
Step 1: Create the Application File
Create a file named `app.py` inside your project folder.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)Step 2: Run the Application
In the terminal (with virtual environment activated), run:
python app.pyYou will see output like: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)Step 3: View in Browser
Open a web browser and go to `http://127.0.0.1:5000/`. You should see "Hello, World!" displayed.
Understanding the Code
- `Flask(__name__)`: creates the Flask application instance.
- `@app.route('/')`: decorator that tells Flask which URL triggers the function.
- `def hello()`: the function that runs when the URL is visited.
- `app.run(debug=True)`: starts the development server with debug mode (auto‑reloads on code changes).
Two Minute Drill
- Create a Flask app instance with `Flask(__name__)`.
- Use `@app.route('/')` to map the root URL.
- Return a string to display it in the browser.
- Run with `python app.py` and visit `http://127.0.0.1:5000`.
Need more clarification?
Drop us an email at career@quipoinfotech.com
