WEBVTT

0
00:00.390 --> 00:05.390
As we saw previously when we rendered the HTML page of our CV,

1
00:06.360 --> 00:11.360
one of the components is an image tag and it has a image file that it needs to

2
00:11.910 --> 00:16.080
render. But it looks pretty broken when we rendered HTML.

3
00:17.040 --> 00:19.830
So if we go ahead and open up the Chrome developer tool

4
00:20.310 --> 00:24.180
which is under view, developer, and developer tools,

5
00:24.630 --> 00:26.190
then if we go to console

6
00:26.280 --> 00:30.690
you'll see that we actually get a bunch of 404 errors and they relate

7
00:30.690 --> 00:35.130
to the styles.css file as well as the angela.png file.

8
00:35.610 --> 00:37.710
That is what this is referring to.

9
00:39.570 --> 00:40.920
And you can actually see

10
00:40.920 --> 00:45.920
we get the same 404 errors when we look at the debug log in here as

11
00:46.200 --> 00:50.520
well. So we're missing some stuff basically. Well, firstly,

12
00:50.550 --> 00:51.990
let's see if we can recover it.

13
00:52.530 --> 00:56.460
We can go ahead and save this image from the website

14
00:56.790 --> 00:59.550
which I'll just call angela.png.

15
00:59.940 --> 01:03.960
And then we can drag this image into our personal site.

16
01:06.870 --> 01:09.930
And there it is right there. Now, in my case,

17
01:09.960 --> 01:12.600
my image actually happens to be a circular one

18
01:12.960 --> 01:15.210
which I edited in Photoshop,

19
01:15.270 --> 01:18.180
but you might just have a square one and it doesn't matter at all.

20
01:19.050 --> 01:22.380
Now that we've got our image inside our project,

21
01:22.650 --> 01:27.650
how do we make sure that that image actually gets rendered in our HTML site?

22
01:30.330 --> 01:35.330
Because here is the image tag and it's looking inside Angela's personal site

23
01:36.570 --> 01:41.460
files for angela.png, which is not what we have here.

24
01:42.030 --> 01:46.470
In fact, if we had moved this PNG file to the same

25
01:46.470 --> 01:51.120
folder so they're on the same hierarchy, basically inside the same folder,

26
01:51.630 --> 01:56.280
well then we could probably just change this to angela.png.

27
01:56.700 --> 02:01.700
If I go ahead and restart and rerun my server after I've added in all these

28
02:02.010 --> 02:06.150
files and I go to my personal site and I hit reload,

29
02:06.480 --> 02:10.980
you'll see that there's still no photo. So why is that? Well,

30
02:11.040 --> 02:13.050
back to the Flask documentation we go.

31
02:13.710 --> 02:17.940
The reason is because, just like our templates have to be inside a folder called

32
02:17.970 --> 02:21.840
templates, Flask will look for all your static files

33
02:21.840 --> 02:26.840
like your images or your CSS files inside a folder called static.

34
02:27.780 --> 02:31.410
So you have to put it inside a folder called static.

35
02:32.280 --> 02:36.450
They made the framework, they make the rules. So inside our project

36
02:36.480 --> 02:41.480
my-personal-site, let's create a new directory called static and let's move our

37
02:42.030 --> 02:44.610
image into that static folder.

38
02:46.170 --> 02:51.120
Now inside our angela.html, when we want to point to that image,

39
02:51.630 --> 02:56.630
then the path is going to be static/ the name of the file inside

40
02:57.870 --> 03:02.170
that folder, which in my case is angela.png. Now,

41
03:02.170 --> 03:05.440
if I go ahead and rerun my application,

42
03:05.800 --> 03:09.100
then go back to my homepage and refresh,

43
03:09.370 --> 03:12.160
you should see that image being rendered.

44
03:13.600 --> 03:18.010
It was able to look inside the static folder and find that image.

45
03:19.900 --> 03:23.470
Whenever you create a Flask application, 9 out of 10 times

46
03:23.500 --> 03:27.100
you'll need to create it with the templates folder and the static folder.

47
03:27.490 --> 03:31.570
And you put all your static files like your CSS files or your images or your

48
03:31.570 --> 03:33.700
videos inside that folder

49
03:33.970 --> 03:37.030
and your file path is relative to that folder.

50
03:37.750 --> 03:40.000
And then when you create your HTML templates,

51
03:40.030 --> 03:42.400
you're going to put it inside the templates folder.

52
03:43.420 --> 03:45.640
Now going back to my project,

53
03:46.000 --> 03:49.960
I'm going to delete all the extra files that we created just now in the demo.

54
03:50.550 --> 03:51.383
<v 1>Right.</v>

55
03:55.770 --> 03:59.880
<v 0>And I should now only end up with my index.html</v>

56
04:00.120 --> 04:03.270
which is our simple website. Now,

57
04:03.300 --> 04:07.170
the reason why I wanna keep it simple is just so that you really understand

58
04:07.170 --> 04:08.850
everything from the ground up.

59
04:09.450 --> 04:14.450
And the challenge I've got for you is to go ahead and create a CSS style sheet,

60
04:16.530 --> 04:18.510
and then using the style sheet

61
04:18.840 --> 04:23.840
change the background of the body of this index.html website to purple.

62
04:26.190 --> 04:29.850
That's the goal. You are going to have to change the server.py,

63
04:30.330 --> 04:33.150
you're going to have to change the index.html,

64
04:33.540 --> 04:35.850
and you're going to have to create a CSS file.

65
04:36.480 --> 04:39.570
Pause the video and see if you can complete this challenge.

66
04:40.020 --> 04:40.853
<v 1>Right.</v>

67
04:43.680 --> 04:47.820
<v 0>All right. The first thing we're going to do is create a CSS file.</v>

68
04:48.330 --> 04:50.880
That is going to be a static file.

69
04:51.390 --> 04:53.670
We're going to create it inside the static folder.

70
04:54.360 --> 04:57.750
So I'm going to call it styles.css

71
04:59.580 --> 05:04.580
and now I'm going to go ahead and target the body of my website to the body tag,

72
05:07.680 --> 05:12.120
and then I'm going to change the background color to purple

73
05:13.770 --> 05:14.490
like this.

74
05:14.490 --> 05:19.490
So very simple CSS code here. Now that when it's run is going to target this body

75
05:22.140 --> 05:24.720
tag which is basically the body of our website

76
05:24.810 --> 05:28.560
and hopefully it's going to turn the background purple. Now,

77
05:28.620 --> 05:30.630
in order for that CSS to work though,

78
05:30.690 --> 05:35.340
we're going to have to add it into the head of our HTML file just as we've done

79
05:35.340 --> 05:39.660
it thousands of times before when we did the lessons on CSS and HTML.

80
05:40.620 --> 05:43.710
In order to add that in, we're going to create a link element

81
05:44.160 --> 05:47.670
which is going to have a relationship. Now,

82
05:47.700 --> 05:50.820
the relationship is going to be a style sheet.

83
05:50.970 --> 05:55.530
So it's basically telling the HTML file that this link points to a style sheet.

84
05:55.980 --> 06:00.980
And then the href of the style sheet is going to be inside static/

85
06:01.370 --> 06:03.170
styles.css.

86
06:04.190 --> 06:09.170
Now let's go ahead and save both of these files and go back to our server and

87
06:09.170 --> 06:14.120
make sure that we're rendering that HTML template which is index.html.

88
06:14.930 --> 06:19.930
And now if we go ahead and stop and restart this project so that it takes into

89
06:21.080 --> 06:25.190
account all the new files, and then we go to our homepage,

90
06:25.430 --> 06:29.630
hopefully it should show you a page that has a purple background

91
06:29.990 --> 06:32.420
like you see here. Now,

92
06:32.420 --> 06:37.280
one of the things you have to be careful about Chrome is that it likes to cache

93
06:37.580 --> 06:40.190
your static files. So what does this mean? Well,

94
06:40.640 --> 06:44.420
let's go back to the styles.css and I'm going to change that background-color

95
06:44.420 --> 06:47.870
to red. Now I'm going to hit save on this file.

96
06:48.140 --> 06:51.290
I'm even going to stop and rerun my file,

97
06:51.620 --> 06:55.130
but you can see if I refresh this page, it's still purple.

98
06:55.760 --> 07:00.760
And if I go into the Chrome developer tools and we take a look at our elements,

99
07:02.510 --> 07:07.310
you can see that the body still has CSS saying that the background color should

100
07:07.310 --> 07:11.660
be purple. If we go to the sources inside the static folder,

101
07:11.720 --> 07:14.290
there's our styles.css. And you can see

102
07:14.300 --> 07:18.500
this is an older version of our style sheet. So what's going on?

103
07:18.590 --> 07:22.970
Is it because we didn't save this change? No, we definitely saved it.

104
07:23.570 --> 07:28.400
What's actually happening is that to save your internet usage,

105
07:28.700 --> 07:33.700
Chrome likes to cache the style sheets and other static files,

106
07:34.310 --> 07:35.960
so images or JavaScript.

107
07:36.530 --> 07:39.770
and the reason why it does that is because when you visit a website,

108
07:40.130 --> 07:41.990
if you're visiting it on the same day,

109
07:42.290 --> 07:45.200
these static files are unlikely to change.

110
07:45.530 --> 07:50.530
A website is unlikely to update their CSS or their images over one day.

111
07:51.470 --> 07:54.380
So once the browser downloads it,

112
07:54.620 --> 07:56.990
it keeps hold of those static files.

113
07:57.350 --> 08:01.910
This just means that you don't have to keep downloading these large files every

114
08:01.910 --> 08:04.340
single time you hit up the same website.

115
08:05.090 --> 08:10.090
That's kind of good if we are in fact visiting a real live website.

116
08:11.120 --> 08:14.330
But when we're testing, it's a little bit confusing.

117
08:14.900 --> 08:19.610
So one of the things you have to do is you have to get Chrome to hard reload.

118
08:20.330 --> 08:25.330
And you can do that by holding down the shift key and clicking on this refresh

119
08:25.850 --> 08:29.450
button, which is going to do a hard reload.

120
08:29.660 --> 08:34.220
And it's going to get rid of all the cached files and pull in any new files.

121
08:34.970 --> 08:39.970
So now the background changes to red. And we can confirm that by... let's change

122
08:40.310 --> 08:44.930
this to blue hit save on that file, holding down shift, reload,

123
08:45.050 --> 08:47.450
and we get the blue background updating.

124
08:48.590 --> 08:52.640
So that's just something to be careful about when you find that your CSS files

125
08:52.640 --> 08:56.300
or your other static files are not being changed or not being rendered.