You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/step_1.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ You'll set up a web server and create a simple website using Flask, Python, and
6
6
7
7

8
8
9
-
The web server will be able to react to the user inputting dynamic content, turning your website into a web application capable of doing more than just showing static information.
9
+
The web server will be able to react to the dynamic content that the user inputs, so your website will be a web application that can more than just show static information.
10
10
11
11
--- collapse ---
12
12
@@ -16,7 +16,7 @@ title: What you will need
16
16
17
17
### Hardware
18
18
19
-
+Computer capable of running Python 3
19
+
+A computer capable of running Python 3
20
20
21
21
### Software
22
22
@@ -30,7 +30,7 @@ title: What you will need
30
30
title: What you will learn
31
31
---
32
32
33
-
- How to install Python modules using pip
33
+
- How to install Python modules using `pip`
34
34
- How to build a basic web app with Python and Flask
35
35
36
36
This resource covers elements from the following strands of the [Raspberry Pi Digital Making Curriculum](https://www.raspberrypi.org/curriculum/):
Copy file name to clipboardExpand all lines: en/step_3.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
Now you're going to add a new page to your web app by creating a new **route**.
4
4
5
-
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.
5
+
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. You create the routes and determine what each route does.
6
6
7
-
In the 'Hello world' example, we used a single route:
7
+
In the code you already have in `app.py`, there is a single route:
8
8
9
9
```python
10
10
@app.route('/')
@@ -15,23 +15,23 @@ def index():
15
15
This route is made up of three parts:
16
16
17
17
-`@app.route('/')`: this determines the entry point; the `/` means the root of the website, so `http://127.0.0.1:5000/`
18
-
-`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
18
+
-`def index()`: this is the name you give the route, in this case `index`, because it's the index (or homepage) of the website
19
19
-`return 'Hello world'`: this is the content of the web page, which is returned when the user goes to this URL
20
20
21
-
The 2nd part of the `app.py` code runs the web server and your app:
21
+
The second half of the `app.py` code runs the web server and your app:
22
22
23
23
```python
24
24
if__name__=='__main__':
25
25
app.run(debug=True, host='0.0.0.0')
26
26
```
27
27
28
-
**Note:** the `host='0.0.0.0'` means the web app will be accessible to any device on the network.
28
+
**Note:** the `host='0.0.0.0'` means the web app is accessible to any device on the network.
29
29
30
-
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.
30
+
The following instructions show how to create a new page and route called 'cakes'. If you want, you can change the name and and the content to be whatever you wish.
31
31
32
32
--- task ---
33
33
34
-
Create a new route by adding these lines of code below the first route:
34
+
To create a new page and a route to it, add these lines of code below where the first page and route are created in `app.py`:
35
35
36
36
```python
37
37
@app.route('/cakes')
@@ -70,7 +70,7 @@ if __name__ == '__main__':
70
70
71
71
--- task ---
72
72
73
-
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:
73
+
Save your code and navigate to the 'cakes' page in the browser at the address `127.0.0.1:5000/cakes`. You should see a web page with the text "Yummy cakes!" on it.
Copy file name to clipboardExpand all lines: en/step_4.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
## Using HTML
1
+
## Return HTML web pages
2
2
3
3
Next, you'll modify your existing routes to return a full HTML web page rather than just simple text.
4
4
5
-
The HTML web page will be created from a **template** that holds the static content of the page. You'll later learn how to insert data to create a dynamic version of the page.
5
+
The HTML web page will be created from a **template** that holds the static content of the page. In a later section of this project, you'll learn how to insert data to create a dynamic version of the page.
6
6
7
7
--- task ---
8
8
9
-
First, create a `templates` directory in your `webapp` directory by entering this into the terminal or command prompt window:
9
+
First, create a `templates` directory in your `webapp` directory by entering the following command into the terminal or command prompt window:
10
10
11
11
```bash
12
12
mkdir templates
@@ -54,15 +54,15 @@ from flask import Flask, render_template
54
54
55
55
--- task ---
56
56
57
-
Finally, you'll need to modify your `index()` function to return the `index.html` HTML template instead of the normal text. Change the code inside the definition so that the code looks like this:
57
+
Finally, modify your `index()` function to return the `index.html` HTML template instead of the normal text. Change the code inside the definition so that the code looks like this:
58
58
59
59
```python
60
60
@app.route('/')
61
61
defindex():
62
62
return render_template('index.html')
63
63
```
64
64
65
-
Flask will look for `index.html` in the `templates` directory that the `app.py` program is in.
65
+
This code makes Flask look for `index.html` in the `templates` directory that the `app.py` program is in.
66
66
67
67
--- /task ---
68
68
@@ -78,7 +78,7 @@ Load the `http://127.0.0.1:5000/` page in your web browser to see your new HTML
78
78
79
79

80
80
81
-
In this case it's not much different, as all you've done is added a HTML header, but there's plenty of scope to expand!
81
+
In this case what you see is not much different, because the only new thing is a HTML header. There's plenty of scope to add other things!
Copy file name to clipboardExpand all lines: en/step_6.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Now you'll add some Cascading Style Sheets (CSS) to add colour to your web page.
4
4
5
5
--- task ---
6
6
7
-
First, return to the terminal/command prompt window and navigate to the `webapp` directory. If you're in the `templates` directory, go back up one level with `cd ..`.
7
+
First, return to the terminal/command prompt window and navigate to the `webapp` directory. If you're in the `templates` directory, go up one level with the command`cd ..`.
8
8
9
9
--- /task ---
10
10
@@ -20,7 +20,7 @@ mkdir static
20
20
21
21
--- task ---
22
22
23
-
Create a new file in IDLE by clicking **File** and **New File**, and save this file as `style.css` in the `static` folder you just created.
23
+
Create a new file in IDLE by clicking **File** and **New File**, and save this file as `style.css` in the `static` folder.
24
24
25
25
--- /task ---
26
26
@@ -37,7 +37,7 @@ body {
37
37
38
38

39
39
40
-
**Note:**you have used colour names here, but you could also create colours using hex codes like `#ff0000` (red).
40
+
**Note:**this code contains colour names, but you could also create colours using hex codes like `#ff0000` (red).
41
41
42
42
--- /task ---
43
43
@@ -66,15 +66,15 @@ Now modify your `index.html` HTML template to include the CSS rules by adding a
66
66
67
67
--- task ---
68
68
69
-
Save the change to `index.html` and refresh your browser. You should see a colourful version of the web app!
69
+
Save the change to `index.html` and refresh your browser. You should see a colourful version of your web app!
70
70
71
71

72
72
73
73
--- /task ---
74
74
75
-
If your web app doesn't look right, you may not have saved your CSS file in the right place.
75
+
If your web app doesn't look right, your CSS file might not be in the right directory.
76
76
77
-
By now you've created a number of files and directories for your web app. It is worth quickly double-checking your `webapp` project directory, which should contain the following and have a structure like this now:
77
+
You now have a number of files and directories for your web app. It is worth making sure your `webapp` project directory contains the following files and has the following structure:
Copy file name to clipboardExpand all lines: en/step_7.md
+17-15Lines changed: 17 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,24 @@
1
-
## Adding dynamic content
1
+
## Add dynamic content
2
2
3
-
So far you've learned to deliver static HTML web pages using templates. Lets add some dynamic content to the pages to display different information. Large websites like Facebook, YouTube and BBC News show different content depending on the page you visit, even though the templates are very similar.
3
+
Now you know how to deliver static HTML web pages using templates. Large websites like Facebook, YouTube and BBC News have dynamic content: these websites show different content depending on the page you visit, even though the templates are very similar.
4
4
5
-
Now you'll create a new route on your website so that when you go to `http://127.0.0.1/hello/name`, it will say "Hello name!" and replace 'name' with whatever you put there; so `/hello/Paul/` will display "Hello Paul!".
5
+
You will now add some dynamic content to your pages so they can display different information.
6
+
7
+
Now you will create a new route on your website so that when you go to `http://127.0.0.1/hello/name`, the page says 'Hello name!', replacing 'name' with whatever you put there. So for example, `/hello/Dana/` displays 'Hello Dana!'.
6
8
7
9
--- task ---
8
10
9
-
Create a new route in your application like so:
11
+
Add the following code to create a new route in your application:
10
12
11
13
```python
12
14
@app.route('/hello/<name>')
13
15
defhello(name):
14
16
return render_template('page.html', name=name)
15
17
```
16
18
17
-
-`@app.route('/hello/<name>')` - the `<name>` part means it passes the name into the `hello` function as a variable called `name`
18
-
-`def hello(name)` - this is the function that determines what content is shown - this time it takes the given name as a parameter
19
-
-`return render_template('page.html', name=name)` - here we look up the template `page.html` and pass in the variable `name` from the URL, so the template can use it
19
+
-`@app.route('/hello/<name>')`: the `<name>` part means it passes the name into the `hello` function as a variable called `name`.
20
+
-`def hello(name)`: this is the function that determines what content is shown. Here, the function takes the given name as a parameter.
21
+
-`return render_template('page.html', name=name)`: this code looks up the template `page.html` and passes in the variable `name` from the URL so that the template can use it.
20
22
21
23
--- /task ---
22
24
@@ -36,11 +38,11 @@ Create a new HTML template called `page.html`, and add the following HTML code t
36
38
37
39
--- task ---
38
40
39
-
Save the files and visit `http://127.0.0.1:5000/hello/paul`. It should look like this:
41
+
Save the files and visit `http://127.0.0.1:5000/hello/paul`. The page you see should look like this:
40
42
41
43

42
44
43
-
Try it with different names!
45
+
Try `http://127.0.0.1/hello/name` with different names!
44
46
45
47
--- /task ---
46
48
@@ -50,23 +52,23 @@ Try it with different names!
50
52
title: What's happening here?
51
53
---
52
54
53
-
Flask uses `jinja`, a Python library for rendering templates. Using the braces (curly brackets) on this line:
55
+
Flask uses `jinja`, a Python library for rendering templates. Look at this code with the braces (curly brackets):
54
56
55
57
```html
56
58
<h1>Hello {{ name }}!</h1>
57
59
```
58
60
59
-
... tells the template to render the variable `name`which was passed in the route function `hello`.
61
+
This code tells the template to render the variable `name`that was passed in the route function `hello`.
60
62
61
-
Visiting `127.0.0.1:5000/hello/` without a name will create an error. Think about how you can prevent this from happening.
63
+
Visiting `127.0.0.1:5000/hello/` without a name creates an error. Try to think of a way to prevent this error.
62
64
63
65
--- /collapse ---
64
66
65
67
--- task ---
66
68
67
-
Create a link to your new dynamic hello page from your index.
69
+
Create a link to your new, dynamic hello page from your index.
68
70
69
-
Edit `index.html` to add a link to your new hello page under the heading.
71
+
Edit `index.html` to add a link to the hello page under the heading.
70
72
71
73
```html
72
74
<h1>My website</h1>
@@ -77,7 +79,7 @@ Edit `index.html` to add a link to your new hello page under the heading.
77
79
78
80
--- task ---
79
81
80
-
Save your changes and refresh the index page to see the result.
82
+
Save the changes to `index.html`, and then refresh the index page in the browser to see the updated version.
Copy file name to clipboardExpand all lines: en/step_8.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
- Add more CSS rules to each of your pages
4
4
- Create links to your favourite web pages
5
-
- Learn more about HTML, CSS and web development with [Coder Dojo](https://projects.raspberrypi.org/en/CoderDojo/21), [Mozilla Developer Network](https://developer.mozilla.org/en-US/Learn) and [Codecademy](https://www.codecademy.com/en/tracks/web)
6
-
- Learn more about Flask using the [Flask documentation](http://flask.pocoo.org/docs)
7
-
- Create a physical computing project with Raspberry Pi and use Flask to create a web interface to it, see [Matt Richardson's guide](http://mattrichardson.com/Raspberry-Pi-Flask/index.html) or [Ben Nuttall's BETT Bot](https://github.com/bennuttall/bett-bot)
5
+
- Learn more about HTML, CSS, and web development with [CoderDojo](https://projects.raspberrypi.org/en/CoderDojo/21), [Mozilla Developer Network](https://developer.mozilla.org/en-US/Learn) and [Codecademy](https://www.codecademy.com/en/tracks/web)
6
+
- Learn more about Flask from the [Flask documentation](http://flask.pocoo.org/docs)
7
+
- Create a physical computing project with Raspberry Pi and use Flask to create a web interface to it — you could use [Matt Richardson's guide](http://mattrichardson.com/Raspberry-Pi-Flask/index.html) or [Ben Nuttall's BETT Bot code](https://github.com/bennuttall/bett-bot) for help
0 commit comments