1
00:00:05,430 --> 00:00:11,120
Welcome everyone to this lecture on structuring a large flask application previously we just reviewed

2
00:00:11,120 --> 00:00:14,480
the overall structure of our newly reformatted flask webapp.

3
00:00:14,480 --> 00:00:19,010
Now it's actually time to implement this restructuring since you've already written a lot of the code

4
00:00:19,010 --> 00:00:20,300
in previous lectures.

5
00:00:20,300 --> 00:00:23,460
Mainly we're just going to be copying and pasting from the provided files.

6
00:00:24,940 --> 00:00:30,460
Now we're actually going to do this through the use of blueprints flask has a built in blueprints capability

7
00:00:30,760 --> 00:00:35,010
which will allow us to register modular components for our flask application.

8
00:00:35,080 --> 00:00:39,080
This way we can easily reference a view for each component.

9
00:00:39,140 --> 00:00:43,570
For example we're going to have to the rectories one for owners and one for puppies.

10
00:00:43,730 --> 00:00:47,140
And each of those that rectories will have its own separate views that py file.

11
00:00:47,330 --> 00:00:51,950
So essentially we'll have two views that PI file one for owners and one for puppies.

12
00:00:52,020 --> 00:00:55,310
Then each of these views is going to have their own ad view.

13
00:00:55,310 --> 00:00:59,240
So that means there's going to be a way to add owners and a way to add puppies.

14
00:00:59,240 --> 00:01:05,390
Now typically what's going to happen is there would be some sort of confusion as far as when you say

15
00:01:05,540 --> 00:01:08,880
slash add row if you're referring to owners or puppies.

16
00:01:09,110 --> 00:01:14,510
Because now you have two views that profile file previously would call one forward slash at owner and

17
00:01:14,510 --> 00:01:16,800
the other one for Slash at puppies.

18
00:01:16,890 --> 00:01:18,150
How do we take care of this problem.

19
00:01:18,160 --> 00:01:20,860
And now we have two separate views that py files.

20
00:01:20,880 --> 00:01:25,670
Well in order for the flask application to not get confused with the ad route that's where we use blueprints

21
00:01:27,170 --> 00:01:32,340
the blueprints are going to allow us to register a your a prefix for each views that py file.

22
00:01:32,480 --> 00:01:38,210
So we're going to automatically be able to reference add as a view in each of these views that pi and

23
00:01:38,210 --> 00:01:43,640
then separately with the use of blueprints flask is going to automatically register your old prefix

24
00:01:43,880 --> 00:01:49,310
so that when we actually run this on our web application we'll see owners for Slash add and puppies

25
00:01:49,310 --> 00:01:52,540
for slushed ads separately.

26
00:01:52,860 --> 00:01:57,150
So overall the major differences in our code for this lecture are going to be restructuring and the

27
00:01:57,150 --> 00:02:05,080
project folders adding in blueprints and then registering the blueprints in an in the pie file.

28
00:02:05,200 --> 00:02:05,830
Please no.

29
00:02:05,920 --> 00:02:10,750
It's really easy to make a simple typo that actually causes an error throughout this entire project.

30
00:02:10,870 --> 00:02:14,880
So please make sure you follow along first very carefully and use our notes directly.

31
00:02:14,920 --> 00:02:17,200
Make sure not to do any modification Tornos.

32
00:02:17,200 --> 00:02:21,790
So if you're ever getting an error for this particular lecture it's almost impossible for us to help

33
00:02:21,790 --> 00:02:22,230
fix it.

34
00:02:22,230 --> 00:02:23,260
From our end.

35
00:02:23,350 --> 00:02:29,290
Instead what you should really do is copy and use our note files directly because any simple typo is

36
00:02:29,290 --> 00:02:30,810
going to be really hard to catch.

37
00:02:32,530 --> 00:02:36,630
OK so again this lecture is really focusing on the overall structure of the fullers files.

38
00:02:36,630 --> 00:02:40,940
Most of the actual content of the files is just going to be copied and pasted from our notes.

39
00:02:40,950 --> 00:02:43,770
All right let's get started by heading over to the text editor.

40
00:02:45,120 --> 00:02:50,100
OK so here you have the structure text file opening and we're going to set up all these files and folders

41
00:02:50,100 --> 00:02:53,970
first and then we'll just copy and paste the actual content into them.

42
00:02:53,970 --> 00:02:57,270
A lot of these files and folders We've actually already created.

43
00:02:57,270 --> 00:03:01,020
For instance we've already made these templates for adding owners and adding puppies as well as the

44
00:03:01,020 --> 00:03:02,310
forms and views.

45
00:03:02,310 --> 00:03:07,560
All we're doing now is we're just separating them into their separate components for owners and puppies

46
00:03:07,620 --> 00:03:11,640
and then we'll manually add in blueprints as well as manually set it up.

47
00:03:11,650 --> 00:03:12,670
This in a file.

48
00:03:12,770 --> 00:03:17,040
But first let's go over here to the left hand tab and start creating these folders.

49
00:03:17,160 --> 00:03:21,970
First of all they're going to create or first file I should say is my app that py file.

50
00:03:22,930 --> 00:03:30,580
So say create app that pi and then all start by creating my project folder so first thing to do here

51
00:03:30,940 --> 00:03:40,550
is say new folder and will say my project and you want to make sure you follow the same naming convention

52
00:03:40,590 --> 00:03:41,230
I do.

53
00:03:41,230 --> 00:03:43,990
Otherwise your source code won't work.

54
00:03:44,340 --> 00:03:48,750
So underneath my project will create a couple of directories.

55
00:03:48,900 --> 00:03:51,450
One will be our owners directory.

56
00:03:51,450 --> 00:03:55,260
That's the components where all the owners related materials are going to go.

57
00:03:55,260 --> 00:03:58,230
Then I'll create a new folder called Puppies.

58
00:03:58,310 --> 00:04:05,790
Let's roll up puppies material and then I'll create a new folder called templates and these are going

59
00:04:05,790 --> 00:04:10,950
to be the top level templates things like my bass H.T. mail and my home H.T. file other templates are

60
00:04:10,950 --> 00:04:13,200
going to go within owners and puppies.

61
00:04:13,320 --> 00:04:17,270
And then after that I'll create a couple more files.

62
00:04:17,310 --> 00:04:24,420
One is going to be my underscore underscore and it underscore underscore that py file and the last one

63
00:04:24,420 --> 00:04:31,690
is going to be a model that py file that's going to store models and technically we could also separate

64
00:04:31,690 --> 00:04:36,170
this model's not pie into owners and puppies but in this case there's only two models that exist so

65
00:04:36,230 --> 00:04:38,540
it's not really worth the effort of separating it out.

66
00:04:39,510 --> 00:04:44,370
OK so now it's time to go into owners and actually start building this out.

67
00:04:44,420 --> 00:04:49,700
And in fact we're going to do a really similar thing for both owners and puppies so owners will create

68
00:04:50,090 --> 00:04:53,690
a new folder here called templates.

69
00:04:53,870 --> 00:04:55,690
Now we're going to the same thing within puppies.

70
00:04:55,910 --> 00:04:59,240
So new folder templates.

71
00:04:59,240 --> 00:05:03,860
So these are templates that are associated with puppies and owners and the actual structure for these

72
00:05:03,860 --> 00:05:04,540
to work.

73
00:05:04,670 --> 00:05:11,720
You have to then the way flask works is make a new folder underneath owners templates and create another

74
00:05:11,960 --> 00:05:13,150
owner's folder.

75
00:05:13,170 --> 00:05:16,190
So notice here in my structure it gives owners templates.

76
00:05:16,190 --> 00:05:18,060
Owners will do the same thing for puppies.

77
00:05:18,060 --> 00:05:21,620
Puppies templates new folder.

78
00:05:21,620 --> 00:05:22,850
Puppies.

79
00:05:22,850 --> 00:05:23,270
OK.

80
00:05:23,540 --> 00:05:25,540
So this is really common as far as a structuring.

81
00:05:25,570 --> 00:05:29,490
It basically allows flasks to read and make sure it's connecting to the right template.

82
00:05:29,810 --> 00:05:32,820
Later on we'll add outreached him team files there ourselves.

83
00:05:32,840 --> 00:05:36,630
So right now we have our template file their structure set up.

84
00:05:36,680 --> 00:05:45,160
Now we just need to add for both owners and puppies will create their own forms that py file and then

85
00:05:45,180 --> 00:05:49,590
also create their own views that profile.

86
00:05:49,960 --> 00:05:50,860
And again notice here.

87
00:05:50,860 --> 00:05:57,400
Same level as templates so owners has a template directory then forms interviews and basically people

88
00:05:57,400 --> 00:05:59,170
is going to have the same thing.

89
00:05:59,200 --> 00:06:01,630
So templates is already set up for puppies.

90
00:06:01,630 --> 00:06:09,610
So then we're just going to save puppies and you file forms thought pie and then puppies and you file

91
00:06:11,340 --> 00:06:13,140
views PI OK.

92
00:06:13,410 --> 00:06:16,950
And this is basically the main structure we had to set up.

93
00:06:16,950 --> 00:06:21,030
Now we have our owners components all within the owner's directory.

94
00:06:21,210 --> 00:06:26,100
It has it's going to have its own formed its own views and its own templates within its owner's directory

95
00:06:26,100 --> 00:06:29,120
here and puppies is going to work the exact same way.

96
00:06:29,400 --> 00:06:36,240
OK let's start off by continuing an ad in the files that really have no changes between our last iteration

97
00:06:36,270 --> 00:06:39,690
of this puppy adoption Web site versus our restructuring right now.

98
00:06:39,690 --> 00:06:43,230
So those are mainly going to be the templates and the models that Paey file.

99
00:06:43,470 --> 00:06:46,070
So lets open up a start for models that py file.

100
00:06:46,080 --> 00:06:52,490
So just to make it clear right now I'm in models that pie and this is essentially the top level there's

101
00:06:52,500 --> 00:06:54,420
only one models not pite file.

102
00:06:54,430 --> 00:07:00,090
All we're going to do is we're going to copy and paste what we previously had before.

103
00:07:00,170 --> 00:07:06,050
So I'm going to say models that pi and copy and paste from our previous iteration of this project the

104
00:07:06,080 --> 00:07:09,960
owner class and then the puppy class.

105
00:07:09,990 --> 00:07:15,440
The one thing that we're going to have to take care of is how we actually get this D-B the model.

106
00:07:15,540 --> 00:07:19,290
So we're going to set that all up in our in it that PI file.

107
00:07:19,830 --> 00:07:25,140
So this underscore underscore in its underscore underscore that pi which is under the my project folder

108
00:07:25,470 --> 00:07:31,050
that's going to contain all the code that creates the application and the database in order to import

109
00:07:31,050 --> 00:07:36,120
that since it's underneath the init PI file underneath the my project folder.

110
00:07:36,120 --> 00:07:41,720
All we need to say is from my project import D-B.

111
00:07:42,060 --> 00:07:56,720
And that means that we're going to need to set up D-B inside the in a PI file under the my project folder.

112
00:07:56,740 --> 00:07:57,190
OK.

113
00:07:57,310 --> 00:08:02,080
So this is essentially something that's on our to do list set up this at about pi file but everything

114
00:08:02,080 --> 00:08:06,770
else as far as setting up the puppy model and the owner model that looks exactly the same as we had

115
00:08:06,770 --> 00:08:08,200
it before.

116
00:08:08,200 --> 00:08:12,730
Suddenly to see these changes again and just copy and pasting here from the notes but our models up

117
00:08:12,850 --> 00:08:15,010
file is now done.

118
00:08:15,010 --> 00:08:20,420
The next thing I want to do is add in the templates that go at the top level of my project.

119
00:08:20,500 --> 00:08:24,970
So let me expand templates right now and create some new files inside of templates.

120
00:08:24,970 --> 00:08:29,510
Basically we just need to add the top level one is based on HVM L..

121
00:08:29,710 --> 00:08:31,750
So I'm going to copy and paste that in right now.

122
00:08:31,750 --> 00:08:33,750
This essentially just holds the nav bar.

123
00:08:34,240 --> 00:08:37,410
So will copy and paste that save that in number.

124
00:08:37,450 --> 00:08:42,550
We have the nav bar and then we say block content and we'll go over these actually your L's and just

125
00:08:42,550 --> 00:08:43,060
a little bit.

126
00:08:43,120 --> 00:08:44,650
That's going to be slightly different.

127
00:08:45,130 --> 00:08:53,470
And then we'll say templates new file and I'll say home thought HVM L so home that each T.M. that's

128
00:08:53,470 --> 00:08:56,420
essentially going to look pretty much exactly the same as our previous home.

129
00:08:56,800 --> 00:09:02,100
It's just going to be extending from based l a jumbotron and I'll say welcome to our adoption page.

130
00:09:02,200 --> 00:09:06,140
Please click on the links for the nav bar so I'll save that right there.

131
00:09:06,460 --> 00:09:13,490
So far we finished our templates and our models not pi file since our models that PI file is now finished.

132
00:09:13,620 --> 00:09:16,690
Let's go ahead and sync it to the in it up high.

133
00:09:16,700 --> 00:09:19,540
So remember I said we had to set up this from my project.

134
00:09:19,570 --> 00:09:21,000
Import D-B line.

135
00:09:21,000 --> 00:09:26,310
So I'm going to go to my in it up PI again just to make it clear I'm right now and underscore underscore

136
00:09:26,460 --> 00:09:32,820
in its underscore underscore that PI underneath the my project folder.

137
00:09:32,820 --> 00:09:34,810
So that's the current file I'm in.

138
00:09:34,830 --> 00:09:47,870
What I'm going to do is import os and then I'll say from flask import flask from flask sequel alchemy

139
00:09:48,970 --> 00:09:50,190
import sequel alchemy.

140
00:09:50,230 --> 00:09:53,920
Again a lot of the same imports we used to have in our apple pie file.

141
00:09:53,920 --> 00:09:56,490
Except now they're going underneath the pie.

142
00:09:56,620 --> 00:10:01,990
And last thing we're going to say is from flask migrates import

143
00:10:04,980 --> 00:10:08,170
then we'll say app is equal to flask.

144
00:10:08,490 --> 00:10:11,500
Well passen the builtin name variable.

145
00:10:12,000 --> 00:10:14,320
And now we're going to set up our configurations.

146
00:10:14,460 --> 00:10:21,670
So it is that bar configurations for our database so we're going to set up our base directory as OS

147
00:10:22,060 --> 00:10:31,920
path absolute path and then OS path directory name and we'll pass and file just as we've done before.

148
00:10:31,990 --> 00:10:33,170
So nothing really new here.

149
00:10:33,190 --> 00:10:39,950
It's just going in a different location and then we'll set up our app config first one is going to be

150
00:10:39,950 --> 00:10:46,630
the sequel alchemy underscore database underscore.

151
00:10:46,910 --> 00:10:52,120
You are I basically where the actual sequel database is going to be located.

152
00:10:52,270 --> 00:10:53,320
Elysée sequel.

153
00:10:53,540 --> 00:10:55,990
Colin three forward slashes.

154
00:10:56,270 --> 00:11:02,820
And then we'll put in the paff are currently located essentially OS pathic join based directory they

155
00:11:03,030 --> 00:11:04,840
did the sequel.

156
00:11:06,920 --> 00:11:07,370
OK.

157
00:11:07,410 --> 00:11:12,180
So we set up our sequel alchemy database just as we've done many times in the past and they say app

158
00:11:12,180 --> 00:11:21,470
config and we'll say sequel alchemy underscore track modifications.

159
00:11:21,810 --> 00:11:23,340
And just as we've done before.

160
00:11:23,580 --> 00:11:31,440
Well synthetical to false then I'll set up D-B is equal to sequel alchemy pasand the app.

161
00:11:31,660 --> 00:11:38,230
And then also perform migrations on the app by passing the app and the database and to migrate save

162
00:11:38,240 --> 00:11:39,270
those changes.

163
00:11:39,320 --> 00:11:45,050
And now again I'm in the in it up file underneath my project folder and I've set up my application as

164
00:11:45,050 --> 00:11:50,090
well as the configurations I need for my sequel Alcmene database I've created that database and now

165
00:11:50,120 --> 00:11:52,500
I have the ability to migrate on that database.

166
00:11:52,520 --> 00:11:58,580
So if I take a look at models up-I I've now successfully linked from my project import the B thus directly

167
00:11:58,580 --> 00:12:03,830
calling in at that pipe because it's underneath my project folder and it's going to import this D-B

168
00:12:03,890 --> 00:12:04,790
right here.

169
00:12:05,030 --> 00:12:07,820
The last configuration I want to make here is for the forms.

170
00:12:07,820 --> 00:12:10,010
Remember we need to configure a secret key.

171
00:12:10,010 --> 00:12:11,000
So go ahead and do that.

172
00:12:11,000 --> 00:12:20,330
Right below we create the app will see app config secret underscore key and you can just set this to

173
00:12:20,330 --> 00:12:21,200
any string you want.

174
00:12:21,220 --> 00:12:21,910
It's.

175
00:12:22,040 --> 00:12:28,920
My secret key save that now we're not finished yet with this in it that PI file.

176
00:12:28,960 --> 00:12:33,270
The last thing is going to have to do is register the actual blueprints but we haven't done that yet.

177
00:12:33,430 --> 00:12:35,580
So right now we'll just leave it as is.

178
00:12:35,830 --> 00:12:36,350
OK.

179
00:12:36,550 --> 00:12:38,740
So we have our pie file set up.

180
00:12:38,800 --> 00:12:44,850
Now let's go ahead and start setting up the templates for owners and puppies as well as the forms.

181
00:12:44,860 --> 00:12:49,700
So next up we're going to do is start setting up forms for puppies.

182
00:12:49,960 --> 00:12:56,200
So right now I am in the forms that PI file underneath puppies and I'm just going to copy and paste

183
00:12:57,480 --> 00:13:00,530
the code that we previously used for our adoption site.

184
00:13:00,690 --> 00:13:06,360
Again just importing flask form and importing the fields and I have an add form and a delete form for

185
00:13:06,360 --> 00:13:08,790
the puppies.

186
00:13:08,790 --> 00:13:13,560
Notice how I don't need to clarify further that this is add a puppy form or delete puppies form.

187
00:13:13,560 --> 00:13:18,750
That clarification is already going to happen automatically for me because I have this as a subcomponent

188
00:13:18,810 --> 00:13:20,720
of the puppies directory.

189
00:13:21,150 --> 00:13:27,750
That being said let's now go to our owners forms potpie file and I was again going to copy and paste

190
00:13:27,750 --> 00:13:28,950
this from the notes.

191
00:13:28,950 --> 00:13:38,330
So the forms file for the owners and let me make it clear again I'm right now under owner folder forms

192
00:13:38,330 --> 00:13:44,120
up high here and is going to copy and paste basically the same thing except now it says out of form

193
00:13:44,410 --> 00:13:45,320
for the owner.

194
00:13:45,320 --> 00:13:48,450
So I have name pop ID and submit again.

195
00:13:48,500 --> 00:13:53,960
It's really crucial to note here that I have forms that are now sharing the same name as a form under

196
00:13:53,960 --> 00:13:58,220
puppies and then add a form under owners.

197
00:13:58,220 --> 00:14:03,100
What's crucial is that when I utilize blueprints later on in the views there's not going to be any mix

198
00:14:03,100 --> 00:14:05,200
up as far as what form is what.

199
00:14:05,270 --> 00:14:07,910
Because they're already in their own subcomponent folders.

200
00:14:08,180 --> 00:14:15,320
OK so I going to say those changes in my forms that pie and other forms are essentially taken care of.

201
00:14:15,320 --> 00:14:19,910
Now all I need to add in are the actual template files are going to be using.

202
00:14:19,910 --> 00:14:22,690
So let's go back and add in those template files.

203
00:14:22,790 --> 00:14:25,050
Essentially those are just are HVM l files.

204
00:14:25,130 --> 00:14:27,860
We'll start with the puppies they actually have more templates.

205
00:14:27,860 --> 00:14:32,600
So underneath puppies templates puppies are going to create some new files.

206
00:14:33,200 --> 00:14:36,680
So the first one is going to be ad HVM L..

207
00:14:37,100 --> 00:14:41,210
So I will say copy and paste from the notes.

208
00:14:41,360 --> 00:14:43,250
Again this is just adding a puppy.

209
00:14:43,250 --> 00:14:47,160
Remember we grab that form and then add a puppy to our database.

210
00:14:47,660 --> 00:14:49,020
So save that there.

211
00:14:49,190 --> 00:14:51,140
Then we want to be able to delete a puppy.

212
00:14:51,140 --> 00:14:58,910
So create a new file we'll call delete that age DML and then we're going to copy and paste this pretty

213
00:14:58,910 --> 00:15:03,230
much the same files we've been working with save that so that a puppy get adopted.

214
00:15:03,230 --> 00:15:07,580
We thought the forums forms removed the puppy from the list and then we want to be able to list all

215
00:15:07,580 --> 00:15:16,890
the puppies so all say new file lists h TNL and then they're going to copy and paste from the notes

216
00:15:17,960 --> 00:15:18,830
save that change.

217
00:15:18,840 --> 00:15:22,130
And now I have all the views I need for my puppies.

218
00:15:22,130 --> 00:15:25,990
Now I want to add one view for my owners and that's going to be an add.

219
00:15:26,000 --> 00:15:30,230
Owner HTL form so underneath that I'll say new file.

220
00:15:30,590 --> 00:15:39,700
I will call it an underscore owner the h t m l and I'm going to copy this and paste it.

221
00:15:40,040 --> 00:15:45,700
Save it again this is an owner h t m l underneath the owner's templates OK.

222
00:15:45,720 --> 00:15:50,340
So basically what we've done so far is we've added in all the files that essentially don't need any

223
00:15:50,340 --> 00:15:53,260
major modification for the blueprint restructuring.

224
00:15:53,400 --> 00:15:55,780
And those are things like your templates and your forms.

225
00:15:55,890 --> 00:15:58,710
The only modifications you'll need is the naming scheme.

226
00:15:58,710 --> 00:16:01,300
Sometimes maybe sometimes you want to simplify it.

227
00:16:01,680 --> 00:16:06,750
And again notice here on forms we have to out of forms previously would have something like a puppy

228
00:16:06,750 --> 00:16:08,200
form and an owner form.

229
00:16:08,340 --> 00:16:12,430
But now since they're in their own subcomponents we don't need to do that in the very next lecture.

230
00:16:12,450 --> 00:16:17,250
We're going to continue our restructuring by now focusing in on the views that py files.

231
00:16:17,250 --> 00:16:20,160
Those are the ones that are going to be slightly different because that's what we're actually going

232
00:16:20,160 --> 00:16:22,560
to add in these blueprint calls.

233
00:16:22,560 --> 00:16:25,820
We'll see you in the next lecture where we start manipulating with blueprints.
