🎓 A1Learner
🔍
0 of 50 lessons completed

Running a Flask Server

The development server runs a Flask app so a browser can visit it. The app.run line starts the server.

The app

This app has one route. The server will serve it to a browser.

from flask import Flask

app = Flask(__name__)

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

Start the server

This line starts the server. It runs until CTRL+C stops it, so it never appears in a test program.

app.run()

Check the app first

The test client checks the app without a server. Run this check before starting the server.

client = app.test_client()
print(client.get("/").data.decode())

Example

from flask import Flask

app = Flask(__name__)

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

client = app.test_client()
print(client.get("/").data.decode())

Key points

  • app.run() starts the development server.
  • The server listens on http://127.0.0.1:5000 unless a port is given.
  • debug=True restarts the server when a file changes.
  • Press CTRL+C in the terminal to stop the server.
  • The test client still works for checks without a server.

In this example, the app has one route that returns "Hello, Flask!". The test client asks for / and prints the page. To serve the app to a browser, add app.run() as the last line and run the file. The server prints Serving Flask app. It then prints Debug mode: off. A warning line follows. Then it prints Running on http://127.0.0.1:5000.

Note

The development server is for learning, not for a public site.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What starts the Flask development server?