1
1

00:00:00,000  -->  00:00:03,250
<v Jose>In this video we're going to code a Blueprint.</v>
2

2

00:00:03,250  -->  00:00:05,050
The first thing to do is to go ahead
3

3

00:00:05,050  -->  00:00:07,640
and create your app.py.
4

4

00:00:07,640  -->  00:00:11,160
And I've also created a new file called learning.py.
5

5

00:00:11,160  -->  00:00:14,000
learning.py will contain our blueprint
6

6

00:00:14,000  -->  00:00:16,883
and app.py will contain our app definition.
7

7

00:00:18,020  -->  00:00:19,760
In app.py we are going to say
8

8

00:00:19,760  -->  00:00:21,893
from flask import Flask,
9

9

00:00:22,900  -->  00:00:24,853
and then we're going to define our app.
10

10

00:00:26,390  -->  00:00:27,910
As usual we're gonna say
11

11

00:00:27,910  -->  00:00:30,800
app is Flask with dunder name,
12

12

00:00:30,800  -->  00:00:33,460
and this is going to define our flask app.
13

13

00:00:33,460  -->  00:00:36,630
Then, whatever we do, we have to define
14

14

00:00:36,630  -->  00:00:39,430
a route for our flask app.
15

15

00:00:39,430  -->  00:00:40,650
So the way we do that is we say
16

16

00:00:40,650  -->  00:00:44,523
app.route and then we define the endpoint,
17

17

00:00:45,510  -->  00:00:47,560
and in this case we're going to leave it as that
18

18

00:00:47,560  -->  00:00:50,970
and then we're gonna say what function
19

19

00:00:50,970  -->  00:00:55,300
is going to run when we access this endpoint
20

20

00:00:55,300  -->  00:00:56,410
and then we're going to go ahead
21

21

00:00:56,410  -->  00:00:58,743
and say something like Hello, world!
22

22

00:00:59,620  -->  00:01:02,520
So obviously a very simple endpoint here.
23

23

00:01:02,520  -->  00:01:03,770
Not much happening,
24

24

00:01:03,770  -->  00:01:06,610
this is just something that we've seen before
25

25

00:01:06,610  -->  00:01:07,810
and then at the end we're gonna say
26

26

00:01:07,810  -->  00:01:09,300
if name equal
27

27

00:01:10,360  -->  00:01:13,303
main, we're gonna say app.run.
28

28

00:01:15,220  -->  00:01:19,110
Alright, so this is the most basic flask app,
29

29

00:01:19,110  -->  00:01:20,950
something we've looked at before.
30

30

00:01:20,950  -->  00:01:22,940
The thing about blueprints is that
31

31

00:01:22,940  -->  00:01:25,160
when your app starts to grow,
32

32

00:01:25,160  -->  00:01:28,280
you have more and more of these endpoints,
33

33

00:01:28,280  -->  00:01:31,147
you don't want them all inside app.py.
34

34

00:01:31,147  -->  00:01:32,450
And the reason for that is
35

35

00:01:32,450  -->  00:01:34,180
that your app.py can get very long,
36

36

00:01:34,180  -->  00:01:35,600
it can get difficult to work with,
37

37

00:01:35,600  -->  00:01:37,240
it can be difficult to find
38

38

00:01:37,240  -->  00:01:38,680
whenever you wanna change something,
39

39

00:01:38,680  -->  00:01:40,600
where that something is.
40

40

00:01:40,600  -->  00:01:43,230
So, a good thing to do in flask apps
41

41

00:01:43,230  -->  00:01:45,380
is to split out of the endpoint,
42

42

00:01:45,380  -->  00:01:47,630
into related endpoints.
43

43

00:01:47,630  -->  00:01:49,140
So, whenever you have a bunch of endpoints
44

44

00:01:49,140  -->  00:01:51,350
that are related to the same thing,
45

45

00:01:51,350  -->  00:01:54,820
you can split them out and put them into a blueprint.
46

46

00:01:54,820  -->  00:01:55,810
So that's what we're gonna do.
47

47

00:01:55,810  -->  00:01:58,150
We're gonna take this endpoint here,
48

48

00:01:58,150  -->  00:02:01,160
and we're going to put it into our learning blueprint,
49

49

00:02:01,160  -->  00:02:03,950
just to show you how blueprints work.
50

50

00:02:03,950  -->  00:02:05,440
So, what I'm gonna do is
51

51

00:02:05,440  -->  00:02:08,200
take this endpoint here, I'm gonna cut it.
52

52

00:02:08,200  -->  00:02:09,800
I'm gonna remove it there,
53

53

00:02:09,800  -->  00:02:12,123
and then I'm gonna go over to learning.py,
54

54

00:02:13,140  -->  00:02:17,030
and I'm going to say from flask import Blueprint.
55

55

00:02:17,030  -->  00:02:19,340
And now we can define our blueprint
56

56

00:02:21,360  -->  00:02:22,890
and give it a name.
57

57

00:02:22,890  -->  00:02:27,880
This name is not going to be terribly useful right now,
58

58

00:02:27,880  -->  00:02:31,630
but we will be using this name later on in the course.
59

59

00:02:31,630  -->  00:02:34,398
And so this name here is how you will be accessing
60

60

00:02:34,398  -->  00:02:36,920
the blueprint's endpoints later on,
61

61

00:02:36,920  -->  00:02:39,170
if you wanna do that dynamically.
62

62

00:02:39,170  -->  00:02:40,170
We will be doing that.
63

63

00:02:40,170  -->  00:02:42,710
Then, we're gonna paste our endpoint here.
64

64

00:02:42,710  -->  00:02:46,210
But now notice how app is not defined.
65

65

00:02:46,210  -->  00:02:48,560
It says unresolved reference app.
66

66

00:02:48,560  -->  00:02:51,870
Of course, we don't have access to the app here.
67

67

00:02:51,870  -->  00:02:54,560
So what we're gonna do is instead of doing app.route,
68

68

00:02:54,560  -->  00:02:57,960
we're gonna say learning_blueprint.route.
69

69

00:02:57,960  -->  00:03:00,830
Now, this route here
70

70

00:03:00,830  -->  00:03:04,500
is defined only inside the blueprint.
71

71

00:03:04,500  -->  00:03:06,100
It is not defined in the app,
72

72

00:03:06,100  -->  00:03:09,550
and the app does not have access to that endpoint.
73

73

00:03:09,550  -->  00:03:11,800
In order to give the app access to that endpoint,
74

74

00:03:11,800  -->  00:03:13,410
we have to import
75

75

00:03:15,100  -->  00:03:16,530
the learning blueprint,
76

76

00:03:16,530  -->  00:03:20,253
and we also have to register the blueprint.
77

77

00:03:23,470  -->  00:03:26,280
So, when we register the blueprint like this,
78

78

00:03:26,280  -->  00:03:29,030
that means that this endpoint here
79

79

00:03:29,030  -->  00:03:34,030
is almost as if it were defined as we had before, app.route.
80

80

00:03:34,380  -->  00:03:37,140
Because we've registered the blueprint onto the app,
81

81

00:03:37,140  -->  00:03:39,700
the app has access to that endpoint,
82

82

00:03:39,700  -->  00:03:42,800
and if we were to run our app and access that endpoint,
83

83

00:03:42,800  -->  00:03:45,080
it would indeed return a hello world.
84

84

00:03:45,080  -->  00:03:46,310
So, we can do that just now.
85

85

00:03:46,310  -->  00:03:50,350
We're gonna go app.py, right click it, and run it.
86

86

00:03:50,350  -->  00:03:52,603
And there you have that.
87

87

00:03:54,160  -->  00:03:55,930
So, now I've brought that window
88

88

00:03:55,930  -->  00:03:56,930
over here to the right,
89

89

00:03:56,930  -->  00:03:59,910
so you can see what happens when you access the endpoint.
90

90

00:03:59,910  -->  00:04:02,440
Notice that we're not accessing any particular endpoint,
91

91

00:04:02,440  -->  00:04:04,990
this is just the home, or the base endpoint,
92

92

00:04:04,990  -->  00:04:08,390
because our blueprint has that defined there.
93

93

00:04:08,390  -->  00:04:11,580
If we wanted to do something like string name, for example,
94

94

00:04:11,580  -->  00:04:15,230
to be able to pass a parameter onto the endpoint,
95

95

00:04:15,230  -->  00:04:18,680
we could do that, and then we can make it return
96

96

00:04:18,680  -->  00:04:19,530
something different.
97

97

00:04:19,530  -->  00:04:23,790
So here, we're gonna refresh the app, and then...
98

98

00:04:23,790  -->  00:04:25,720
Now if we access this, it says not found,
99

99

00:04:25,720  -->  00:04:27,750
of course, and if we say
100

100

00:04:27,750  -->  00:04:30,280
something like Rolf, now we get Hello, Rolf!
101

101

00:04:30,280  -->  00:04:32,280
By the way, new thing here that we've not looked at
102

102

00:04:32,280  -->  00:04:34,690
previously in this course is this f string.
103

103

00:04:34,690  -->  00:04:36,990
You put an f in front of the quotation marks
104

104

00:04:36,990  -->  00:04:38,460
that make up a string, and that means
105

105

00:04:38,460  -->  00:04:40,610
we're creating a formatted string.
106

106

00:04:40,610  -->  00:04:42,500
So then we can use curly braces,
107

107

00:04:42,500  -->  00:04:43,740
and then inside the curly braces,
108

108

00:04:43,740  -->  00:04:45,710
put a variable name.
109

109

00:04:45,710  -->  00:04:48,370
And that's going to replace the variable value
110

110

00:04:48,370  -->  00:04:50,690
into the string, on runtime.
111

111

00:04:50,690  -->  00:04:53,510
So this is going to do hello Rolf.
112

112

00:04:53,510  -->  00:04:56,320
Okay, so now that we have this here,
113

113

00:04:56,320  -->  00:04:57,470
what do we do?
114

114

00:04:57,470  -->  00:05:00,000
Well, we can go over to our app,
115

115

00:05:00,000  -->  00:05:03,450
and notice that as soon as we do slash Rolf,
116

116

00:05:03,450  -->  00:05:05,810
we see hello Rolf.
117

117

00:05:05,810  -->  00:05:07,060
But there are some cases
118

118

00:05:07,060  -->  00:05:11,150
where we want the endpoints related to a specific blueprint
119

119

00:05:11,150  -->  00:05:13,450
to be on their own name space.
120

120

00:05:13,450  -->  00:05:17,100
So, basically, to have their own URL prefix.
121

121

00:05:17,100  -->  00:05:18,093
For example,
122

122

00:05:19,430  -->  00:05:22,793
we could do something like greetings/Rolf.
123

123

00:05:23,730  -->  00:05:24,760
If we do this right now,
124

124

00:05:24,760  -->  00:05:26,270
of course we get not found.
125

125

00:05:26,270  -->  00:05:30,090
But, we can fix that by doing url_prefix=greetings,
126

126

00:05:32,370  -->  00:05:33,780
like that.
127

127

00:05:33,780  -->  00:05:35,575
If we then restart our app,
128

128

00:05:35,575  -->  00:05:37,720
you'll see that this now works.
129

129

00:05:37,720  -->  00:05:39,880
So what we've done is we have said
130

130

00:05:39,880  -->  00:05:43,220
for all the endpoints in this blueprint,
131

131

00:05:43,220  -->  00:05:48,220
just prepend /greetings to the route.
132

132

00:05:48,700  -->  00:05:52,280
So now it would be greetings/string:name.
133

133

00:05:52,280  -->  00:05:53,510
That happens automatically,
134

134

00:05:53,510  -->  00:05:56,920
so we do not have to add the slash greetings in here,
135

135

00:05:56,920  -->  00:05:59,440
and this is particularly useful when you want again
136

136

00:05:59,440  -->  00:06:04,240
all your blueprints to have the same paths at the start.
137

137

00:06:04,240  -->  00:06:06,300
Of course, if we go back to /Rolf,
138

138

00:06:06,300  -->  00:06:08,400
this now is the thing that doesn't work,
139

139

00:06:08,400  -->  00:06:10,860
because it's no longer a valid endpoint.
140

140

00:06:10,860  -->  00:06:12,770
Alright, so that's everything for this video.
141

141

00:06:12,770  -->  00:06:15,370
Wanted to cover blueprints and show you how they work
142

142

00:06:15,370  -->  00:06:16,950
because we're going to be using them
143

143

00:06:16,950  -->  00:06:19,700
pretty extensively in this project.
144

144

00:06:19,700  -->  00:06:20,750
Thanks for joining me.
145

145

00:06:20,750  -->  00:06:22,200
I'll see you on the next one.
