Flask First App

Your first Flask application is a simple program that runs a web server on your computer and shows a message in the browser. You write it in about ten lines of Python.

Creating the File

Inside your project folder, create a file called app.py. Open it in any code editor and type the following:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, Flask!'

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

Understanding Each Line

Line 1: Importing Flask

from flask import Flask

This line pulls the Flask class from the flask package. Think of it like borrowing a blueprint from a toolbox.

Line 3: Creating the App Object

app = Flask(__name__)

This creates your Flask application. The __name__ argument tells Flask where your app file lives. Flask uses this to find your templates and static files. When you run app.py directly, __name__ equals "__main__".

Lines 5–7: Your First Route

@app.route('/')
def home():
    return 'Hello, Flask!'

The @app.route('/') line is a decorator. It tells Flask: "When someone visits the root URL /, run the home function." The function returns the text that the browser displays.

User visits: http://127.0.0.1:5000/
Flask sees the route: '/'
Flask runs: home()
Browser shows: Hello, Flask!

Lines 9–10: Running the Server

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

This block starts the Flask development server only when you run app.py directly. The debug=True option turns on two useful features: the server restarts automatically when you edit your code, and a detailed error page appears in the browser when something goes wrong.

Running the Application

Open your terminal, activate the virtual environment, and run:

python app.py

You see this output:

 * Running on http://127.0.0.1:5000
 * Debug mode: on
 * Restarting with stat

Open your browser and go to http://127.0.0.1:5000. The page shows: Hello, Flask!

What 127.0.0.1:5000 Means

http://   127.0.0.1   :   5000
  │           │               │
Protocol   Your own       Port number
           computer       Flask uses

127.0.0.1 always refers to your own machine. Port 5000 is the default port Flask listens on. Nobody else on the internet can reach this server — it runs locally on your computer only.

Adding a Second Route

Let's add a second page at /about:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, Flask!'

@app.route('/about')
def about():
    return 'This is the About page.'

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

Save the file. Because debug mode is on, Flask restarts the server automatically. Visit http://127.0.0.1:5000/about and the browser shows: This is the About page.

Stopping the Server

Press Ctrl + C in the terminal to stop the Flask development server.

The Alternative: Flask CLI

Instead of using app.run(), you can start the server from the terminal using the Flask CLI:

flask --app app run --debug

Both methods produce the same result. The CLI approach separates running logic from application code, which is the preferred style for larger projects.

Debug Mode Warning

Debug mode is only for development on your own computer. It allows anyone with access to the server to run Python code through the browser — which is a security risk. Always turn debug mode off before publishing your application to the internet.

Summary

A Flask app needs three things to work: an app object, at least one route with a function, and a way to start the server. The entire working application fits in ten lines. Every Flask project you ever build grows outward from this same pattern.

Leave a Comment

Your email address will not be published. Required fields are marked *