1
00:00:01,210 --> 00:00:02,260
Hi again.

2
00:00:02,380 --> 00:00:08,890
And we can't consider this lecture as the last lecture about building our Web application,

3
00:00:08,950 --> 00:00:10,480
but we still have something more to do.

4
00:00:11,800 --> 00:00:18,870
We have to deploy our Web application on a live server so that we can visit that through you URL.

5
00:00:19,870 --> 00:00:23,620
Because for now, as you know, this is just running on our local computer.

6
00:00:25,640 --> 00:00:27,500
And yeah, the lecture is a short one.

7
00:00:27,800 --> 00:00:35,540
So I'm just going to calculate the average height out of the data base table and then send that average

8
00:00:35,600 --> 00:00:37,490
back to the user e-mail address.

9
00:00:38,600 --> 00:00:41,460
So let's do that. This is our send_email.py script.

10
00:00:42,230 --> 00:00:47,780
And we have to calculate that somewhere here in our main Python script.

11
00:00:49,100 --> 00:00:54,070
And how do we go about calculating an average out of the database table field?

12
00:00:55,110 --> 00:00:58,180
Well'd need all the values of that field.

13
00:00:58,820 --> 00:01:01,750
So that means we're talking about a query.

14
00:01:02,720 --> 00:01:04,790
And so when should we do that query?

15
00:01:05,330 --> 00:01:08,600
Well, we want to calculate the average.

16
00:01:08,930 --> 00:01:13,600
We also want to include the data of the current user to that average.

17
00:01:14,000 --> 00:01:19,310
Which means the data that this user is entering in their session.

18
00:01:20,060 --> 00:01:25,160
And yeah, we can see that here we are sending the user data to the database.

19
00:01:25,190 --> 00:01:27,980
So we are committing the changes to our database table.

20
00:01:28,420 --> 00:01:34,280
And that suggests we can calculate an average just of that statement.

21
00:01:34,730 --> 00:01:35,240
So here.

22
00:01:37,340 --> 00:01:41,240
Let's create a variable where we'll restore the average value.

23
00:01:41,840 --> 00:01:44,580
Let's say average_height.

24
00:01:45,660 --> 00:01:50,840
That would be equal to, again, db.session.query.

25
00:01:52,130 --> 00:02:00,320
And in this case, we need to apply an average function that the average function is made available

26
00:02:00,320 --> 00:02:00,830
by

27
00:02:03,830 --> 00:02:11,440
the core sqlalchemy library, which we have already installed. And the sql class.

28
00:02:11,540 --> 00:02:13,470
And we need func

29
00:02:13,490 --> 00:02:15,000
from the sql class

30
00:02:15,080 --> 00:02:15,950
of this library.

31
00:02:17,480 --> 00:02:21,800
And once we have done that, we you don't here in our query.

32
00:02:22,310 --> 00:02:24,920
And then we can set the arguments of our query.

33
00:02:25,310 --> 00:02:27,920
We can pass here the table field that we want to query.

34
00:02:28,850 --> 00:02:32,810
That'd be data.height.

35
00:02:33,140 --> 00:02:34,070
With an underscore.

36
00:02:34,550 --> 00:02:37,370
But we don't need the entire field.

37
00:02:37,390 --> 00:02:41,430
We only need the average data.

38
00:02:43,720 --> 00:02:45,160
Dot height_.

39
00:02:46,870 --> 00:02:53,290
So, again, the average methods of this class, which we imported out of sqlalchemy.

40
00:02:53,890 --> 00:03:02,290
And, you know, we are almost ready to send these average value to the send_email function.

41
00:03:02,620 --> 00:03:08,550
So that the send_email function sends the email to the user with that average so we can incorporate that

42
00:03:08,550 --> 00:03:08,750
here.

43
00:03:09,790 --> 00:03:16,080
But before we do that, it's good to make sure this statement here is working well.

44
00:03:16,600 --> 00:03:19,630
So print average_height.

45
00:03:20,950 --> 00:03:22,180
Save script.

46
00:03:22,780 --> 00:03:24,400
I have an invalid syntax there.

47
00:03:25,570 --> 00:03:29,380
You probably notice that I didn't write import here.

48
00:03:30,730 --> 00:03:31,690
I didn't notice that.

49
00:03:32,860 --> 00:03:34,510
And try again.

50
00:03:35,690 --> 00:03:44,460
Now it's working, so maybe reload the page and let's just enter some dummy data there.

51
00:03:46,280 --> 00:03:50,800
We just need to know this print function is producing the correct output.

52
00:03:51,200 --> 00:03:52,520
So b.com.

53
00:03:54,410 --> 00:03:58,910
And let me zoom out the page.

54
00:03:59,480 --> 00:03:59,720
Okay.

55
00:03:59,720 --> 00:04:05,200
I hope you see it and 176. Submit.

56
00:04:08,970 --> 00:04:11,800
The Web app is working smoothly as we can see it.

57
00:04:12,920 --> 00:04:15,570
And let's look at this line that is being printed in here.

58
00:04:16,320 --> 00:04:21,330
As you can see, this is not a number, so not exactly what we were expecting.

59
00:04:22,140 --> 00:04:23,630
This is an sql statement.

60
00:04:24,570 --> 00:04:27,510
Python is printing that out.

61
00:04:27,680 --> 00:04:29,970
We need to do a trick here again.

62
00:04:30,240 --> 00:04:35,550
And what that trick does is it applies this scalar() method to this object.

63
00:04:35,940 --> 00:04:42,870
So to this sql statement. Dot scalar with brackets and you don't pass anything in there.

64
00:04:43,320 --> 00:04:45,960
And what they do, do is it will extract the scalar 

65
00:04:45,990 --> 00:04:50,610
out of this statement and a scalar is just a number.

66
00:04:51,870 --> 00:04:53,280
So nothing fancy about that.

67
00:04:54,180 --> 00:04:55,260
Save the script.

68
00:04:55,980 --> 00:04:57,150
I'll try this again.

69
00:04:58,740 --> 00:05:02,100
Maybe c@b.com this time.

70
00:05:04,660 --> 00:05:07,370
And yet now we can see the actual average.

71
00:05:08,000 --> 00:05:09,790
So it's 176.

72
00:05:10,760 --> 00:05:13,580
But we have quite a lot of decimal points in there.

73
00:05:13,910 --> 00:05:17,660
And we surely don't want to send that number to the user

74
00:05:18,050 --> 00:05:20,120
as an e-mail.

75
00:05:20,120 --> 00:05:22,040
Let's apply something in there.

76
00:05:23,030 --> 00:05:26,100
And here, what I would do is apply a round function.

77
00:05:26,870 --> 00:05:33,500
And so average height equals to round the current average height.

78
00:05:35,120 --> 00:05:39,980
So what that will do, it will around of this number to one decimal point.

79
00:05:40,310 --> 00:05:47,780
Or if you leave this empty, it will just up the number without any decimal points, or you can go off

80
00:05:47,780 --> 00:05:49,670
to two or three or whatever you want.

81
00:05:50,350 --> 00:05:50,590
Good.

82
00:05:51,190 --> 00:05:53,360
And yeah, that should do it.

83
00:05:57,160 --> 00:05:57,490
D this

84
00:05:57,930 --> 00:05:59,490
time @b.com.

85
00:06:00,950 --> 00:06:02,930
Let me try another value.

86
00:06:03,450 --> 00:06:06,210
Let's say 200 and submit.

87
00:06:09,770 --> 00:06:17,320
Yeah, this is the average by adding 200 there, I increase the average from 176

88
00:06:17,330 --> 00:06:21,250
to 182 centimeters cm in the case of my application.

89
00:06:21,550 --> 00:06:23,250
Maybe you're using some different units there.

90
00:06:23,810 --> 00:06:25,750
Yeah, I think this is all clear.

91
00:06:26,160 --> 00:06:35,680
And let's go ahead now and send these average high value to the email function as well so that the email

92
00:06:35,680 --> 00:06:40,390
function, then we'll do something like average, let's call this avarage_height as well.

93
00:06:40,480 --> 00:06:41,680
So it's just a parameter.

94
00:06:42,220 --> 00:06:43,830
It would be a local variable in here.

95
00:06:44,320 --> 00:06:46,540
It doesn't have to be the same as this one.

96
00:06:47,860 --> 00:06:49,860
Good, average height.

97
00:06:49,900 --> 00:06:54,480
So we could do something like average height.

98
00:06:55,780 --> 00:06:57,380
Let me maximize this.

99
00:06:57,670 --> 00:07:05,990
O all is a placeholder, maybe a period there than inside the tuple

100
00:07:06,040 --> 00:07:14,470
we pass the first variable for the first placeholder and the second valuable variable for the second

101
00:07:14,470 --> 00:07:15,010
placeholder.

102
00:07:15,960 --> 00:07:16,720
That should do it.

103
00:07:17,170 --> 00:07:24,940
Oh, we're almost done there, except of if we let me save this and resize this.

104
00:07:26,260 --> 00:07:35,560
So, yes, we're almost done, except this function is being called before we even check for an existing

105
00:07:35,560 --> 00:07:38,900
e-mail address in our database, which means if we pass

106
00:07:38,920 --> 00:07:45,430
now, if we pass average_height there, we average calculated by these two statements wouldn't make much

107
00:07:45,430 --> 00:07:48,430
sense because we haven't calculated the average height just yet.

108
00:07:49,870 --> 00:07:54,580
We don't want to send the user an average without getting their data as well.

109
00:07:54,880 --> 00:07:55,410
They height

110
00:07:56,290 --> 00:08:03,670
Which means we take this statement, called it from there and put it in here.

111
00:08:05,530 --> 00:08:05,920
In here.

112
00:08:06,850 --> 00:08:09,340
Make sure you indented just like that.

113
00:08:09,750 --> 00:08:09,880
No.

114
00:08:10,720 --> 00:08:12,460
So email_height, average_height.

115
00:08:12,550 --> 00:08:15,030
And we send those three in here.

116
00:08:16,750 --> 00:08:24,070
And, you know, we could also calculate something very quickly here, which is the count

117
00:08:24,070 --> 00:08:24,640
and db.session.

118
00:08:25,090 --> 00:08:26,380
I'll explain that in a minute.

119
00:08:27,730 --> 00:08:29,890
Query and data.height.

120
00:08:30,090 --> 00:08:33,640
And give me the count.

121
00:08:36,300 --> 00:08:41,130
So what we're actually counting are the height values that we have in our database table.

122
00:08:41,340 --> 00:08:42,300
Why would you want that?

123
00:08:42,360 --> 00:08:49,020
Well, you use something, the average value to the users, but they may also want to have an idea of

124
00:08:49,020 --> 00:08:52,020
how statistically significant is this value.

125
00:08:52,560 --> 00:08:59,460
Like, if you have three values collected in total, that wouldn't make much sense to say, like the

126
00:08:59,460 --> 00:09:04,260
average value of population or whatever is this one here.

127
00:09:05,040 --> 00:09:12,420
So it's good to send the user count, actually, which we were calculating in here.

128
00:09:13,530 --> 00:09:17,880
So up here, we did almost the same thing, but here we applied a filter.

129
00:09:18,360 --> 00:09:27,030
So we filter only those rows where the data.email, the value of this field was equal to the email

130
00:09:27,030 --> 00:09:29,580
address that the user was entering in that session.

131
00:09:30,600 --> 00:09:32,280
But here we are getting all the rows.

132
00:09:32,940 --> 00:09:33,710
So count.

133
00:09:33,840 --> 00:09:38,700
And then we pass that to the function call.

134
00:09:38,700 --> 00:09:39,950
Count.

135
00:09:41,120 --> 00:09:42,690
Also in here.

136
00:09:43,820 --> 00:09:46,660
Count. You're not seeing these.

137
00:09:46,680 --> 00:09:49,980
So let me do View, Toggle Soft Wrap.

138
00:09:50,740 --> 00:09:53,700
And now we have the average height of all.

139
00:09:56,210 --> 00:09:56,780
Is.

140
00:09:58,100 --> 00:10:10,640
Is this value here and that is calculated out of this number of users of people?

141
00:10:11,210 --> 00:10:11,480
Maybe.

142
00:10:13,300 --> 00:10:19,680
And you can't hear that looks good almost.

143
00:10:19,780 --> 00:10:23,650
We can also make these bold, strong, they're

144
00:10:26,950 --> 00:10:29,790
strong here, strong.

145
00:10:29,800 --> 00:10:32,860
They're there as well.

146
00:10:34,330 --> 00:10:34,690
Good.

147
00:10:36,220 --> 00:10:38,890
Finally, I'll say this.

148
00:10:39,100 --> 00:10:41,490
See if we get any syntax error there.

149
00:10:41,530 --> 00:10:41,920
No.

150
00:10:43,260 --> 00:10:46,860
Everything looks good, and, boy, save this as well.

151
00:10:47,370 --> 00:10:52,500
I think it's now safe to test through the application, but in order to get that email, you want to

152
00:10:52,500 --> 00:10:58,830
make sure you are using an email address that you haven't used before in your in that application.

153
00:10:59,520 --> 00:11:01,800
So you don't want to send the next e-mail address.

154
00:11:02,520 --> 00:11:08,760
I'll make sure this is a database to collect it and go to school and you'd want to delete.

155
00:11:09,060 --> 00:11:14,730
So if I do select all for all data, there's there's a table.

156
00:11:15,510 --> 00:11:18,210
You'll see that I have this my email address there.

157
00:11:18,930 --> 00:11:25,510
And I want to delete that to delete from data aware email.

158
00:11:26,430 --> 00:11:30,410
So the name of the field and you want to use single quotes.

159
00:11:30,690 --> 00:11:35,550
So e-mail is equal to my Hotmail address.

160
00:11:37,620 --> 00:11:37,720
Yeah.

161
00:11:39,480 --> 00:11:39,940
Execute.

162
00:11:40,130 --> 00:11:42,310
That's Waldo affected.

163
00:11:43,220 --> 00:11:48,030
I can prove that data.

164
00:11:50,190 --> 00:11:52,050
Yeah, my Hotmail addresses.

165
00:11:52,080 --> 00:11:52,710
No, they're not.

166
00:11:53,220 --> 00:11:55,350
So I can go ahead and

167
00:11:59,580 --> 00:12:00,210
try this.

168
00:12:00,240 --> 00:12:01,800
No, I'm

169
00:12:05,200 --> 00:12:05,780
good.

170
00:12:06,180 --> 00:12:06,640
Solid.

171
00:12:09,240 --> 00:12:10,400
I have an error there.

172
00:12:11,390 --> 00:12:17,900
Uh, Dutil has no attribute highlight because in here that should be another school.

173
00:12:18,920 --> 00:12:19,180
Mm hmm.

174
00:12:19,380 --> 00:12:23,520
So that's the name of the column, as you can see in here.

175
00:12:23,820 --> 00:12:25,290
So it's high with another school.

176
00:12:26,120 --> 00:12:26,250
Yeah.

177
00:12:26,520 --> 00:12:27,260
Sorry for that.

178
00:12:27,270 --> 00:12:28,710
The errors happen all the time.

179
00:12:29,380 --> 00:12:29,840
Mm.

180
00:12:31,050 --> 00:12:32,030
Go back again.

181
00:12:33,000 --> 00:12:37,580
If I try this again, it seems like we've got something from that e-mail address.

182
00:12:37,630 --> 00:12:37,990
All right.

183
00:12:38,520 --> 00:12:44,680
So at least we know that when we enter an existing e-mail address that is not working yet.

184
00:12:45,020 --> 00:12:56,640
I have to go here again and delete from Dotel where mail equals to.

185
00:13:04,020 --> 00:13:09,620
So this is a query here and execute Wataru affected.

186
00:13:09,810 --> 00:13:10,200
Good.

187
00:13:11,020 --> 00:13:12,810
Oh, let me try this again.

188
00:13:13,120 --> 00:13:15,030
I hope this will work this time.

189
00:13:18,060 --> 00:13:18,630
Submit.

190
00:13:21,680 --> 00:13:23,120
Thank you for your submission.

191
00:13:23,240 --> 00:13:26,000
Now, why should expect an e-mail?

192
00:13:28,440 --> 00:13:29,250
That was quick.

193
00:13:29,750 --> 00:13:31,850
Hey, there you are, Heidi, swell, 76.

194
00:13:31,920 --> 00:13:35,020
Average height of all is this.

195
00:13:36,060 --> 00:13:38,610
That is calculated on or five people.

196
00:13:39,480 --> 00:13:41,970
And we're done with our application.

197
00:13:42,320 --> 00:13:42,560
Sorry.

198
00:13:42,640 --> 00:13:43,890
That was too much.

199
00:13:44,010 --> 00:13:47,220
But I believe we did quite a lot of things through all the lectures.

200
00:13:49,590 --> 00:13:53,190
The good part of this is that you can modify this application.

201
00:13:53,580 --> 00:13:55,080
This is highly customizable.

202
00:13:55,470 --> 00:13:56,560
You can customize this.

203
00:13:56,580 --> 00:13:57,480
You can tweak it.

204
00:13:57,690 --> 00:14:03,710
And you can adjust it so that you gather your own data or any other information that you get.

205
00:14:03,840 --> 00:14:08,890
You may might be interested on any of these scraps of these instructions.

206
00:14:09,480 --> 00:14:15,680
But the next lecture, I'll show you how to get this application, have database and deployed on a life

207
00:14:15,680 --> 00:14:16,020
server.

208
00:14:16,500 --> 00:14:17,160
Quite interesting.

209
00:14:17,950 --> 00:14:18,100
Yeah.

210
00:14:18,210 --> 00:14:19,290
Talk to you in the next lecture.

