Before We Begin · Install Flask
One-timeYou already have Python and VS Code from the Python course. Flask is a free add-on for Python, so we install it once.
- Make a project folder
- Make a folder called
my_site. - In VS Code, choose File → Open Folder and pick it.
- Make a folder called
- Open the terminal
- In VS Code, choose Terminal → New Terminal. A panel opens at the bottom.
- Install Flask
- Type
pip install flaskand press Enter. - On Windows, if
pipis not found, typepy -m pip install flaskinstead. - Wait for the line that starts
Successfully installed.
- Type
Write down exactly what the terminal says and ask in class. Install problems are common, and we will fix them together.
Learning Goals
3 minBy the end of this lesson you can:
- Explain what happens when a browser opens a website.
- Write an HTML page with
<head>,<title>,<body>,<header>,<p>and<a>. - Run a Flask app that sends your page to the browser.
Warm-Up · Peek Behind a Page
5 minOpen any website you like in your browser.
Right-click an empty part of the page and choose View page source. A new tab fills with text.
Press Ctrl + F (⌘ + F on a Mac) and search for <title>. What words come straight after it? Now look at the name on the browser tab.
That text is HTML. Every page you have ever visited arrived as HTML first. Today you write your own.
New Concept · HTML and Flask
12 minHow a website reaches you
When you type a web address, your browser sends a request to a computer called a server.
The server answers with a page written in HTML. Your browser reads the HTML and draws the page.
Flask lets Python be that server. Python decides which page to send, and Flask sends it.
HTML is made of tags
A tag is a word inside angle brackets, like <p>. Most tags come in pairs: an opening tag and a closing tag with a slash.
<p>Hello, web!</p>
The opening tag, the words inside and the closing tag together make an element. This one is a paragraph.
The six tags for today
Today's tags
- <head>
- Information about the page. Nothing in it shows on the page itself.
- <title>
- Goes inside the head. Its words appear on the browser tab.
- <body>
- Everything the visitor sees on the page.
- <header>
- The top part of the page, such as the site's name.
- <p>
- A paragraph of text.
- <a>
- A link. Its
hrefsays where the link goes.
Extra information inside an opening tag is called an attribute. Here, href is the attribute:
<a href="https://www.python.org">Visit the Python website</a>
The visitor sees only the words between the tags, and clicks them.
Flask in four lines
A Flask app connects an address to a Python function. That connection is called a route.
@app.route("/") def home(): return render_template("index.html")
"/" means the home page. When a browser asks for it, Flask runs home().
render_template finds index.html in a folder called templates and sends it back.
Worked Example · My First Site
12 minWe need two files. Set up your my_site folder like this:
my_site/
├── app.py
└── templates/
└── index.htmlStep 1 · The page
Make a folder called templates inside my_site. Inside it, make index.html and type this:
templates/index.html
<!DOCTYPE html>
<html>
<head>
<title>My First Site</title>
</head>
<body>
<header>
<p>Welcome to my site!</p>
</header>
<p>This page was sent by Python and Flask.</p>
<p>I am learning to build websites.</p>
<a href="https://www.python.org">Visit the Python website</a>
</body>
</html>The first line tells the browser this is a modern HTML page. Everything else sits inside <html>.
Notice the spaces at the start of lines. Tags inside other tags move in a little, so you can see what belongs where.
Step 2 · The Flask app
Now make app.py in my_site, next to the templates folder:
app.py
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def home(): return render_template("index.html") if __name__ == "__main__": app.run(debug=True)
Flask(__name__) creates the app. Copy it exactly for now; Flask uses it to find your files.
debug=True restarts the app when you save, and shows clear error pages.
Step 3 · Run it
In the terminal, type python app.py and press Enter. You should see a line like this:
Output
* Running on http://127.0.0.1:5000
Open http://127.0.0.1:5000 in your browser. Your page appears, and the tab says My First Site.
To stop the app, click the terminal and press Ctrl + C.
The folder must be called exactly templates, with an s. If not, Flask shows a TemplateNotFound error.
On a Mac, port 5000 is sometimes busy. Use app.run(debug=True, port=5001) and open port 5001 instead.
Basic
5 minBuild the worked example yourself, then make one change.
Make app.py and templates/index.html as in the worked example. Run the app and open your page.
Then change the words inside <title> to your own site name. Save and refresh the browser. Did the tab change?
Hint
Keep the app running. Save index.html, then press F5 in the browser to load the page again.
Challenge 1 · About My Hobby
4 minTurn your page into a page about a hobby you enjoy.
- The
<header>shows the name of your hobby. - The body has three paragraphs about it.
- The browser tab shows the hobby's name too.
Hint
Each paragraph needs its own <p> and </p>. Put the paragraphs after the closing </header>.
Challenge 2 · Links Out
4 minAdd two links to websites about your hobby.
- Each link has its own
<a>element. - The link words say where they go. Avoid click here.
- Click both links to check they work.
Hint
Copy the full address from your browser's address bar into href="…". It must start with https://.
Challenge 3 · A Second Page
8 minA real site has more than one page. Give yours an About page.
- Make
templates/about.htmlwith its own title, header and a paragraph about you. - In
app.py, add a route for/aboutthat sendsabout.html. - On the home page, add a link to
/about. On the About page, add a link back to/.
Test it at http://127.0.0.1:5000/about, then click your links both ways.
Hint
Copy the whole home route and change three things: the address, the function name and the file name.
A link to your own page needs no https://. Use href="/about".
Recap
3 minToday Python became a web server. A browser asked for a page, Flask ran a function, and the function sent back our HTML.
Vocabulary Card
- HTML
- The language every web page is written in.
- tag / element
<p>is a tag. Opening tag, content and closing tag together are an element.- attribute
- Extra information in an opening tag, like
hrefon a link. - server
- The computer that answers a browser's request with a page.
- route
- A link between a web address and a Python function.
- template
- An HTML file in the
templatesfolder that Flask sends.
Extra Mission · A Three-Page Site
4 minGrow your site to three pages: Home, About and Favourites.
- Each page has its own route and its own template.
- Each page has a different
<title>. - Every page's
<header>holds links to all three pages, so a visitor can go anywhere. - The Favourites page lists three favourite things, one paragraph each.
Bring your my_site folder to the next lesson.
Sample · app.py
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def home(): return render_template("index.html") @app.route("/about") def about(): return render_template("about.html") @app.route("/favourites") def favourites(): return render_template("favourites.html") if __name__ == "__main__": app.run(debug=True)
Sample · templates/favourites.html
<!DOCTYPE html>
<html>
<head>
<title>My Favourites</title>
</head>
<body>
<header>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/favourites">Favourites</a>
</header>
<p>My favourite food is pizza.</p>
<p>My favourite sport is swimming.</p>
<p>My favourite place is the beach.</p>
</body>
</html>The other two templates use the same header. Yours can say anything, as long as every page links to all three.