Creating First Flask App


Step-by-Step: Create Your First Flask App

1. Install Flask

Make sure Flask is installed:

pip install flask

2. Create the Project Structure

/flask_app
├── app.py
├── /templates
│   └── index.html

3. Write Your Flask App

app.py

from flask import Flask, render_template

# Initialize the Flask app
app = Flask(__name__)

# Create a route
@app.route('/')
def home():
    return render_template('index.html')

# Run the app
if __name__ == '__main__':
    app.run(debug=True)

4. Create an HTML Template

templates/index.html

<!DOCTYPE html>
<html>
<head>
    <title>My First Flask App</title>
</head>
<body>
    <h1>Hello, Flask!</h1>
    <p>Welcome to your first web app using Python Flask.</p>
</body>
</html>

5. Run the Flask App

Open your terminal and navigate to the folder, then run:

python app.py

You'll see output like:

Running on http://127.0.0.1:5000/

6. View in Your Browser

Open your browser and go to: http://127.0.0.1:5000/

You will see:

Hello, Flask!
Welcome to your first web app using Python Flask.

Bonus: Add Another Page

Update app.py:

@app.route('/about')
def about():
    return '<h2>This is the About Page</h2>'

Visit: http://127.0.0.1:5000/about

Done!

You've now created a fully functional, simple Flask web app!