NOTE
This file was generated by Descript <www.descript.com>
1

00:00:00,000  -->  00:00:01,410
Hi guys and welcome back.

2

00:00:01,590  -->  00:00:04,880
In this video, let me show you
how to use Flask's app factory to

3

00:00:04,880  -->  00:00:10,280
create apps instead of just hard
coding them over in the app file.

4

00:00:10,670  -->  00:00:14,570
The reason to use an app factory is
because I mentioned earlier on, that you

5

00:00:14,570  -->  00:00:20,270
shouldn't create the database and store
it in there directly in the main file.

6

00:00:20,480  -->  00:00:23,210
And by using an app factory,
we're going to prevent a lot of

7

00:00:23,210  -->  00:00:25,070
problems that can happen later on.

8

00:00:25,190  -->  00:00:26,570
So let me show you how to do that.

9

00:00:26,570  -->  00:00:27,320
It's real simple.

10

00:00:28,165  -->  00:00:32,392
We've got app.py and in
app.py, we're creating our app.

11

00:00:32,722  -->  00:00:34,822
We've got this app line there.

12

00:00:34,822  -->  00:00:38,362
We're then connecting to our
database and we are storing

13

00:00:38,362  -->  00:00:39,562
the database over in the app.

14

00:00:39,952  -->  00:00:43,312
We're then also defining
the end points of our app.

15

00:00:43,432  -->  00:00:46,732
So by all intents and purposes, we
can say that this creates our app.

16

00:00:47,250  -->  00:00:53,240
However, there is one situation where
this file might run multiple times.

17

00:00:53,690  -->  00:00:56,660
And that is when we
deploy our applications.

18

00:00:57,080  -->  00:01:01,400
Sometimes the deployment mechanism
can run this files multiple times.

19

00:01:01,480  -->  00:01:04,910
What will happen is we'll end up
creating multiple different MongoClients.

20

00:01:04,930  -->  00:01:07,510
One for each time that the file runs.

21

00:01:07,540  -->  00:01:08,740
Even though we don't want to do that.

22

00:01:09,130  -->  00:01:12,190
So by using an app factory, we're
going to solve that problem because

23

00:01:12,220  -->  00:01:16,930
even if the file runs multiple times,
the deployment mechanism will be smart

24

00:01:16,930  -->  00:01:19,270
enough to not create multiple apps.

25

00:01:19,750  -->  00:01:23,780
All we have to do is create a function
that must be called  create_app.

26

00:01:23,800  -->  00:01:25,030
This must be the name.

27

00:01:25,470  -->  00:01:27,265
And put everything inside it.

28

00:01:27,262  -->  00:01:28,042
That's really it.

29

00:01:28,432  -->  00:01:31,252
The only thing we have to remember
to do is at the end of the

30

00:01:31,252  -->  00:01:34,942
create_app function, return app.

31

00:01:35,272  -->  00:01:40,342
Because the Flask command line
interface, the Flask run is going

32

00:01:40,342  -->  00:01:44,638
to automatically see that there
is a create_app function, and it's

33

00:01:44,638  -->  00:01:46,718
going to run it to create your app.

34

00:01:47,193  -->  00:01:51,213
Note that you do have to put
all your endpoint registrations

35

00:01:51,243  -->  00:01:52,293
inside the function.

36

00:01:52,473  -->  00:01:55,713
And if you have a lot of them,
that's going to get quite long.

37

00:01:55,983  -->  00:01:59,253
But later on, we're going to learn
about Flask blueprints which will

38

00:01:59,253  -->  00:02:03,573
allow us to extract these end points
over to different files and just

39

00:02:03,573  -->  00:02:05,373
register them here with a single line.

40

00:02:05,373  -->  00:02:06,873
So it's going to be much simpler.

41

00:02:07,375  -->  00:02:09,865
If you have that, save
the file and Flask run.

42

00:02:09,865  -->  00:02:11,035
It should just run your app.

43

00:02:11,035  -->  00:02:12,595
And it'll work in exactly the same way.

44

00:02:12,745  -->  00:02:15,415
This is just future proofing for later on.

45

00:02:15,928  -->  00:02:19,528
Also, if you're interested in testing
your Flask apps, it's not something that

46

00:02:19,528  -->  00:02:22,408
we're going to cover in this course, but
it's something that is very popular to do.

47

00:02:22,828  -->  00:02:26,368
Using the app factory allows you
to run this function from your

48

00:02:26,368  -->  00:02:30,778
tests and create your app with
different configuration parameters.

49

00:02:30,868  -->  00:02:31,798
If you want to do that.

50

00:02:32,178  -->  00:02:32,418
All right.

51

00:02:32,448  -->  00:02:34,008
Thank you guys for
joining me in this video.

52

00:02:34,008  -->  00:02:36,318
Just a quick future-proofing like I said.

53

00:02:36,498  -->  00:02:40,198
But it's a very important and I recommend
that you do this for all your Flask apps.

54

00:02:40,218  -->  00:02:42,918
Just make sure to name your function
correctly and return app at the

55

00:02:42,918  -->  00:02:45,078
bottom, and you won't have any issues.

56

00:02:45,498  -->  00:02:48,018
Thanks guys for joining me,
I'll see you in the next video.
