🎓 A1Learner
🔍
0 of 50 lessons completed

Your First Flask App

A Flask app is a Python program that answers web requests. The first app answers one address with one line of text.

Make the app

This line makes the Flask app. Every route in the lesson belongs to it.

from flask import Flask

app = Flask(__name__)

Add the route

The decorator marks home as the answer for /. The function returns the page text.

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

Ask the test client

The test client calls the app without a server. It asks for the address and prints the page it gets back.

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

  • Flask(__name__) makes the app.
  • @app.route("/") marks home as the answer for the address /.
  • A view function is an ordinary Python function that returns a page.
  • The test client calls the app without a server.
  • Use the test client to check an app while writing it.

In this example, the app is made first. The route marks home as the answer for /. The view returns "Hello, Flask!". The test client then asks for / and prints the page the app returns.

Note

The app object is named app by habit, and this course keeps that name.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does app = Flask(__name__) do?