1 00:00:00,300 --> 00:00:04,530 Flask makes it really easy to build routes, control HTTP methods and 2 00:00:04,530 --> 00:00:06,650 headers and parse and send out JSON. 3 00:00:06,650 --> 00:00:10,530 But why write all of that ourselves, we don't have to, right? 4 00:00:10,530 --> 00:00:13,450 There's an excellent package named Flask-RESTful that we're going 5 00:00:13,450 --> 00:00:14,750 to use to save ourselves some work. 6 00:00:15,910 --> 00:00:19,910 With Flask-RESTful, you create classes, kind of like models, that represent your 7 00:00:19,910 --> 00:00:24,260 resources and the HTTP methods that you want to allow on those resources. 8 00:00:24,260 --> 00:00:26,930 We're gonna spend a lot of time working with these resources, so 9 00:00:26,930 --> 00:00:27,910 let's get started right away. 10 00:00:29,140 --> 00:00:33,400 Okay well, the first thing I need to do is, install the necessary packages. 11 00:00:33,400 --> 00:00:37,730 So let's do pip install flask-restful and 12 00:00:37,730 --> 00:00:40,630 then, I need an ORM, so I'm gonna install peewee as well. 13 00:00:42,180 --> 00:00:43,990 I don't have to install Flask, 14 00:00:43,990 --> 00:00:49,330 because it's automatically installed on workspaces, so that's great for me. 15 00:00:49,330 --> 00:00:52,830 If you're working along at home, you'll need to install Flask as well. 16 00:00:52,830 --> 00:00:56,190 If Flask-restful doesn't already install it for you, I have a feeling it does. 17 00:00:56,190 --> 00:01:00,940 All right, so now, I want to make my basic app and my first models. 18 00:01:02,520 --> 00:01:06,450 I wanna be really creative, so I'm gonna call my app app.py. 19 00:01:06,450 --> 00:01:13,090 All right, easy enough and then, let's go ahead and we'll make our normal app. 20 00:01:13,090 --> 00:01:16,510 [from] flask import Flask 21 00:01:16,510 --> 00:01:22,040 then let's debug equal to true, host equal to 0.0.0.0. 22 00:01:22,040 --> 00:01:26,960 Since we're on workspaces we need to be looking at all the ports and not just or 23 00:01:26,960 --> 00:01:28,950 not, sorry not the ports, all the interfaces. 24 00:01:28,950 --> 00:01:34,150 And not just the looped back local host interface and then, port. 25 00:01:34,150 --> 00:01:35,238 Why do I have available here? 26 00:01:35,238 --> 00:01:41,966 We'll do 8000, all right, so port is at 8000. 27 00:01:41,966 --> 00:01:46,300 And then we'll make Flask(_ _name_ _), paying attention to the current file. 28 00:01:46,300 --> 00:01:50,600 And then let's add a route to this, just to be certain that things are working. 29 00:01:57,480 --> 00:02:00,272 And we'll just do @app.route at that forward slash, def hello_world and 30 00:02:00,272 --> 00:02:01,747 we'll just return 'Hello World'. 31 00:02:01,747 --> 00:02:06,154 All right, now let's get down here and if the name is equal to main, so 32 00:02:06,154 --> 00:02:09,540 if we're running the file directly. 33 00:02:09,540 --> 00:02:14,267 Then we're gonna do app.run debug=DEBUG, 34 00:02:14,267 --> 00:02:17,300 host=HOST and port=PORT. 35 00:02:18,640 --> 00:02:24,800 While I'm thinking of those, I'm gonna change my spaces to 4 and all right. 36 00:02:24,800 --> 00:02:28,910 So I'm gonna save that, I think we're good there. 37 00:02:28,910 --> 00:02:32,690 I'm not gonna even run and test this right now. 38 00:02:32,690 --> 00:02:33,700 Actually, I will. 39 00:02:33,700 --> 00:02:38,637 Let's go ahead and do that, so we'll do python app.py and 40 00:02:38,637 --> 00:02:41,816 then we'll hit here and go 8000. 41 00:02:41,816 --> 00:02:45,100 And cool, we got Hello World, great. 42 00:02:45,100 --> 00:02:47,810 All right, so now I wanna make my models. 43 00:02:47,810 --> 00:02:54,474 I'm gonna make another new file models.py and then let's setup these models. 44 00:02:54,474 --> 00:03:00,345 So we're going to import datetime and then from peewee import [*], 45 00:03:00,345 --> 00:03:05,681 I don't like doing that but that's the peewee standard. 46 00:03:05,681 --> 00:03:12,901 DATABASE = SqliteDatabase and I wanna call this courses.sqlite, 47 00:03:12,901 --> 00:03:17,530 you can call this whatever you want of course. 48 00:03:19,310 --> 00:03:23,170 All right and then make a course model and 49 00:03:23,170 --> 00:03:27,270 we'll give this a title, that's a CharField. 50 00:03:27,270 --> 00:03:32,490 And we give it a URL, that's a CharField and I'm gonna make this one (unique=True). 51 00:03:32,490 --> 00:03:35,630 So, that people don't submit the same URL twice, hopefully. 52 00:03:35,630 --> 00:03:40,370 I mean, you could maybe use a forward slash at the end of one of them and 53 00:03:40,370 --> 00:03:41,760 you don't at the end of the other one. 54 00:03:41,760 --> 00:03:45,540 But close enough, And then we will do a created_at,. 55 00:03:45,540 --> 00:03:51,911 Which is a DateTimeField and we will set a default in here of datetime.datetime.now. 56 00:03:51,911 --> 00:03:56,650 And we don't put the parenthesis, because we want to call the function. 57 00:03:56,650 --> 00:04:02,510 Whenever it creates the field and not whenever it instantiates the model. 58 00:04:02,510 --> 00:04:07,165 And we'll do class Meta and database=DATABASE. 59 00:04:07,165 --> 00:04:11,871 All right, so pretty straightforward model. 60 00:04:11,871 --> 00:04:16,090 This is some of the other models that we've made in the past and 61 00:04:16,090 --> 00:04:18,180 let's make one more here. 62 00:04:18,180 --> 00:04:23,566 Class Review which is also a model and 63 00:04:23,566 --> 00:04:27,922 course = ForeignKeyField. 64 00:04:27,922 --> 00:04:32,890 And this is related to course up here, and related name, 65 00:04:32,890 --> 00:04:35,800 I'm gonna call this review_set. 66 00:04:37,840 --> 00:04:40,410 And rating is an integer field, 67 00:04:42,610 --> 00:04:47,710 comment is a text field with a default of an empty string. 68 00:04:47,710 --> 00:04:49,940 Basically makes a thread, they don't have to put in a comment. 69 00:04:49,940 --> 00:04:51,880 We'll just record an empty string if they don't provide one. 70 00:04:53,000 --> 00:05:00,450 And then again, created_at is date time field, default is datetime.datetime.now. 71 00:05:00,450 --> 00:05:04,389 And that's mainly just for housekeeping, so 72 00:05:04,389 --> 00:05:07,924 that we can see when things were created. 73 00:05:07,924 --> 00:05:10,612 Database = DATABASE. 74 00:05:10,612 --> 00:05:15,905 And then let's set up an initialize function and 75 00:05:15,905 --> 00:05:22,760 we'll do DATABASE.connect, DATABASE.create_tables. 76 00:05:23,840 --> 00:05:26,030 And I wanna create course and review. 77 00:05:27,230 --> 00:05:30,903 We'll have this be safe, so those two tables already exist, it just passes over, 78 00:05:30,903 --> 00:05:32,955 it doesn't try to throw an error or anything. 79 00:05:32,955 --> 00:05:37,410 And then DATABASE.close(), all right. 80 00:05:37,410 --> 00:05:37,920 Cool. 81 00:05:37,920 --> 00:05:42,578 So two models, title and u.r.l created for that course and 82 00:05:42,578 --> 00:05:46,890 course, rating and comment and created_at for review. 83 00:05:46,890 --> 00:05:49,190 Nothing new or out of the ordinary there. 84 00:05:49,190 --> 00:05:52,730 Now I need to make sure that the app 85 00:05:52,730 --> 00:05:56,340 makes sure that the models are created whenever the app starts. 86 00:05:56,340 --> 00:05:59,520 So let's quit the app real quick, 87 00:06:02,350 --> 00:06:06,440 control C and then, let's come over here. 88 00:06:06,440 --> 00:06:11,240 And up here at the top, we'll add in import models 89 00:06:12,940 --> 00:06:15,900 and then down here in the name main, 90 00:06:15,900 --> 00:06:21,223 let's have it do models.initialize. 91 00:06:21,223 --> 00:06:23,636 Okay cool. 92 00:06:23,636 --> 00:06:27,267 So it will run this function whenever the app starts or 93 00:06:27,267 --> 00:06:29,940 it should but let's make sure of that. 94 00:06:31,180 --> 00:06:37,620 So, I will run that and if I refresh my column over here. 95 00:06:37,620 --> 00:06:39,245 Yeah there we go, courses.sqllite.