1
00:00:05,300 --> 00:00:06,050
Welcome back.

2
00:00:06,050 --> 00:00:11,900
This is technically part two of structuring a large flash application so far we haven't really changed

3
00:00:11,900 --> 00:00:13,370
the contents of any files.

4
00:00:13,370 --> 00:00:19,350
We've simply just reorganized everything into separate directory components for the views that py files.

5
00:00:19,350 --> 00:00:24,090
However we'll need to add them as blueprints and then register them to our IT thought by file.

6
00:00:24,260 --> 00:00:28,920
So here is we're really actually going to be editing the actual contents of a python file.

7
00:00:29,180 --> 00:00:34,130
So far we've really just been using the same files as before in a different location.

8
00:00:34,130 --> 00:00:40,610
So what's left to do is to add in the views that PI content at in the blueprints register those blueprints

9
00:00:41,000 --> 00:00:46,550
then add in the app that PI content for main application file and then I want to do a quick review of

10
00:00:46,550 --> 00:00:49,420
how to actually do your for calls in the nav bar.

11
00:00:49,460 --> 00:00:50,900
When you're using blueprints.

12
00:00:50,900 --> 00:00:53,690
So just a couple of steps here and I'll actually go pretty fast.

13
00:00:53,690 --> 00:00:57,780
I think you'll find it quite surprising how simple at the end they'll all be OK.

14
00:00:57,800 --> 00:00:58,600
Let's get started.

15
00:00:58,800 --> 00:00:59,050
OK.

16
00:00:59,050 --> 00:01:01,670
Here I am back at the project from our last lecture.

17
00:01:01,670 --> 00:01:07,940
The things we still need to do are set up our views that py file both for owners and for puppies.

18
00:01:07,940 --> 00:01:13,910
Then after that we'll connect them to our in it up PI file as well as fill up our app that by then at

19
00:01:13,910 --> 00:01:18,170
the end of all this we'll run our migrations create our database and then see to make sure everything

20
00:01:18,170 --> 00:01:19,580
works OK.

21
00:01:19,820 --> 00:01:23,650
Let's start by going into our owner's directory.

22
00:01:24,020 --> 00:01:28,200
And then underneath owners directly underneath it I'll create a new file.

23
00:01:28,990 --> 00:01:29,920
Called views up high.

24
00:01:29,920 --> 00:01:32,580
In fact I've actually already created that file right here.

25
00:01:32,830 --> 00:01:40,590
So again the pathway that I'm at right now is owner's views that pi and this whole thing is under the

26
00:01:40,710 --> 00:01:42,610
my project folder.

27
00:01:42,660 --> 00:01:43,930
So this is the file I'm in right now.

28
00:01:43,950 --> 00:01:53,150
My project owner's views that by first thing going to do is say from flask import and I'm going to import

29
00:01:53,210 --> 00:02:03,740
blueprints and I'll also import render template redirect and then your L for that I need to actually

30
00:02:03,740 --> 00:02:05,040
reference that database.

31
00:02:05,090 --> 00:02:17,730
So to do that I'll save from my project import D.B then also save from my project models import owner

32
00:02:18,740 --> 00:02:24,070
and then from my project owners.

33
00:02:24,270 --> 00:02:34,400
Well that right owners thought forms import add a form.

34
00:02:34,420 --> 00:02:40,080
OK so what this is doing is it's importing the database from that in it that pile which is underneath

35
00:02:40,110 --> 00:02:45,630
my project and then my project it has its own models up-I file remember we have that owner model in

36
00:02:45,630 --> 00:02:46,190
there.

37
00:02:46,230 --> 00:02:51,380
We'll be needing it and then we'll also be needing the form from the owners folder.

38
00:02:51,390 --> 00:02:56,330
So we say from project or my project owners forms import add form.

39
00:02:56,340 --> 00:03:01,940
So it's basically saying underneath this file are here owners fill their forms up by grabbing this ad

40
00:03:01,950 --> 00:03:05,990
form OK now that we've done that.

41
00:03:05,990 --> 00:03:07,220
Let's set up the blueprint.

42
00:03:07,310 --> 00:03:16,140
I'm going to create a variable code owners blueprints and I'm going to equal to an instance of a blueprint.

43
00:03:16,160 --> 00:03:24,150
First thing to do is we'll give it a name so we'll just call it owners and then we pass an the built

44
00:03:24,150 --> 00:03:29,620
in name variable and then finally you assign a template folder essentially where should it look for

45
00:03:29,620 --> 00:03:30,840
its templates.

46
00:03:30,940 --> 00:03:37,720
And in this case to make sure it works we just say template underscore folder is equal to.

47
00:03:37,720 --> 00:03:44,090
And I will say templates for Slash owners and they're going you can always copy and paste this code

48
00:03:44,540 --> 00:03:45,920
from our actual file.

49
00:03:47,040 --> 00:03:49,320
Ok so so far we set up our own blueprint.

50
00:03:49,530 --> 00:03:56,850
Now what I'm going to do is I'm going to copy and paste the actual view for adding an owner.

51
00:03:56,870 --> 00:03:59,770
So this is pretty much the same view that we saw earlier.

52
00:03:59,800 --> 00:04:05,120
It's just an out of view except this time it's just called add ins that have an owner recreate the form

53
00:04:05,210 --> 00:04:08,240
that we imported from our owners foreign file.

54
00:04:08,240 --> 00:04:13,610
We say OK if everything's valid on some mission we just initiate a new owner and add them to our database

55
00:04:13,940 --> 00:04:16,710
and then we redirect and notice redirecting.

56
00:04:16,910 --> 00:04:18,470
I'm saying your L for.

57
00:04:18,710 --> 00:04:23,270
And instead of saying Just list now I'm saying puppies that list because when you start registering

58
00:04:23,270 --> 00:04:28,770
the blueprints what's going to happen is you're gonna be able to register it to this name dot the view.

59
00:04:28,850 --> 00:04:35,680
So later on if I want to link to owners ad I would just say owners ad which you'll see later on.

60
00:04:35,840 --> 00:04:41,390
Now the next step is to actually route this at a view using our owner's blueprint.

61
00:04:41,390 --> 00:04:49,550
So what we do is we create a decorator and say owner's blueprints route and they're going to pass and

62
00:04:49,580 --> 00:04:50,560
the route you want.

63
00:04:50,570 --> 00:04:53,680
So in this case we'll say forward slash and and notice here.

64
00:04:53,690 --> 00:04:55,180
I don't need to say owners.

65
00:04:55,310 --> 00:05:01,070
Slash and that's going to be in the registering of the blueprint later on in there in at that pie file.

66
00:05:01,070 --> 00:05:06,590
And since there is a form in this view just like we did earlier on it's same methods as equal to the

67
00:05:06,650 --> 00:05:12,840
list of gets post and then save those changes.

68
00:05:13,150 --> 00:05:15,760
OK let's quickly discuss what we just did here.

69
00:05:15,790 --> 00:05:17,650
We are normal imports with flask.

70
00:05:17,770 --> 00:05:23,170
Now we also added in blueprint then we're saying from my project import D-B.

71
00:05:23,260 --> 00:05:29,230
So when we say from my project import something I'm really saying go to that in that pie file underneath

72
00:05:29,230 --> 00:05:34,930
my project and import something from there that I'm saying OK from the models folder underneath my project

73
00:05:35,110 --> 00:05:42,430
import the owner model and then from my project subfolder owners so file forms import add a form.

74
00:05:42,430 --> 00:05:47,110
So that's how we're able to grab this out of form and this owner for our view.

75
00:05:47,110 --> 00:05:52,930
Again our view hasn't really changed what's changed is the routing for that view with the blueprint.

76
00:05:52,930 --> 00:05:54,520
Now let's check out views for puppies.

77
00:05:54,520 --> 00:05:56,060
Let's go to that file again.

78
00:05:56,080 --> 00:06:02,590
Right now I'm going to Neith my puppy's folder looking at these up high.

79
00:06:02,640 --> 00:06:04,800
So that's where I'm currently located.

80
00:06:04,860 --> 00:06:09,660
We're going to add in some imports and I'm going to copy and paste these imports save ourselves a little

81
00:06:09,660 --> 00:06:15,460
bit of time here essentially just importing the same lines as they did last time for my project B.

82
00:06:15,480 --> 00:06:21,120
Except this time I'm saying from puppies forms import add form and delete form and then from the models

83
00:06:21,240 --> 00:06:22,450
import the puppy model.

84
00:06:22,560 --> 00:06:25,470
Very similar to what we just did in the owners.

85
00:06:25,470 --> 00:06:34,610
Now what I'm going to do is set up my puppies underscore blueprint is equal to blueprints and I'm going

86
00:06:34,610 --> 00:06:45,230
to pass and puppies I'll passen the name variable and then we'll say template folder is equal to and

87
00:06:45,230 --> 00:06:48,470
the puppy templates can be found under templates.

88
00:06:48,470 --> 00:06:56,800
Puppies say that and then we need to set up our forms essentially our views for adding a puppy listening

89
00:06:56,840 --> 00:07:00,470
puppy and leading a puppy on copy and paste from the notes.

90
00:07:00,470 --> 00:07:03,350
They're essentially the same views that we just created earlier.

91
00:07:03,350 --> 00:07:09,050
So we have an ad puppy view almost the exact same thing as what we just saw for the owners except this

92
00:07:09,050 --> 00:07:11,550
time lotas to redirect saying puppies that list.

93
00:07:11,780 --> 00:07:17,700
So we're actually going to have a list of you as well so I'll say a list of you.

94
00:07:17,950 --> 00:07:20,470
And then finally we'll have a delete view.

95
00:07:20,770 --> 00:07:22,510
So it's copy and paste that as well.

96
00:07:22,630 --> 00:07:25,650
Copy and paste delete.

97
00:07:25,910 --> 00:07:29,130
The last thing I need to do is set up the routing for each of these views.

98
00:07:30,110 --> 00:07:41,530
So we're going to do is the following will say puppies underscore blueprints the route and for the add.

99
00:07:41,550 --> 00:07:42,900
I'm just going to say forward slash.

100
00:07:42,940 --> 00:07:48,130
And just like that and the owners except remember this is going to register two puppies forward slash.

101
00:07:48,130 --> 00:07:52,500
And then since there's a form in this page will say methods is equal to

102
00:07:56,130 --> 00:07:57,380
and post.

103
00:07:57,600 --> 00:07:59,580
So that sets up our view.

104
00:07:59,580 --> 00:08:04,890
Now let's set up our list view then we'll just connect the routing here on this list view we'll see

105
00:08:04,890 --> 00:08:10,700
at puppy's blueprint the route and we'll pass that to list.

106
00:08:10,830 --> 00:08:13,550
There's no form on this page so no need to set the methods.

107
00:08:13,710 --> 00:08:22,280
And then here will say puppies blueprints routes and then we'll say delete and this does have a method

108
00:08:22,310 --> 00:08:33,860
so say methods is equal to gets and post saved that OK.

109
00:08:34,170 --> 00:08:38,910
So essentially use these blueprints for routing within their own views that py file.

110
00:08:38,940 --> 00:08:44,400
But in order to make sure those routes work what we need to do is register those in our in it thought

111
00:08:44,400 --> 00:08:45,750
by file.

112
00:08:45,750 --> 00:08:52,560
So back underneath or in it that PI file underneath my project folder I've already set up my configurations

113
00:08:52,560 --> 00:08:58,590
for forms and my siecle databases and I set up my database as well as my ability to migrate my database

114
00:08:58,590 --> 00:09:00,590
with my application.

115
00:09:00,590 --> 00:09:06,020
Now the next step is to actually register those blueprints with my in at that PI file and something

116
00:09:06,020 --> 00:09:09,570
to note here is often you'll do all your imports at the top.

117
00:09:09,680 --> 00:09:15,560
But in order for the DB to be defined by the time the blueprints are using them when need to register

118
00:09:15,650 --> 00:09:19,590
blueprints after we've done and defined DBI.

119
00:09:19,700 --> 00:09:22,210
So right below this we're going to say from.

120
00:09:22,310 --> 00:09:29,390
Let me create some new lines here so we can see the center screen will say from my project

121
00:09:32,380 --> 00:09:41,550
that puppies that use import puppies blueprint and then we'll do the same for the owners will say from

122
00:09:42,360 --> 00:09:43,710
my project.

123
00:09:43,820 --> 00:09:49,760
The owner's views import owners blueprint.

124
00:09:49,830 --> 00:09:55,710
Then once we've done that we can grab our application and register those blueprints which is just a

125
00:09:55,710 --> 00:09:57,730
call register underscore blueprint.

126
00:09:57,990 --> 00:10:00,330
You pass in the blueprint that you just imported.

127
00:10:00,330 --> 00:10:06,540
So for example the owner's blueprint and then we give it to your prefix that we wanted to have.

128
00:10:07,050 --> 00:10:10,700
Typically it should match the name of that blueprint.

129
00:10:10,700 --> 00:10:17,430
So will say owners that will do the same thing for the puppies will say register the blueprint passin

130
00:10:17,700 --> 00:10:23,440
puppies blueprint your L prefix and set that equal to.

131
00:10:23,540 --> 00:10:31,050
In this case forward slash puppies OK say that and we've pretty much set up everything we need.

132
00:10:31,050 --> 00:10:37,470
The last thing we need to do is actually put everything in the app file to connect to our home page.

133
00:10:37,470 --> 00:10:39,870
So underneath our app that py file.

134
00:10:39,900 --> 00:10:46,680
So this is the very basic app that pi at the same level of the my project folder so keep that in mind

135
00:10:46,920 --> 00:10:48,000
underneath my project.

136
00:10:48,060 --> 00:10:51,040
If I collapse my project I still see that pie.

137
00:10:51,540 --> 00:11:01,770
So underneath this apple pie file I'll say from my project import app which is going to import the app

138
00:11:01,770 --> 00:11:11,530
created here on line 7 of the in that pie file and then they'll say from flask import rendered template.

139
00:11:11,970 --> 00:11:20,670
Then I just need to set up the route for the first page the home page will say app routes forward slash

140
00:11:21,900 --> 00:11:23,190
the index.

141
00:11:23,310 --> 00:11:30,710
And here I'm just going to say return rendered template of the home h T.M..

142
00:11:30,920 --> 00:11:35,610
So this is going to have direct access to any templates that were underneath the templates folder here.

143
00:11:35,660 --> 00:11:38,620
Base HTML and home page T.M..

144
00:11:38,770 --> 00:11:46,110
And then after all that will just say if name is equal to Main take my app and run it with debug mode

145
00:11:46,150 --> 00:11:50,990
equal to true save all those changes and we should be ready to begin testing.

146
00:11:51,040 --> 00:11:53,870
The last thing I want to do is actually set up our database.

147
00:11:54,100 --> 00:11:56,680
So open up our command line here.

148
00:11:57,840 --> 00:12:06,870
Now we're going to say flask D-B and it's and after your run that flask D-B in it.

149
00:12:06,900 --> 00:12:11,490
You should get something like this generating and then please add a configuration if you got some sort

150
00:12:11,490 --> 00:12:12,450
of error here.

151
00:12:12,480 --> 00:12:14,070
That means you made a typo.

152
00:12:14,160 --> 00:12:18,330
And if that's the case it's going to be pretty much impossible for us to help from our end.

153
00:12:18,330 --> 00:12:21,830
Instead what you should be doing is then just running our files directly.

154
00:12:21,870 --> 00:12:26,280
So please keep in mind at this stage if you've made a typo and you've gotten an error on Flast the be

155
00:12:26,310 --> 00:12:29,250
in it please use our files directly.

156
00:12:29,260 --> 00:12:31,890
OK with that being said we're going to do next.

157
00:12:31,900 --> 00:12:33,200
Say flask.

158
00:12:33,280 --> 00:12:45,750
D.B migrate and we'll just say created puppies and owners will hit enter that will create the migrations.

159
00:12:45,780 --> 00:12:51,070
And finally let's run those migrations by saying Flast the upgrade.

160
00:12:51,220 --> 00:12:54,850
Hit enter and now we have our tables and our models running.

161
00:12:55,140 --> 00:12:59,410
OK so now let's actually run this and see if it works.

162
00:12:59,490 --> 00:13:02,690
So what we need to do is run our actual application file.

163
00:13:02,940 --> 00:13:09,640
So I will say Python WIPs python app potpie enter.

164
00:13:10,000 --> 00:13:11,390
Looks like debug mode is on.

165
00:13:11,390 --> 00:13:13,540
So let's see if we were actually able to run this.

166
00:13:13,540 --> 00:13:15,370
If not we'll do a little debugging.

167
00:13:15,640 --> 00:13:17,620
OK we're going to bring in our site now.

168
00:13:17,620 --> 00:13:19,090
Welcome to the puppy adoption page.

169
00:13:19,090 --> 00:13:19,870
So that's good news.

170
00:13:19,870 --> 00:13:21,440
Looks like the home page is working.

171
00:13:21,490 --> 00:13:26,000
Let's now come to add pup and let's add in a puppy to this page.

172
00:13:26,050 --> 00:13:31,720
So let's just go ahead and add in Rufus as a puppy will add that puppy.

173
00:13:31,720 --> 00:13:34,820
And now we've been redirect to here's a list of all available puppies.

174
00:13:34,960 --> 00:13:35,950
Puppy name is Rufus.

175
00:13:35,960 --> 00:13:40,720
Has no owner assigned yet so the indexing here should start a position one for Rufus.

176
00:13:40,870 --> 00:13:47,620
So let's go ahead and add an owner for Rufus will set the name of the owner as David and the ID they

177
00:13:47,620 --> 00:13:53,460
should be linked to is then puppy 1 and then we'll add the owner.

178
00:13:53,670 --> 00:13:55,170
And now that takes me back to THE LIST.

179
00:13:55,260 --> 00:13:56,980
Here's a list of all available puppies.

180
00:13:57,090 --> 00:14:02,050
Puppy name is Rufus and owner is David perfect so looks like the last few We need to check is delete

181
00:14:02,070 --> 00:14:04,590
pup and then we'll click Remove puppy.

182
00:14:04,770 --> 00:14:06,570
And now here's a list of all the puppies.

183
00:14:06,570 --> 00:14:07,920
There's no more puppies there.

184
00:14:07,990 --> 00:14:08,460
OK.

185
00:14:08,610 --> 00:14:09,150
Perfect.

186
00:14:09,150 --> 00:14:11,340
Looks like everything's working just as before.

187
00:14:11,460 --> 00:14:16,290
Except now we have it in subcomponents which are going to be more manageable when we want to add more

188
00:14:16,290 --> 00:14:17,910
views to our project.

189
00:14:17,910 --> 00:14:18,270
All right.

190
00:14:18,270 --> 00:14:19,300
If you have any questions.

191
00:14:19,320 --> 00:14:21,660
Make sure to check our notes very carefully.

192
00:14:21,660 --> 00:14:24,470
This is probably one of those things where it's super easy to make a typo.

193
00:14:24,480 --> 00:14:30,330
As you follow along so I'd really suggest that you use our code as a skeleton framework instead of trying

194
00:14:30,330 --> 00:14:32,910
to type everything out manually yourself.

195
00:14:32,930 --> 00:14:34,590
OK we'll see you at the next lecture.
