1
00:00:00,390 --> 00:00:05,390
As we saw previously when we rendered the HTML page of our CV,

2
00:00:06,360 --> 00:00:11,360
one of the components is an image tag and it has a image file that it needs to

3
00:00:11,910 --> 00:00:16,079
render. But it looks pretty broken when we rendered HTML.

4
00:00:17,040 --> 00:00:19,830
So if we go ahead and open up the Chrome developer tool

5
00:00:20,310 --> 00:00:24,180
which is under view, developer, and developer tools,

6
00:00:24,630 --> 00:00:26,190
then if we go to console

7
00:00:26,280 --> 00:00:30,690
you'll see that we actually get a bunch of 404 errors and they relate

8
00:00:30,690 --> 00:00:35,130
to the styles.css file as well as the angela.png file.

9
00:00:35,610 --> 00:00:37,710
That is what this is referring to.

10
00:00:39,570 --> 00:00:40,920
And you can actually see

11
00:00:40,920 --> 00:00:45,920
we get the same 404 errors when we look at the debug log in here as

12
00:00:46,200 --> 00:00:50,520
well. So we're missing some stuff basically. Well, firstly,

13
00:00:50,550 --> 00:00:51,990
let's see if we can recover it.

14
00:00:52,530 --> 00:00:56,460
We can go ahead and save this image from the website

15
00:00:56,790 --> 00:00:59,550
which I'll just call angela.png.

16
00:00:59,940 --> 00:01:03,960
And then we can drag this image into our personal site.

17
00:01:06,870 --> 00:01:09,930
And there it is right there. Now, in my case,

18
00:01:09,960 --> 00:01:12,600
my image actually happens to be a circular one

19
00:01:12,960 --> 00:01:15,210
which I edited in Photoshop,

20
00:01:15,270 --> 00:01:18,180
but you might just have a square one and it doesn't matter at all.

21
00:01:19,050 --> 00:01:22,380
Now that we've got our image inside our project,

22
00:01:22,650 --> 00:01:27,650
how do we make sure that that image actually gets rendered in our HTML site?

23
00:01:30,330 --> 00:01:35,330
Because here is the image tag and it's looking inside Angela's personal site

24
00:01:36,570 --> 00:01:41,460
files for angela.png, which is not what we have here.

25
00:01:42,030 --> 00:01:46,470
In fact, if we had moved this PNG file to the same

26
00:01:46,470 --> 00:01:51,120
folder so they're on the same hierarchy, basically inside the same folder,

27
00:01:51,630 --> 00:01:56,280
well then we could probably just change this to angela.png.

28
00:01:56,700 --> 00:02:01,700
If I go ahead and restart and rerun my server after I've added in all these

29
00:02:02,010 --> 00:02:06,150
files and I go to my personal site and I hit reload,

30
00:02:06,480 --> 00:02:10,979
you'll see that there's still no photo. So why is that? Well,

31
00:02:11,039 --> 00:02:13,050
back to the Flask documentation we go.

32
00:02:13,710 --> 00:02:17,940
The reason is because, just like our templates have to be inside a folder called

33
00:02:17,970 --> 00:02:21,840
templates, Flask will look for all your static files

34
00:02:21,840 --> 00:02:26,840
like your images or your CSS files inside a folder called static.

35
00:02:27,780 --> 00:02:31,410
So you have to put it inside a folder called static.

36
00:02:32,280 --> 00:02:36,450
They made the framework, they make the rules. So inside our project

37
00:02:36,480 --> 00:02:41,480
my-personal-site, let's create a new directory called static and let's move our

38
00:02:42,030 --> 00:02:44,610
image into that static folder.

39
00:02:46,170 --> 00:02:51,120
Now inside our angela.html, when we want to point to that image,

40
00:02:51,630 --> 00:02:56,630
then the path is going to be static/ the name of the file inside

41
00:02:57,870 --> 00:03:02,170
that folder, which in my case is angela.png. Now,

42
00:03:02,170 --> 00:03:05,440
if I go ahead and rerun my application,

43
00:03:05,800 --> 00:03:09,100
then go back to my homepage and refresh,

44
00:03:09,370 --> 00:03:12,160
you should see that image being rendered.

45
00:03:13,600 --> 00:03:18,010
It was able to look inside the static folder and find that image.

46
00:03:19,900 --> 00:03:23,470
Whenever you create a Flask application, 9 out of 10 times

47
00:03:23,500 --> 00:03:27,100
you'll need to create it with the templates folder and the static folder.

48
00:03:27,490 --> 00:03:31,570
And you put all your static files like your CSS files or your images or your

49
00:03:31,570 --> 00:03:33,700
videos inside that folder

50
00:03:33,970 --> 00:03:37,030
and your file path is relative to that folder.

51
00:03:37,750 --> 00:03:40,000
And then when you create your HTML templates,

52
00:03:40,030 --> 00:03:42,400
you're going to put it inside the templates folder.

53
00:03:43,420 --> 00:03:45,640
Now going back to my project,

54
00:03:46,000 --> 00:03:49,960
I'm going to delete all the extra files that we created just now in the demo.

55
00:03:50,550 --> 00:03:51,383
Right.

56
00:03:55,770 --> 00:03:59,880
And I should now only end up with my index.html

57
00:04:00,120 --> 00:04:03,270
which is our simple website. Now,

58
00:04:03,300 --> 00:04:07,170
the reason why I wanna keep it simple is just so that you really understand

59
00:04:07,170 --> 00:04:08,850
everything from the ground up.

60
00:04:09,450 --> 00:04:14,450
And the challenge I've got for you is to go ahead and create a CSS style sheet,

61
00:04:16,529 --> 00:04:18,510
and then using the style sheet

62
00:04:18,839 --> 00:04:23,840
change the background of the body of this index.html website to purple.

63
00:04:26,190 --> 00:04:29,850
That's the goal. You are going to have to change the server.py,

64
00:04:30,330 --> 00:04:33,150
you're going to have to change the index.html,

65
00:04:33,540 --> 00:04:35,850
and you're going to have to create a CSS file.

66
00:04:36,480 --> 00:04:39,570
Pause the video and see if you can complete this challenge.

67
00:04:40,020 --> 00:04:40,853
Right.

68
00:04:43,680 --> 00:04:47,820
All right. The first thing we're going to do is create a CSS file.

69
00:04:48,330 --> 00:04:50,880
That is going to be a static file.

70
00:04:51,390 --> 00:04:53,670
We're going to create it inside the static folder.

71
00:04:54,360 --> 00:04:57,750
So I'm going to call it styles.css

72
00:04:59,580 --> 00:05:04,580
and now I'm going to go ahead and target the body of my website to the body tag,

73
00:05:07,680 --> 00:05:12,120
and then I'm going to change the background color to purple

74
00:05:13,770 --> 00:05:14,490
like this.

75
00:05:14,490 --> 00:05:19,490
So very simple CSS code here. Now that when it's run is going to target this body

76
00:05:22,140 --> 00:05:24,720
tag which is basically the body of our website

77
00:05:24,810 --> 00:05:28,560
and hopefully it's going to turn the background purple. Now,

78
00:05:28,620 --> 00:05:30,630
in order for that CSS to work though,

79
00:05:30,690 --> 00:05:35,340
we're going to have to add it into the head of our HTML file just as we've done

80
00:05:35,340 --> 00:05:39,660
it thousands of times before when we did the lessons on CSS and HTML.

81
00:05:40,620 --> 00:05:43,710
In order to add that in, we're going to create a link element

82
00:05:44,160 --> 00:05:47,670
which is going to have a relationship. Now,

83
00:05:47,700 --> 00:05:50,820
the relationship is going to be a style sheet.

84
00:05:50,970 --> 00:05:55,530
So it's basically telling the HTML file that this link points to a style sheet.

85
00:05:55,980 --> 00:06:00,980
And then the href of the style sheet is going to be inside static/

86
00:06:01,370 --> 00:06:03,170
styles.css.

87
00:06:04,190 --> 00:06:09,170
Now let's go ahead and save both of these files and go back to our server and

88
00:06:09,170 --> 00:06:14,120
make sure that we're rendering that HTML template which is index.html.

89
00:06:14,930 --> 00:06:19,930
And now if we go ahead and stop and restart this project so that it takes into

90
00:06:21,080 --> 00:06:25,190
account all the new files, and then we go to our homepage,

91
00:06:25,430 --> 00:06:29,630
hopefully it should show you a page that has a purple background

92
00:06:29,990 --> 00:06:32,420
like you see here. Now,

93
00:06:32,420 --> 00:06:37,280
one of the things you have to be careful about Chrome is that it likes to cache

94
00:06:37,580 --> 00:06:40,190
your static files. So what does this mean? Well,

95
00:06:40,640 --> 00:06:44,420
let's go back to the styles.css and I'm going to change that background-color

96
00:06:44,420 --> 00:06:47,870
to red. Now I'm going to hit save on this file.

97
00:06:48,140 --> 00:06:51,290
I'm even going to stop and rerun my file,

98
00:06:51,620 --> 00:06:55,130
but you can see if I refresh this page, it's still purple.

99
00:06:55,760 --> 00:07:00,760
And if I go into the Chrome developer tools and we take a look at our elements,

100
00:07:02,510 --> 00:07:07,310
you can see that the body still has CSS saying that the background color should

101
00:07:07,310 --> 00:07:11,660
be purple. If we go to the sources inside the static folder,

102
00:07:11,720 --> 00:07:14,290
there's our styles.css. And you can see

103
00:07:14,300 --> 00:07:18,500
this is an older version of our style sheet. So what's going on?

104
00:07:18,590 --> 00:07:22,970
Is it because we didn't save this change? No, we definitely saved it.

105
00:07:23,570 --> 00:07:28,400
What's actually happening is that to save your internet usage,

106
00:07:28,700 --> 00:07:33,700
Chrome likes to cache the style sheets and other static files,

107
00:07:34,310 --> 00:07:35,960
so images or JavaScript.

108
00:07:36,530 --> 00:07:39,770
and the reason why it does that is because when you visit a website,

109
00:07:40,130 --> 00:07:41,990
if you're visiting it on the same day,

110
00:07:42,290 --> 00:07:45,200
these static files are unlikely to change.

111
00:07:45,530 --> 00:07:50,530
A website is unlikely to update their CSS or their images over one day.

112
00:07:51,470 --> 00:07:54,380
So once the browser downloads it,

113
00:07:54,620 --> 00:07:56,990
it keeps hold of those static files.

114
00:07:57,350 --> 00:08:01,910
This just means that you don't have to keep downloading these large files every

115
00:08:01,910 --> 00:08:04,340
single time you hit up the same website.

116
00:08:05,090 --> 00:08:10,090
That's kind of good if we are in fact visiting a real live website.

117
00:08:11,120 --> 00:08:14,330
But when we're testing, it's a little bit confusing.

118
00:08:14,900 --> 00:08:19,610
So one of the things you have to do is you have to get Chrome to hard reload.

119
00:08:20,330 --> 00:08:25,330
And you can do that by holding down the shift key and clicking on this refresh

120
00:08:25,850 --> 00:08:29,450
button, which is going to do a hard reload.

121
00:08:29,660 --> 00:08:34,220
And it's going to get rid of all the cached files and pull in any new files.

122
00:08:34,970 --> 00:08:39,970
So now the background changes to red. And we can confirm that by... let's change

123
00:08:40,309 --> 00:08:44,930
this to blue hit save on that file, holding down shift, reload,

124
00:08:45,050 --> 00:08:47,450
and we get the blue background updating.

125
00:08:48,590 --> 00:08:52,640
So that's just something to be careful about when you find that your CSS files

126
00:08:52,640 --> 00:08:56,300
or your other static files are not being changed or not being rendered.

