|
1 | | -## Add HTML templates to your web app |
| 1 | +## Adding colour to the web page with CSS |
2 | 2 |
|
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. |
4 | 4 |
|
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 ..`. |
6 | 6 |
|
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. |
10 | 12 |
|
11 | | -- Open `Text Editor` under `Accessories` in the main menu: |
| 13 | +- Add the following CSS rules to the file: |
12 | 14 |
|
13 | | -  |
| 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. |
14 | 23 |
|
15 | | - This will open up a basic text editor called Leafpad. |
| 24 | +- Save the file. |
16 | 25 |
|
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: |
18 | 27 |
|
19 | 28 | ```html |
20 | 29 | <html> |
| 30 | + <head> |
| 31 | + <link rel="stylesheet" href='/static/style.css' /> |
| 32 | + </head> |
21 | 33 | <body> |
22 | 34 | <h1>Hello from a template!</h1> |
23 | 35 | </body> |
24 | 36 | </html> |
25 | 37 | ``` |
26 | 38 |
|
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! |
44 | 40 |
|
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 | +  |
46 | 42 |
|
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: |
48 | 44 |
|
49 | | -  |
| 45 | +``` |
| 46 | +├── app.py |
| 47 | +├── static |
| 48 | +│ └── style.css |
| 49 | +└── templates |
| 50 | + └── index.html |
| 51 | +``` |
50 | 52 |
|
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. |
52 | 54 |
|
0 commit comments