1
00:00:00,503 --> 00:00:02,103
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,103 --> 00:00:04,021
In this video, we're going to be adding

3
00:00:04,021 --> 00:00:05,522
a bunch of things that
we need in our project

4
00:00:05,522 --> 00:00:07,939
in order to run it on Heroku.

5
00:00:09,462 --> 00:00:11,787
Now, the first thing we have to add is

6
00:00:11,787 --> 00:00:15,817
a way to tell Heroku what
Python version we're using.

7
00:00:15,817 --> 00:00:18,533
And indeed, that we are using Python.

8
00:00:18,533 --> 00:00:21,286
So, let's right click on our Code folder,

9
00:00:21,286 --> 00:00:24,301
and add a new file, which we have to call,

10
00:00:24,301 --> 00:00:26,884
this is important, runtime.txt.

11
00:00:27,877 --> 00:00:30,919
It's going to tell Heroku
what sort of language

12
00:00:30,919 --> 00:00:33,169
and what version we're using.

13
00:00:33,169 --> 00:00:37,252
And the contents of this
have to be python-3.5.2.

14
00:00:38,739 --> 00:00:40,782
Okay, that is it.

15
00:00:40,782 --> 00:00:44,115
Now, over time, this version may change.

16
00:00:46,254 --> 00:00:49,754
So, do be aware that it may go up to 3.5.3

17
00:00:51,279 --> 00:00:52,860
in the future and so on.

18
00:00:52,860 --> 00:00:55,770
I'll have a link in
the lecture description

19
00:00:55,770 --> 00:00:58,966
so that you can go and
see if this is still

20
00:00:58,966 --> 00:01:02,041
the latest version and you
can change that if not, okay?

21
00:01:02,041 --> 00:01:04,484
So for now, it's Python
3.5.2, but that may

22
00:01:04,484 --> 00:01:06,213
change in the future.

23
00:01:06,213 --> 00:01:08,100
So, keep an eye on the link in the

24
00:01:08,100 --> 00:01:10,267
lecture description, okay?

25
00:01:11,759 --> 00:01:13,499
So, that's the runtime.txt.

26
00:01:13,499 --> 00:01:16,051
And that's everything
that that should contain.

27
00:01:16,051 --> 00:01:19,224
Then we're also going to need to tell

28
00:01:19,224 --> 00:01:22,277
Heroku how to instal the libraries that we

29
00:01:22,277 --> 00:01:24,474
are using in this app.

30
00:01:24,474 --> 00:01:27,666
So remember, we're using
Flask, Flask-RESTful,

31
00:01:27,666 --> 00:01:30,755
Flask-JWT, and Flask-SQLAlchemy.

32
00:01:30,755 --> 00:01:34,023
So, we have to tell Heroku
that these libraries

33
00:01:34,023 --> 00:01:36,395
are in use and that it should instal them.

34
00:01:36,395 --> 00:01:40,422
Python has a standard way
of telling other systems

35
00:01:40,422 --> 00:01:42,873
what libraries we're using and that's in

36
00:01:42,873 --> 00:01:45,456
a file called requirements.txt.

37
00:01:47,053 --> 00:01:48,439
Once again, the spelling
is really important,

38
00:01:48,439 --> 00:01:49,856
requirements.txt.

39
00:01:51,924 --> 00:01:53,717
And in here, we'll just put the library

40
00:01:53,717 --> 00:01:55,017
names that we're using.

41
00:01:55,017 --> 00:01:57,140
So, we're using Flask with a capital F,

42
00:01:57,140 --> 00:02:00,064
Flask-RESTful, with the REST in caps,

43
00:02:00,064 --> 00:02:02,731
Flask-JWT, and Flask-SQLAlchemy.

44
00:02:04,035 --> 00:02:04,868
Okay?

45
00:02:06,517 --> 00:02:08,977
Now, I remember also
mentioning in the lecture

46
00:02:08,977 --> 00:02:10,846
at the start of this section that there's

47
00:02:10,846 --> 00:02:13,224
something called UWSGI.

48
00:02:13,224 --> 00:02:17,344
UWSGI is used to interact
with our Flask app

49
00:02:17,344 --> 00:02:21,777
and make it available for
others to go and access it.

50
00:02:21,777 --> 00:02:25,674
Flask can do this as well but
it's just not quite as good.

51
00:02:25,674 --> 00:02:29,033
UWSGI gives us things like more efficient

52
00:02:29,033 --> 00:02:32,959
multi-process support,
it gives us things like

53
00:02:32,959 --> 00:02:36,502
restarting processes if they
hang up and things like that.

54
00:02:36,502 --> 00:02:40,121
So, we're also going
to instal that, uwsgi.

55
00:02:40,121 --> 00:02:43,365
You don't have to instal
UWSGI in your computer.

56
00:02:43,365 --> 00:02:46,036
If you're using an editor such as PyCharm,

57
00:02:46,036 --> 00:02:49,265
as soon as you create
this requirements.txt file

58
00:02:49,265 --> 00:02:52,052
and you save it, it will
tell you whether you want to

59
00:02:52,052 --> 00:02:55,036
instal UWSGI, and in your computer,

60
00:02:55,036 --> 00:02:57,595
you don't have to instal UWSGI, okay?

61
00:02:57,595 --> 00:03:00,446
If you try, it is likely
it will fail because

62
00:03:00,446 --> 00:03:03,768
you need a C Compiler to instal UWSGI.

63
00:03:03,768 --> 00:03:04,767
So, don't bother.

64
00:03:04,767 --> 00:03:05,765
Don't worry about it.

65
00:03:05,765 --> 00:03:08,000
You don't need UWSGI luckily.

66
00:03:08,000 --> 00:03:11,907
Heroku will be able to instal
it and will use it, okay?

67
00:03:11,907 --> 00:03:14,999
That's it for the requirements.txt file.

68
00:03:14,999 --> 00:03:19,879
Next, we have to create
another file called uwsgi.ini.

69
00:03:19,879 --> 00:03:23,346
UWSGI, make sure to spell it right.

70
00:03:23,346 --> 00:03:26,643
And what's going to go in here is some

71
00:03:26,643 --> 00:03:29,548
configuration parameters
for the UWSGI process

72
00:03:29,548 --> 00:03:30,798
to run our app.

73
00:03:32,582 --> 00:03:35,264
Okay, so the first thing
to do is to tag it.

74
00:03:35,264 --> 00:03:38,563
We're going to say uwsgi,
that's going to be the section.

75
00:03:38,563 --> 00:03:40,268
And underneath it is going to come the

76
00:03:40,268 --> 00:03:41,907
configuration parameters.

77
00:03:41,907 --> 00:03:43,697
First thing we have to
tell it is what port

78
00:03:43,697 --> 00:03:45,530
it is going to run on.

79
00:03:46,466 --> 00:03:48,328
Normally, as you remember, we're running

80
00:03:48,328 --> 00:03:51,078
our Flask apps on 127.0.0.1:5000.

81
00:03:52,696 --> 00:03:54,136
5000 there is the port.

82
00:03:54,136 --> 00:03:56,328
But Heroku has a very specific port that

83
00:03:56,328 --> 00:03:58,329
we must use, so we have to tell it

84
00:03:58,329 --> 00:04:02,496
http-socket is going to
be :$(PORT), like that.

85
00:04:07,151 --> 00:04:08,411
And what that's going
to do is that's going to

86
00:04:08,411 --> 00:04:11,799
read the port number from
the Heroku configuration,

87
00:04:11,799 --> 00:04:14,912
so we don't have to know
the port number exactly.

88
00:04:14,912 --> 00:04:18,745
Heroku is gonna tell us
when we use this file.

89
00:04:20,498 --> 00:04:22,455
Next, we're going to say master is true.

90
00:04:22,455 --> 00:04:24,113
That means we're going
to use a master process

91
00:04:24,113 --> 00:04:26,056
when we run UWSGI.

92
00:04:26,056 --> 00:04:28,310
That's something a bit more complicated.

93
00:04:28,310 --> 00:04:30,431
Essentially, there's going
to be a process controlling

94
00:04:30,431 --> 00:04:34,956
the other processes, which
are called slave processes.

95
00:04:34,956 --> 00:04:37,886
We're going to have die-on-term.

96
00:04:37,886 --> 00:04:40,489
If we put a true, that's
when a process terminates,

97
00:04:40,489 --> 00:04:43,125
we're gonna kill the UWSGI process just to

98
00:04:43,125 --> 00:04:45,464
free up the resources.

99
00:04:45,464 --> 00:04:47,079
We're going to tell it
the module, which is

100
00:04:47,079 --> 00:04:48,711
going to be app.app.

101
00:04:48,711 --> 00:04:50,700
Sorry, app:app.

102
00:04:50,700 --> 00:04:54,494
And this probably looks mildly familiar.

103
00:04:54,494 --> 00:04:56,296
The module that we're running is the

104
00:04:56,296 --> 00:04:58,997
Flask app that we're running.

105
00:04:58,997 --> 00:05:03,873
So that's inside app.py, and
the variable is called app.

106
00:05:03,873 --> 00:05:05,557
So it's going to look at that variable

107
00:05:05,557 --> 00:05:09,724
and that's what it's gonna
run as a UWSGI application.

108
00:05:11,017 --> 00:05:14,137
And finally, we can also tell
it memory-report is true.

109
00:05:14,137 --> 00:05:16,176
We probably will not use this in Heroku

110
00:05:16,176 --> 00:05:19,973
but nevertheless, it might
be interesting for later on.

111
00:05:19,973 --> 00:05:21,375
Okay.

112
00:05:21,375 --> 00:05:22,859
So this is everything we tell it.

113
00:05:22,859 --> 00:05:24,402
The socket that we want to run on.

114
00:05:24,402 --> 00:05:26,377
We say that master is true so it has that

115
00:05:26,377 --> 00:05:28,041
sort of master process.

116
00:05:28,041 --> 00:05:30,251
We kill it when the process terminates.

117
00:05:30,251 --> 00:05:32,163
And then we tell it the
module, which is gonna be

118
00:05:32,163 --> 00:05:35,423
the app.py file, and the app variable.

119
00:05:35,423 --> 00:05:37,449
And we're going to also initial as the

120
00:05:37,449 --> 00:05:39,452
memory report as well.

121
00:05:39,452 --> 00:05:43,332
Okay, and the last thing
that we need is a Procfile.

122
00:05:43,332 --> 00:05:46,757
So, that's just Procfile with a capital P.

123
00:05:46,757 --> 00:05:48,421
And what we're going to put here is what

124
00:05:48,421 --> 00:05:51,685
dyno we want to use in Heroku.

125
00:05:51,685 --> 00:05:54,265
Remember, a dyno is the server.

126
00:05:54,265 --> 00:05:56,192
But dyno's have specific types.

127
00:05:56,192 --> 00:05:59,942
This dyno is going to
connect to a HTTP port,

128
00:06:00,940 --> 00:06:04,327
for example, our app runs in port 5000.

129
00:06:04,327 --> 00:06:06,168
This dyno here is also
going to be listening

130
00:06:06,168 --> 00:06:09,571
on that port to receive incoming requests.

131
00:06:09,571 --> 00:06:13,297
So, it's important that
it is a dyno of type web.

132
00:06:13,297 --> 00:06:16,759
So we type web: and then
we're going to tell it

133
00:06:16,759 --> 00:06:19,889
what process it's going to run,

134
00:06:19,889 --> 00:06:21,502
what application it's going to run.

135
00:06:21,502 --> 00:06:25,616
So here we could do something
like python3.5 app.py.

136
00:06:25,616 --> 00:06:28,655
For example, and that would presumably

137
00:06:28,655 --> 00:06:32,467
use the python3.5 process to run app.py.

138
00:06:32,467 --> 00:06:34,464
But we're not going to do that because

139
00:06:34,464 --> 00:06:36,699
we're using UWSGI.

140
00:06:36,699 --> 00:06:40,536
So, all that we have to do is say uwsgi,

141
00:06:40,536 --> 00:06:41,369
uwsgi.ini.

142
00:06:42,295 --> 00:06:44,563
And what that's going to do
is what you would expect.

143
00:06:44,563 --> 00:06:47,943
Run the UWSGI process with this file,

144
00:06:47,943 --> 00:06:50,919
which is the configuration
file that we've created.

145
00:06:50,919 --> 00:06:54,166
And that is then going
to load up the Python app

146
00:06:54,166 --> 00:06:55,249
and run that.

147
00:06:56,633 --> 00:06:58,729
So, when we run the UWSGI process,

148
00:06:58,729 --> 00:07:01,616
that is going to listen in to this module,

149
00:07:01,616 --> 00:07:03,118
it's going to run that,
and that's going to

150
00:07:03,118 --> 00:07:05,451
start our Flask application.

151
00:07:06,736 --> 00:07:09,449
Okay, so hopefully all
of that makes sense.

152
00:07:09,449 --> 00:07:12,699
That's what we need for our deployment.

153
00:07:13,774 --> 00:07:16,978
So now, let's go back to Chrome here.

154
00:07:16,978 --> 00:07:19,921
Well actually, we'll have
to go back to the terminal,

155
00:07:19,921 --> 00:07:21,440
apologies, and make sure to

156
00:07:21,440 --> 00:07:23,273
commit, so git status.

157
00:07:25,339 --> 00:07:27,558
Look at that, git commit.

158
00:07:27,558 --> 00:07:28,975
Sorry, git add *.

159
00:07:31,491 --> 00:07:34,134
And now, these are all new
files, so we're going to

160
00:07:34,134 --> 00:07:35,467
git commit them.

161
00:07:36,430 --> 00:07:40,597
Adding Heroku required files
for running, for example.

162
00:07:42,951 --> 00:07:45,457
And then we're gonna git push.

163
00:07:45,457 --> 00:07:47,334
And now that we've set
up the master branch

164
00:07:47,334 --> 00:07:49,302
to track the remote branch, we no longer

165
00:07:49,302 --> 00:07:52,164
need to do that -umaster.

166
00:07:52,164 --> 00:07:54,905
Okay, so that commit has now appeared

167
00:07:54,905 --> 00:07:56,611
in our remote repository.

168
00:07:56,611 --> 00:07:58,466
We can reload this to have a wee look.

169
00:07:58,466 --> 00:08:02,044
And you can see that the
Procfile, requirements,

170
00:08:02,044 --> 00:08:06,768
runtime and uwsgi.ini have
appeared, which is good.

171
00:08:06,768 --> 00:08:10,441
And then we can go back to Heroku here,

172
00:08:10,441 --> 00:08:14,306
and we have to do something very specific.

173
00:08:14,306 --> 00:08:16,806
We have to go to the Settings.

174
00:08:18,530 --> 00:08:21,990
And in the Buildpacks section here,

175
00:08:21,990 --> 00:08:24,327
the Buildpacks section,
make sure to tell Heroku

176
00:08:24,327 --> 00:08:26,015
that we're using Python.

177
00:08:26,015 --> 00:08:27,228
So all we have to do is we have to say

178
00:08:27,228 --> 00:08:29,666
Add Buildpack, and then
select Python there

179
00:08:29,666 --> 00:08:31,425
and save the changes.

180
00:08:31,425 --> 00:08:33,036
Okay, now that's added.

181
00:08:33,036 --> 00:08:36,119
Heroku is now going to use its Python

182
00:08:37,022 --> 00:08:41,704
stuff to run our code, so we
can go to Deploy once again.

183
00:08:41,704 --> 00:08:42,704
And we're going to go down here and

184
00:08:42,704 --> 00:08:44,625
deploy a master branch, okay?

185
00:08:44,625 --> 00:08:47,352
Let's see if everything wants to work.

186
00:08:47,352 --> 00:08:49,718
And as you can see, it
tells you the Python app

187
00:08:49,718 --> 00:08:51,952
via our Procfile.

188
00:08:51,952 --> 00:08:54,477
It is now installing
Python 3.5.2, which is

189
00:08:54,477 --> 00:08:56,307
what we told it to.

190
00:08:56,307 --> 00:08:57,878
And then it's, as you can see up here,

191
00:08:57,878 --> 00:09:01,460
installing the requirements.txt file.

192
00:09:01,460 --> 00:09:02,603
So it's going to instal
all of the libraries

193
00:09:02,603 --> 00:09:04,964
from the requirements.txt,
and that includes

194
00:09:04,964 --> 00:09:06,714
Flask, Flask-RESTful,

195
00:09:09,086 --> 00:09:11,748
apologies, Flask-JWT, and FlaskSQLAlchemy,

196
00:09:11,748 --> 00:09:12,998
and also UWSGI.

197
00:09:14,182 --> 00:09:16,129
The other level is there
are Flask dependencies,

198
00:09:16,129 --> 00:09:18,879
such as Ginger, Click, and so on.

199
00:09:20,413 --> 00:09:22,881
Okay, and then it starts installing them,

200
00:09:22,881 --> 00:09:26,500
and it's installing everything down there.

201
00:09:26,500 --> 00:09:28,261
It's now installing
UWSGI, which is the one

202
00:09:28,261 --> 00:09:30,178
that takes the longest.

203
00:09:33,550 --> 00:09:35,871
And once it is done, we're
gonna have our own server

204
00:09:35,871 --> 00:09:37,788
running our Python app.

205
00:09:39,317 --> 00:09:40,765
So, let's have a look.

206
00:09:40,765 --> 00:09:42,382
See if we manage.

207
00:09:42,382 --> 00:09:46,330
It's declared the web
type, and as you can see,

208
00:09:46,330 --> 00:09:49,291
it's now identified that
this web type is valid.

209
00:09:49,291 --> 00:09:51,823
Now it's compressing
the app at the very end

210
00:09:51,823 --> 00:09:53,623
so it can put it in the server,

211
00:09:53,623 --> 00:09:55,062
and then it's gonna run it.

212
00:09:55,062 --> 00:09:57,042
And there you go.

213
00:09:57,042 --> 00:10:01,042
Now that's running, so
we can go and click View.

214
00:10:02,011 --> 00:10:04,561
And really, nothing should happen

215
00:10:04,561 --> 00:10:07,636
because this is a rest
API, and it's not meant

216
00:10:07,636 --> 00:10:10,539
to be running from a browser.

217
00:10:10,539 --> 00:10:14,706
And as you can see, we get here
an Application Error, okay?

218
00:10:17,224 --> 00:10:20,488
But that's the deployment valid, and now

219
00:10:20,488 --> 00:10:24,655
we are able to get our
application deployed to Heroku.

220
00:10:26,848 --> 00:10:27,849
In the next video, we're going to look at

221
00:10:27,849 --> 00:10:30,918
running the app
successfully and interacting

222
00:10:30,918 --> 00:10:32,811
with different postmen, but just know that

223
00:10:32,811 --> 00:10:36,003
your API is now live on the Internet

224
00:10:36,003 --> 00:10:39,227
and others can access it as well.

225
00:10:39,227 --> 00:10:40,512
So, congratulations.

226
00:10:40,512 --> 00:10:44,592
We now have our first
deployed app out there

227
00:10:44,592 --> 00:10:45,852
on the Internet.

228
00:10:45,852 --> 00:10:47,650
So that's it for this
video and I'll see you

229
00:10:47,650 --> 00:10:49,483
on the very next one.

