NOTE
This file was generated by Descript <www.descript.com>
1

00:00:00,240  -->  00:00:04,200
Hi guys, and welcome back.

2

00:00:04,410  -->  00:00:08,870
In this video, we're going to be creating
a few files that Heroku needs in order to

3

00:00:08,870  -->  00:00:11,810
know what to run when we give it our code.

4

00:00:12,530  -->  00:00:16,490
The first thing it needs is it needs to
know what Python version or even what

5

00:00:16,490  -->  00:00:19,040
language to use when it looks at our code.

6

00:00:19,310  -->  00:00:21,950
So we need to tell it that we're
using Python and we need to

7

00:00:21,950  -->  00:00:23,480
tell it the version of Python.

8

00:00:24,020  -->  00:00:28,000
We do that with a file called runtime.txt.

9

00:00:28,710  -->  00:00:31,980
When we're deploying in Heroku, it's
very important that this file name is

10

00:00:31,980  -->  00:00:35,440
exactly as I have typed it: runtime.txt.

11

00:00:35,850  -->  00:00:42,100
And in here, all in lowercase, we're
going to put python dash 3 dot 9 dot 0.

12

00:00:42,400  -->  00:00:45,303
And that's it, we're just
going to leave this as that.

13

00:00:45,483  -->  00:00:49,863
And when Heroku reads this file, which
it will do immediately, when we give

14

00:00:49,863  -->  00:00:54,333
it our code, it will know that we are
dealing here with a Python app and the

15

00:00:54,333  -->  00:00:58,743
Python version that it should use when
it's running our code is Python 3.9.

16

00:00:59,703  -->  00:01:03,303
I'm going to leave a link in the resources
section of this lecture for some other

17

00:01:03,303  -->  00:01:08,043
values that you can put in runtime.txt
if you don't want to use Python 3.9.

18

00:01:08,546  -->  00:01:11,036
Now that we've got that
we need something else.

19

00:01:11,426  -->  00:01:14,696
We need to tell Heroku how to run our app.

20

00:01:15,193  -->  00:01:19,183
Normally, when we run our app here in
the terminal, we use flask run after

21

00:01:19,183  -->  00:01:22,963
activating the virtual environment,
but Heroku is not going to do that.

22

00:01:23,828  -->  00:01:28,448
Instead of Heroku needs to rely on
another library to run our code.

23

00:01:29,018  -->  00:01:32,168
So the first thing we're going to
do, is we're going to add that other

24

00:01:32,168  -->  00:01:34,838
library to our requirements.txt file.

25

00:01:35,168  -->  00:01:37,108
And that is gunicorn.

26

00:01:37,798  -->  00:01:42,908
gunicorn is a library that will run
our flask app for us, and it will

27

00:01:42,908  -->  00:01:46,908
add a few optimizations like the
ability to run in multiple processes

28

00:01:47,178  -->  00:01:49,338
and run it in a different way.

29

00:01:49,788  -->  00:01:52,758
I'm not going to get into a lot of
detail as to how it works, because

30

00:01:52,758  -->  00:01:57,558
it is quite complicated, but just
know that Heroku can't use flask run

31

00:01:57,708  -->  00:02:01,648
as we have been doing it, it needs
to use something else like gunicorn.

32

00:02:01,848  -->  00:02:04,338
And there's a few other options
that we could use as well but

33

00:02:04,338  -->  00:02:06,078
gunicorn is the simplest one.

34

00:02:06,580  -->  00:02:11,430
So when we have installed gunicorn
in Heroku, it will be able to use

35

00:02:11,430  -->  00:02:13,300
gunicorn to run our flask app.

36

00:02:13,990  -->  00:02:18,130
However, and this is an important
point, if you are using Windows

37

00:02:18,370  -->  00:02:23,560
on your local development machine,
like I am doing here, you cannot use

38

00:02:23,600  -->  00:02:26,470
gunicorn to run the flask app yourself.

39

00:02:26,920  -->  00:02:34,030
So if we do pip install gunicorn, this
will work even if you're on Windows, but

40

00:02:34,060  -->  00:02:39,700
when you try to use gunicorn to run your
flask app, that won't work. But that's

41

00:02:39,700  -->  00:02:43,540
okay because when we're coding here on
our computer, we can still use flask run

42

00:02:43,540  -->  00:02:45,280
and that will still work fine, no problem.

43

00:02:45,626  -->  00:02:49,586
It is just when we give the code to
Heroku that each  we'll useg unicorn.

44

00:02:49,976  -->  00:02:53,666
If you're using Mac or Linux,
you can use gunicorn in here

45

00:02:53,666  -->  00:02:54,946
and that will be fine as well.

46

00:02:55,550  -->  00:03:00,470
The next thing you have to do is create
a file that will tell Heroku what command

47

00:03:00,500  -->  00:03:02,690
to run in order to execute our app.

48

00:03:03,615  -->  00:03:08,505
We were doing flask run, Heroku's
going to use gunicorn, and the command

49

00:03:08,505  -->  00:03:11,865
itself goes in a file called Procfile.

50

00:03:12,225  -->  00:03:14,715
Make sure that this is
not Procfile dot txt.

51

00:03:14,715  -->  00:03:16,175
It's just Procfile.

52

00:03:16,578  -->  00:03:20,208
In here we have to type web and
then a colon, and then we're

53

00:03:20,208  -->  00:03:24,288
going to type out the command that
Heroku will use, which is gunicorn.

54

00:03:24,768  -->  00:03:29,808
And then it in quotation marks app,
colon create app just like that.

55

00:03:30,438  -->  00:03:32,628
So let me just break
this down for you guys.

56

00:03:33,108  -->  00:03:37,158
What's happening here is we're telling
Heroku that we are working on a web

57

00:03:37,158  -->  00:03:41,028
app and therefore, whatever this
produces is something that is going

58

00:03:41,028  -->  00:03:44,448
to be connected to the internet so
that other people can access it.

59

00:03:44,628  -->  00:03:47,238
That is what the web
part of this line does.

60

00:03:47,298  -->  00:03:49,638
It tells Heroku we're using its web stack.

61

00:03:50,478  -->  00:03:53,808
gunicorn is the name of the
application that Heroku will install

62

00:03:54,228  -->  00:03:56,635
from the requirements txt file.

63

00:03:56,635  -->  00:04:01,180
And this year is an argument to
gunicorn that tells it which app to run.

64

00:04:01,660  -->  00:04:07,570
We're getting the app.py file and inside
it, we're running the create_app function.

65

00:04:08,080  -->  00:04:11,710
And that is what's going to give
us the app that gunicorn we run.

66

00:04:12,730  -->  00:04:14,950
It's important that this is the
inside quotation marks because

67

00:04:14,950  -->  00:04:18,700
otherwise this colon there and these
brackets there will cause problems.

68

00:04:18,940  -->  00:04:21,100
So just make sure to put
it exactly like that.

69

00:04:21,700  -->  00:04:24,490
The last thing we have to do to prepare
our app for Heroku, if you haven't

70

00:04:24,490  -->  00:04:29,710
done so already is to make sure to put
your MongoDB URI here, inside these

71

00:04:29,710  -->  00:04:33,010
quotation marks, including your username
and password and everything else.

72

00:04:34,015  -->  00:04:37,075
What we're going to do next, in the next
few videos is we're going to put this

73

00:04:37,075  -->  00:04:39,325
code online so that Heroku can access it.

74

00:04:39,685  -->  00:04:43,915
And in a later video, we're going
to explore how to hide this from the

75

00:04:43,915  -->  00:04:48,835
code so that it's not accessible to
anybody who reads this app code that's

76

00:04:48,835  -->  00:04:52,255
because some people will be able to
read the app code and I'll explain

77

00:04:52,255  -->  00:04:54,385
how that can happen in the next video.

78

00:04:54,955  -->  00:04:55,315
All right.

79

00:04:55,495  -->  00:04:56,665
Thanks for joining me in this one.

80

00:04:56,755  -->  00:04:58,165
And I'll see you in the next one.
