Skip to content

Commit b6bf19f

Browse files
authored
Merge pull request #25 from raspberrypilearning/draft
Draft
2 parents da02e57 + 2c18587 commit b6bf19f

File tree

8 files changed

+61
-59
lines changed

8 files changed

+61
-59
lines changed

en/step_1.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You'll set up a web server and create a simple website using Flask, Python, and
66

77
![flask web app](images/showcase.png)
88

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.
1010

1111
--- collapse ---
1212

@@ -16,7 +16,7 @@ title: What you will need
1616

1717
### Hardware
1818

19-
+ Computer capable of running Python 3
19+
+ A computer capable of running Python 3
2020

2121
### Software
2222

@@ -30,7 +30,7 @@ title: What you will need
3030
title: What you will learn
3131
---
3232

33-
- How to install Python modules using pip
33+
- How to install Python modules using `pip`
3434
- How to build a basic web app with Python and Flask
3535

3636
This resource covers elements from the following strands of the [Raspberry Pi Digital Making Curriculum](https://www.raspberrypi.org/curriculum/):

en/step_2.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## Build a Flask website
22

3-
You're going to set up a basic web application with Flask and Python.
3+
First, set up a basic web application with Flask and Python.
44

5-
If you don't already have Python 3 on your computer you will need to download and install it.
5+
If you don't have Python 3 on your computer, you need to download and install it.
66

77
--- task ---
88

@@ -12,7 +12,7 @@ If you don't already have Python 3 on your computer you will need to download an
1212

1313
--- /task ---
1414

15-
You will also need to install the Flask package.
15+
You also need to install the Flask package.
1616

1717
--- task ---
1818

@@ -26,7 +26,7 @@ Once Flask is installed, you can create your web application.
2626

2727
--- task ---
2828

29-
Open a terminal or command prompt window, and make a new directory called `webapp` for your project.
29+
Open a terminal or command prompt window, and use the `mkdir` command to create a new directory called `webapp`.
3030

3131
```bash
3232
mkdir webapp
@@ -36,7 +36,7 @@ mkdir webapp
3636

3737
--- task ---
3838

39-
Use the change directory command `cd` to open the new directory.
39+
Use the 'change directory' command `cd` to open the new directory.
4040

4141
```bash
4242
cd webapp
@@ -45,19 +45,19 @@ cd webapp
4545

4646
--- task ---
4747

48-
Open Python 3 IDLE.
48+
Open Python 3 IDLE, and create a new file by clicking on**File** and then on **New file**.
4949

5050
--- /task ---
5151

5252
--- task ---
5353

54-
Create a new file by clicking **File** and then **New file**, and save it as `app.py` inside the `webapp` folder you just created.
54+
Save the new file with the name `app.py` inside the `webapp` folder.
5555

5656
--- /task ---
5757

5858
--- task ---
5959

60-
Now enter the following lines of code into the blank `app.py` window:
60+
Now enter the following lines of code into the `app.py` file:
6161

6262
```python
6363
from flask import Flask
@@ -72,19 +72,19 @@ if __name__ == '__main__':
7272
app.run(debug=True, host='0.0.0.0')
7373
```
7474

75-
You will explore this code in more detail in the next step, but for now let's keep it simple and make sure everything works.
76-
7775
![idle](images/idle-flask.png)
7876

77+
You will explore this code in more detail in the next step. But first, make sure the code works.
78+
7979
--- /task ---
8080

8181
--- task ---
8282

83-
Save your changes by clicking **File** and then **Save**, or by pressing <kbd>Ctrl</kbd> and <kbd>S</kbd>.
83+
Save your changes by clicking on **File** and then on **Save**, or by pressing <kbd>Ctrl</kbd> and <kbd>S</kbd> at the same time.
8484

8585
--- /task ---
8686

87-
You will need to run your web app from the terminal/command prompt window you opened earlier.
87+
You need to run your web app from the terminal/command prompt window you opened earlier.
8888

8989
--- task ---
9090

@@ -98,7 +98,7 @@ Enter the command `python app.py` into the command prompt window.
9898

9999
--- /task ---
100100

101-
If everything has been set up correctly, you should see an output similar to this:
101+
If your code is correct, the window should show you output similar to this:
102102

103103
```
104104
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
@@ -111,9 +111,9 @@ If everything has been set up correctly, you should see an output similar to thi
111111

112112
--- task ---
113113

114-
Open your web browser and enter the URL `http://127.0.0.1:5000/`. You should see a white screen with the words `Hello world`.
114+
Now open your web browser and enter the URL `http://127.0.0.1:5000/`. You should see a white screen with the words `Hello world`.
115115

116-
**Note:** `127.0.0.1` means 'home', i.e. this computer. `:5000` means 'port 5000', which is the port the web server is running on.
116+
**Note:** `127.0.0.1` means 'home', i.e. your computer. `:5000` means 'port 5000', which is the port the web server is running on.
117117

118118
--- /task ---
119119

en/step_3.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Now you're going to add a new page to your web app by creating a new **route**.
44

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.
66

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:
88

99
```python
1010
@app.route('/')
@@ -15,23 +15,23 @@ def index():
1515
This route is made up of three parts:
1616

1717
- `@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
1919
- `return 'Hello world'`: this is the content of the web page, which is returned when the user goes to this URL
2020

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:
2222

2323
```python
2424
if __name__ == '__main__':
2525
app.run(debug=True, host='0.0.0.0')
2626
```
2727

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.
2929

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.
3131

3232
--- task ---
3333

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`:
3535

3636
```python
3737
@app.route('/cakes')
@@ -70,7 +70,7 @@ if __name__ == '__main__':
7070

7171
--- task ---
7272

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.
7474

7575
![Yummy Cakes](images/flask-cakes.png)
7676

en/step_4.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
## Using HTML
1+
## Return HTML web pages
22

33
Next, you'll modify your existing routes to return a full HTML web page rather than just simple text.
44

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.
66

77
--- task ---
88

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:
1010

1111
```bash
1212
mkdir templates
@@ -54,15 +54,15 @@ from flask import Flask, render_template
5454

5555
--- task ---
5656

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:
5858

5959
```python
6060
@app.route('/')
6161
def index():
6262
return render_template('index.html')
6363
```
6464

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.
6666

6767
--- /task ---
6868

@@ -78,7 +78,7 @@ Load the `http://127.0.0.1:5000/` page in your web browser to see your new HTML
7878

7979
![my website](images/flask-template.png)
8080

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!
8282

8383
--- /task ---
8484

en/step_5.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
## Challenge: Add a HTML template to the second page
1+
## Challenge: add a HTML template to the second page
22

3-
Now you've learnt how to change your 'index' page to use a HTML template, do the same for your 'cakes' page!
3+
Now you know how to change your 'index' page to use a HTML template, make your 'cakes' page use a HTML template too!
44

55
--- hints ---
66

77
--- hint ---
88

9-
Repeat the same steps you've just followed to create a template for the cakes page.
9+
Repeat the steps in the previous project section to create and use a HTML template for the 'cakes' page.
1010

1111
--- /hint ---
1212

en/step_6.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Now you'll add some Cascading Style Sheets (CSS) to add colour to your web page.
44

55
--- task ---
66

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 ..`.
88

99
--- /task ---
1010

@@ -20,7 +20,7 @@ mkdir static
2020

2121
--- task ---
2222

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.
2424

2525
--- /task ---
2626

@@ -37,7 +37,7 @@ body {
3737

3838
![idle css](images/idle-css.png)
3939

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).
4141

4242
--- /task ---
4343

@@ -66,15 +66,15 @@ Now modify your `index.html` HTML template to include the CSS rules by adding a
6666

6767
--- task ---
6868

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!
7070

7171
![Flask app with colour](images/flask-app-with-colour.png)
7272

7373
--- /task ---
7474

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.
7676

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:
7878

7979
```
8080
├── app.py

en/step_7.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
## Adding dynamic content
1+
## Add dynamic content
22

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.
44

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!'.
68

79
--- task ---
810

9-
Create a new route in your application like so:
11+
Add the following code to create a new route in your application:
1012

1113
```python
1214
@app.route('/hello/<name>')
1315
def hello(name):
1416
return render_template('page.html', name=name)
1517
```
1618

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.
2022

2123
--- /task ---
2224

@@ -36,11 +38,11 @@ Create a new HTML template called `page.html`, and add the following HTML code t
3638

3739
--- task ---
3840

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:
4042

4143
![Hello Paul!](images/flask-hello-paul.png)
4244

43-
Try it with different names!
45+
Try `http://127.0.0.1/hello/name` with different names!
4446

4547
--- /task ---
4648

@@ -50,23 +52,23 @@ Try it with different names!
5052
title: What's happening here?
5153
---
5254

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):
5456

5557
```html
5658
<h1>Hello {{ name }}!</h1>
5759
```
5860

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`.
6062

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.
6264

6365
--- /collapse ---
6466

6567
--- task ---
6668

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.
6870

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.
7072

7173
```html
7274
<h1>My website</h1>
@@ -77,7 +79,7 @@ Edit `index.html` to add a link to your new hello page under the heading.
7779

7880
--- task ---
7981

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.
8183

8284
![flask app link](images/flask-app-link.png)
8385

en/step_8.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- Add more CSS rules to each of your pages
44
- 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
88

0 commit comments

Comments
 (0)