0% found this document useful (0 votes)
96 views

Build A Python Web Server With Flask - Add A New Page - Raspberry Pi Projects-13

This document discusses adding a new page to a Flask web app by creating a new route. It explains that routes determine the paths into a website based on the URL. It shows an example of an existing 'Hello world' route and then instructs the reader to add a new '/cakes' route that returns the text 'Yummy cakes!'. The reader is told to save the code and navigate to the new '/cakes' page in their browser to see the new content.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Build A Python Web Server With Flask - Add A New Page - Raspberry Pi Projects-13

This document discusses adding a new page to a Flask web app by creating a new route. It explains that routes determine the paths into a website based on the URL. It shows an example of an existing 'Hello world' route and then instructs the reader to add a new '/cakes' route that returns the text 'Yummy cakes!'. The reader is told to save the code and navigate to the new '/cakes' page in their browser to see the new content.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

29/5/2019 Build a Python Web Server with Flask - Add a new page | Raspberry Pi Projects

Home English

Build a Python Web Server with Flask


HTML / CSS Python

Contents

Add a new page

Now you’re going to add a new page to your web app by creating a new route.

In a web application, a route is a certain path into your website, determined by the URL the user types
into their web browser’s address bar. It’s up to you which routes are enabled and what each of them
does.

In the ‘Hello world’ example, we used a single route:

@app.route('/')
def index():
return 'Hello world'
This route is made up of three parts:

@app.route('/'): this determines the entry point; the / means the root of the website, so
http://127.0.0.1:5000/
def index(): this is the name you give to the route; this one is called index, because it’s the index
(or home page) of the website
return 'Hello world': this is the content of the web page, which is returned when the user goes
to this URL

The 2nd part of the app.py code runs the web server and your app:

https://projects.raspberrypi.org/en/projects/python-web-server-with-flask/3 1/3
29/5/2019 Build a Python Web Server with Flask - Add a new page | Raspberry Pi Projects

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Note: the host='0.0.0.0' means the web app will be accessible to any device on the network.

The following instructions will show how to create a new page and route called ‘cakes’, but feel free to
change this name and content to be whatever you want.

Create a new route by adding these lines of code below the rst route:

@app.route('/cakes')
def cakes():
return 'Yummy cakes!'
Complete code

Save your code and navigate to your ‘cakes’ page in the browser at 127.0.0.1:5000/cakes. You
should see a web page with the text “Yummy cakes!” on it:

Using HTML

Spotted a mistake? Enjoying the project? Any opinions on the website? Let us know!

https://projects.raspberrypi.org/en/projects/python-web-server-with-flask/3 2/3
29/5/2019 Build a Python Web Server with Flask - Add a new page | Raspberry Pi Projects

Send feedback

Published by Raspberry Pi Foundation under a Creative Commons license.


View project & license on GitHub

Cookies Policy Privacy Policy

https://projects.raspberrypi.org/en/projects/python-web-server-with-flask/3 3/3

You might also like