Your First App

With Flask installed inside bookshelf-env, it's time to prove it works — the smallest possible Flask application, run two different ways, printing a greeting from Bookshelf in your browser. Nothing about this app is throwaway code either; every line here is exactly how real Flask routes are written, just before any database or template gets involved.

Writing the Smallest Possible Flask App

Inside your activated Bookshelf project folder, create a file named app.py with the following three ideas: import the Flask class, create an instance of it, and attach one route to it.

Example: app.py

from flask import Flask

app = Flask(__name__)

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

Flask(__name__) creates the application object — passing __name__ tells Flask where this file lives, so it can correctly locate related resources like templates and static files later on. @app.route('/') is a decorator that registers the function immediately below it, home(), as the view function Flask calls whenever a request comes in for the path /. Whatever that function returns becomes the response sent back to the browser — here, a plain string.

Running It with flask run

Flask ships its own command-line tool for starting a development server. From the same folder as app.py, with bookshelf-env activated, run:

flask run

Flask needs to know which file holds your application before it can run it, and it looks for an environment variable named FLASK_APP to find out. If your file is named app.py, Flask actually finds it automatically with no extra step — that filename is one of the defaults it checks. For any other filename, set the variable explicitly first:

Operating SystemSetting FLASK_APP
Windows (Command Prompt)set FLASK_APP=app.py
Windows (PowerShell)$env:FLASK_APP="app.py"
Mac / Linux (bash or zsh)export FLASK_APP=app.py

Once running, flask run prints a local address in the terminal, almost always http://127.0.0.1:5000. Opening that address in a browser should show the plain text "Hello, Bookshelf!" — the response our home() view function returned.

The Alternative: python app.py

A second, equally common way to run a Flask app during development is to make the file executable on its own, by adding a small block at the bottom that calls app.run() directly:

Example: app.py (runnable directly)

from flask import Flask

app = Flask(__name__)

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

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

With this block in place, the file can be started with a plain Python command instead of the flask CLI:

python app.py

The if __name__ == '__main__': guard means this block only runs when the file is executed directly (as in python app.py) — not when the file is imported by something else, such as a test suite or a WSGI server in production. Both approaches start the same development server; which one you reach for is mostly a matter of habit, though flask run is the more idiomatic choice for local development.

What debug=True Actually Gives You

Passing debug=True (whether to app.run() or via Flask's debug setting when using flask run) turns on two genuinely useful development features:

  • Auto-reload — the server watches your source files and restarts itself automatically whenever you save a change, so you don't have to stop and restart it by hand after every edit
  • The interactive debugger — if a view function raises an unhandled exception, instead of a bare error page you get a full interactive traceback in the browser, including the ability to inspect variables at each stack frame
Note: Never enable debug=True on a server exposed to the public internet. The interactive debugger can execute arbitrary Python code from inside the browser — a tremendous convenience during local development, and a serious security hole if anyone outside your machine can reach it. This comes up again, with concrete guidance, in the later Security Best Practices and Deployment lessons.

Full Walkthrough: Empty Folder to Running Server

Putting the whole sequence together, here's every step from nothing to a running Bookshelf greeting in the browser:

  1. Create and enter a project folder, e.g. mkdir bookshelf && cd bookshelf
  2. Create the virtual environment: python -m venv bookshelf-env
  3. Activate it (see the activation table in the previous lesson)
  4. Install Flask: pip install flask
  5. Create app.py with the Flask import, the app instance, and the / route shown above
  6. Run it with flask run (or add the app.run(debug=True) block and use python app.py)
  7. Open http://127.0.0.1:5000 in a browser and confirm "Hello, Bookshelf!" appears

That's a complete, working Flask application — small enough to fit in one file, but built from exactly the same pieces every larger Flask app uses: an application instance, one or more routes, and view functions that return responses.

Next up: A single app.py works fine for a demo, but it won't stay pleasant to work in as Bookshelf grows. Head to Project Structure & App Factory to see how larger Flask projects organize themselves.