Skip to content

Commit bb34feb

Browse files
authored
Merge pull request #19 from raspberrypilearning/draft
Remove unneccessary step 3
2 parents 3a1f5f9 + 6c9aa61 commit bb34feb

File tree

10 files changed

+196
-201
lines changed

10 files changed

+196
-201
lines changed

en/meta.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ last_tested: 2017-01-01
1919
steps:
2020
- title: Introduction
2121
- title: What you will need
22-
- title: Build a Python-powered web server with Flask
2322
- title: Installing Flask
2423
- title: Building a basic Flask web application
2524
- title: Adding a new route to your web app

en/step_10.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
## Browsing on other devices
2-
3-
Since we used `host='0.0.0.0'`, on the `app.run` line, the web server is accessible to any device on the same network, including other computers, tablets, and smartphones.
4-
5-
- Enter the following command in the Terminal window to find your Raspberry Pi's IP address:
6-
7-
```bash
8-
hostname -I
9-
```
10-
11-
You should get something like `192.168.1.3`.
12-
13-
- Take another computer, tablet or smartphone, and make sure it's connected to the same network as the Raspberry Pi.
14-
15-
- Open up a web browser on the other device and enter the Raspberry Pi's IP address into the address bar with `:5000` on the end e.g. `http://192.168.1.3:5000/`:
16-
17-
![Address bar](images/flask-on-android.png)
18-
19-
- You should now see the web app from the other device. Try navigating to the other pages too.
1+
## What next?
2+
3+
- Try adding links between pages using anchor tags like `<a href='/hello/Paul/'>Hello Paul</a>`.
4+
- Add parameters to a previous route to make other views dynamic.
5+
- Add more CSS rules to each of your routes.
6+
- Learn more about HTML, CSS and web development with [Google Coder](https://projects.raspberrypi.org/en/projects/coder-html-css-lessons/), [Mozilla Developer Network](https://developer.mozilla.org/en-US/Learn) and [Codecademy](https://www.codecademy.com/en/tracks/web).
7+
- Learn more about Flask using the [Flask documentation](http://flask.pocoo.org/docs).
8+
- 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), [BETT Bot](https://github.com/bennuttall/bett-bot) and [Energenie](http://www.pythonhosted.org/energenie/examples/web/)).
9+
- Use a Flask web app as the control panel in a home automation project: turn the lights on from your phone!
2010

en/step_11.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

en/step_3.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
## Build a Python-powered web server with Flask
1+
## Installing Flask
22

3-
Install the lightweight web framework Flask and set up a basic web server with different pages, using Python, HTML, and CSS.
3+
First you're going to install the Flask package. Make sure you are connected to the internet, either by Ethernet cable or WiFi before you start.
4+
5+
- Start by opening a Terminal window from the taskbar or applications menu:
6+
7+
![Open Terminal window](images/open-terminal.png)
8+
9+
- Now install Flask by typing:
10+
11+
```bash
12+
sudo apt-get install python3-flask
13+
```
414

en/step_4.md

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
1-
## Installing Flask
1+
## Building a basic Flask web application
22

3-
First you're going to install the Flask package. Make sure you are connected to the internet, either by Ethernet cable or WiFi before you start.
3+
Now you're going to set up a basic web application with Flask and Python. You will be able to run a single web page and display some text on a web browser.
44

5-
- Start by opening a Terminal window from the taskbar or applications menu:
5+
- Using the Terminal, make a new directory for your project.
66

7-
![Open Terminal window](images/open-terminal.png)
7+
```bash
8+
mkdir webapp
9+
```
810

9-
- Now install Flask by typing:
11+
- Use the change directory command to open it.
1012

11-
```bash
12-
sudo apt-get install python3-flask
13+
```bash
14+
cd webapp
15+
```
16+
17+
- Open Python 3 from the main menu.
18+
19+
- Open a new window by clicking `File > New file`, and save this as `app.py` inside the project folder you created.
20+
21+
- You'll write your application code here and when you run your code, any printed messages or errors will be shown in the Python shell window which opened first.
22+
23+
- Now enter the following lines into the blank `app.py` window:
24+
25+
```python
26+
from flask import Flask
27+
28+
app = Flask(__name__)
29+
30+
@app.route('/')
31+
def index():
32+
return 'Hello world'
33+
34+
if __name__ == '__main__':
35+
app.run(debug=True, host='0.0.0.0')
1336
```
37+
38+
Note here the `host='0.0.0.0'` means the web app will be accessible to any device on the network.
39+
40+
- Save the file with `Ctrl + S`.
41+
42+
- Return to the Terminal window and enter `python3 app.py` to run the web server.
43+
44+
If everything has been written correctly, you should see an output similar to this:
45+
46+
```
47+
* Running on http://0.0.0.0:5000/
48+
* Restarting with reloader
49+
```
50+
51+
- Open the Pi's web browser from the taskbar or application menu and navigate to `http://127.0.0.1:5000/`. You should see a white screen with the words `Hello world`:
52+
53+
![Flask Hello world](images/flask-hello-world.png)
54+
55+
*Note: `127.0.0.1` means 'home' i.e. this computer, and `:5000` means 'port 5000', which is the port the web server is running on*
1456

en/step_5.md

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,32 @@
1-
## Building a basic Flask web application
1+
## Adding a new route to your web app
22

3-
Now you're going to set up a basic web application with Flask and Python. You will be able to run a single web page and display some text on a web browser.
3+
Now you're going to add a new route to your web app, which will create another web page.
44

5-
- Using the Terminal, make a new directory for your project.
5+
In a web application, a route is a certain path into your website, determined by the request sent by the user when they type it into their web browser. It's up to you which routes are enabled and what each of them does.
66

7-
```bash
8-
mkdir webapp
9-
```
10-
11-
- Use the change directory command to open it.
7+
In the "Hello World" example we used a single route:
128

13-
```bash
14-
cd webapp
9+
```python
10+
@app.route('/')
11+
def index():
12+
return 'Hello world'
1513
```
1614

17-
- Open Python 3 from the main menu.
15+
This route is made up of three parts:
1816

19-
- Open a new window by clicking `File > New file`, and save this as `app.py` inside the project folder you created.
17+
- `@app.route('/')`: this determines the entry point; the `/` means the root of the website, so just `http://127.0.0.1:5000/`.
18+
- `def index()`: this is the name we give to the route. Here it was called `index` because it's the index of the website.
19+
- `return 'Hello world'`: this is the content of the web page, which is returned when the user browses the index of the website.
2020

21-
- You'll write your application code here and when you run your code, any printed messages or errors will be shown in the Python shell window which opened first.
22-
23-
- Now enter the following lines into the blank `app.py` window:
21+
- Create a new route by adding the following lines below the first route:
2422

2523
```python
26-
from flask import Flask
27-
28-
app = Flask(__name__)
29-
30-
@app.route('/')
31-
def index():
32-
return 'Hello world'
33-
34-
if __name__ == '__main__':
35-
app.run(debug=True, host='0.0.0.0')
24+
@app.route('/cakes')
25+
def cakes():
26+
return 'Yummy cakes!'
3627
```
37-
38-
Note here the `host='0.0.0.0'` means the web app will be accessible to any device on the network.
39-
40-
- Save the file with `Ctrl + S`.
41-
42-
- Return to the Terminal window and enter `python3 app.py` to run the web server.
43-
44-
If everything has been written correctly, you should see an output similar to this:
45-
46-
```
47-
* Running on http://0.0.0.0:5000/
48-
* Restarting with reloader
49-
```
50-
51-
- Open the Pi's web browser from the taskbar or application menu and navigate to `http://127.0.0.1:5000/`. You should see a white screen with the words `Hello world`:
5228

53-
![Flask Hello world](images/flask-hello-world.png)
29+
- Save your code and navigate to your website's cake page in the browser at `127.0.0.1:5000/cakes`. You should see a webpage with the text `Yummy cakes!` on it:
5430

55-
*Note: `127.0.0.1` means 'home' i.e. this computer, and `:5000` means 'port 5000', which is the port the web server is running on*
31+
![Yummy Cakes](images/flask-cakes.png)
5632

en/step_6.md

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
1-
## Adding a new route to your web app
1+
## Add HTML templates to your web app
22

3-
Now you're going to add a new route to your web app, which will create another web page.
3+
Next, you'll modify your existing routes to return full HTML templates, rather than simple text strings.
44

5-
In a web application, a route is a certain path into your website, determined by the request sent by the user when they type it into their web browser. It's up to you which routes are enabled and what each of them does.
5+
- First, create a `templates` directory in your `webapp` directory by entering this into the Terminal:
66

7-
In the "Hello World" example we used a single route:
7+
```bash
8+
mkdir templates
9+
```
10+
11+
- Open `Text Editor` under `Accessories` in the main menu:
12+
13+
![Text Editor](images/open-text-editor.png)
814

9-
```python
10-
@app.route('/')
11-
def index():
12-
return 'Hello world'
13-
```
15+
This will open up a basic text editor called Leafpad.
1416

15-
This route is made up of three parts:
17+
- Enter the following HTML code:
1618

17-
- `@app.route('/')`: this determines the entry point; the `/` means the root of the website, so just `http://127.0.0.1:5000/`.
18-
- `def index()`: this is the name we give to the route. Here it was called `index` because it's the index of the website.
19-
- `return 'Hello world'`: this is the content of the web page, which is returned when the user browses the index of the website.
19+
```html
20+
<html>
21+
<body>
22+
<h1>Hello from a template!</h1>
23+
</body>
24+
</html>
25+
```
26+
27+
- Save the file as `index.html` in the `templates` directory, which you'll find inside the `pi` and then `webapp` directories.
2028
21-
- Create a new route by adding the following lines below the first route:
29+
- Return to your `app.py` file in IDLE and modify the first line of your code to import the `render_template` function as well:
2230
2331
```python
24-
@app.route('/cakes')
25-
def cakes():
26-
return 'Yummy cakes!'
32+
from flask import Flask, render_template
2733
```
2834
29-
- Save your code and navigate to your website's cake page in the browser at `127.0.0.1:5000/cakes`. You should see a webpage with the text `Yummy cakes!` on it:
35+
- Finally, you'll need to modify your index view to return the HTML template instead of the normal text. Change the `index()` function to this:
36+
37+
```python
38+
@app.route('/')
39+
def index():
40+
return render_template('index.html')
41+
```
42+
43+
Flask will look for `index.html` in a directory called `templates`, in the same directory as the `app.py` file.
44+
45+
- Save the file. Make sure your web app is still running. If you stopped it, just run `python3 app.py` from your `webapp` directory.
46+
47+
- Reload the route in your web browser (go to the base route at `http://127.0.0.1:5000/`) to see your new HTML template being displayed.
48+
49+
![Hello from a template!](images/flask-template.png)
3050

31-
![Yummy Cakes](images/flask-cakes.png)
51+
In this case it's not much different as all you've done is added a header, but there's plenty of scope to expand!
3252

en/step_7.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
1-
## Add HTML templates to your web app
1+
## Adding colour to the web page with CSS
22

3-
Next, you'll modify your existing routes to return full HTML templates, rather than simple text strings.
3+
Cascading Style Sheets (CSS) are rules for how HTML content is displayed by the browser. Now you'll add some CSS to add colour to your web page.
44

5-
- First, create a `templates` directory in your `webapp` directory by entering this into the Terminal:
5+
- First, return to the Terminal window and navigate to the `webapp` directory. If you're in the `templates` directory, go back up one level with `cd ..`.
66

7-
```bash
8-
mkdir templates
9-
```
7+
- Create a new directory called `static`.
8+
9+
- Then open a new window with the basic text editor (Leafpad), or re-open the text editor from the menu.
10+
11+
- Save the new file as `style.css` in the new `static` directory.
1012

11-
- Open `Text Editor` under `Accessories` in the main menu:
13+
- Add the following CSS rules to the file:
1214

13-
![Text Editor](images/open-text-editor.png)
15+
```css
16+
body {
17+
background: red;
18+
color: yellow;
19+
}
20+
```
21+
22+
Note here we've used colour names: usually colours are defined by hex codes like `#ff0000` (red) but this is a simple example.
1423

15-
This will open up a basic text editor called Leafpad.
24+
- Save the file.
1625

17-
- Enter the following HTML code:
26+
- Now modify your HTML template called `index.html` to include the CSS file, by adding a `<head>` tag containing a `<link>` tag with a reference to the stylesheet:
1827

1928
```html
2029
<html>
30+
<head>
31+
<link rel="stylesheet" href='/static/style.css' />
32+
</head>
2133
<body>
2234
<h1>Hello from a template!</h1>
2335
</body>
2436
</html>
2537
```
2638

27-
- Save the file as `index.html` in the `templates` directory, which you'll find inside the `pi` and then `webapp` directories.
28-
29-
- Return to your `app.py` file in IDLE and modify the first line of your code to import the `render_template` function as well:
30-
31-
```python
32-
from flask import Flask, render_template
33-
```
34-
35-
- Finally, you'll need to modify your index view to return the HTML template instead of the normal text. Change the `index()` function to this:
36-
37-
```python
38-
@app.route('/')
39-
def index():
40-
return render_template('index.html')
41-
```
42-
43-
Flask will look for `index.html` in a directory called `templates`, in the same directory as the `app.py` file.
39+
- Save the HTML file and reload the web server. You should see a colourful version of the web app!
4440

45-
- Save the file. Make sure your web app is still running. If you stopped it, just run `python3 app.py` from your `webapp` directory.
41+
![Flask app with colour](images/flask-app-with-colour.png)
4642

47-
- Reload the route in your web browser (go to the base route at `http://127.0.0.1:5000/`) to see your new HTML template being displayed.
43+
You have so far created a number of files and directories. It is worth just double-checking your `webapp` project directory, which should contain the following and have a structure like this now:
4844

49-
![Hello from a template!](images/flask-template.png)
45+
```
46+
├── app.py
47+
├── static
48+
│   └── style.css
49+
└── templates
50+
└── index.html
51+
```
5052

51-
In this case it's not much different as all you've done is added a header, but there's plenty of scope to expand!
53+
If your web app doesn't look right, check you saved your CSS file in the right place.
5254

0 commit comments

Comments
 (0)