1 00:00:00,000 --> 00:00:04,877 [MUSIC] 2 00:00:04,877 --> 00:00:07,609 Now that we've learned a bit about SQLAlchemy, 3 00:00:07,609 --> 00:00:10,750 it's time to practice our skills with a small project. 4 00:00:11,900 --> 00:00:16,690 I have here a file full of great books to help you on your journey. 5 00:00:16,690 --> 00:00:20,581 Unfortunately, programming books don't have very diverse authors, but 6 00:00:20,581 --> 00:00:22,840 maybe that's something you can help with. 7 00:00:23,960 --> 00:00:25,273 For those working locally, 8 00:00:25,273 --> 00:00:28,780 you can download this file from the teacher's notes below. 9 00:00:28,780 --> 00:00:32,050 Together, we'll need to make a database to hold them. 10 00:00:32,050 --> 00:00:35,300 Then we'll create a console program that looks something like this. 11 00:00:35,300 --> 00:00:38,603 It will have a menu where users can add books to the database. 12 00:00:58,248 --> 00:01:00,106 You can search for a book. 13 00:01:06,751 --> 00:01:09,890 And then have the option to edit or delete it. 14 00:01:13,020 --> 00:01:18,961 The user can also check some data, like the most recently published book, 15 00:01:18,961 --> 00:01:22,467 or how many books there are in the database. 16 00:01:24,407 --> 00:01:25,743 Let's plan this out. 17 00:01:25,743 --> 00:01:28,984 Create a new project folder and open it in your IDE, or 18 00:01:28,984 --> 00:01:32,590 open the workspace attached to this video. 19 00:01:32,590 --> 00:01:35,690 We'll have two files, one to hold our database logic, and 20 00:01:35,690 --> 00:01:37,550 the other to hold our application. 21 00:01:38,620 --> 00:01:43,608 Let's call the database file models.py, and 22 00:01:43,608 --> 00:01:46,979 the application file app.py. 23 00:01:48,241 --> 00:01:55,051 Inside of models.py, let's think about what we'll need to do. 24 00:01:55,051 --> 00:01:59,974 We'll need to create a database and 25 00:01:59,974 --> 00:02:05,420 give it a name, let's say books.db. 26 00:02:05,420 --> 00:02:07,026 Then we'll need to create a model. 27 00:02:11,032 --> 00:02:15,594 And if we look at our data, we'll need to have a title, author, 28 00:02:15,594 --> 00:02:17,720 date published, and price. 29 00:02:26,767 --> 00:02:33,520 Cool, in app.py, we'll need to import our models file. 30 00:02:35,521 --> 00:02:38,813 Our application will need a main menu. 31 00:02:42,024 --> 00:02:46,254 That will have add, search, 32 00:02:46,254 --> 00:02:51,275 analysis, exit, and view options. 33 00:02:51,275 --> 00:02:59,415 We'll also need some functions to add books to the database. 34 00:02:59,415 --> 00:03:04,480 We'll need to edit books and delete them. 35 00:03:08,164 --> 00:03:13,562 We'll also need a search function, and 36 00:03:13,562 --> 00:03:16,467 probably some data cleaning functions too. 37 00:03:19,910 --> 00:03:27,330 Then we'll need to have a loop that's running our program. 38 00:03:27,330 --> 00:03:31,277 So when the user decides to exit, the program stops, nice. 39 00:03:31,277 --> 00:03:36,806 If you wanna plan more and be more detailed about what you'll be creating, 40 00:03:36,806 --> 00:03:39,630 then go ahead, this is your project. 41 00:03:39,630 --> 00:03:43,180 So write as many comments and pseudocode as you need. 42 00:03:44,680 --> 00:03:47,220 Let's start by creating our virtual environment. 43 00:03:49,370 --> 00:03:52,650 If you're in workspaces, you can skip this step. 44 00:03:52,650 --> 00:03:56,540 Also, I'm on a Mac, so if you're on Windows, check the teacher's notes below 45 00:03:56,540 --> 00:03:59,650 for a refresher on how to get your virtual environment working. 46 00:03:59,650 --> 00:04:02,250 The commands are slightly different. 47 00:04:03,550 --> 00:04:08,006 I'm gonna run python3 -m venv env. 48 00:04:12,815 --> 00:04:15,451 And yes, I would like to use it. 49 00:04:17,011 --> 00:04:22,402 You can see our folder was created, and then I need to activate it, 50 00:04:22,402 --> 00:04:29,160 source ./env/bin/activate, and it's been activated, perfect. 51 00:04:30,660 --> 00:04:38,020 Then we can install SQLAlchemy pip install sqlalchemy. 52 00:04:38,020 --> 00:04:41,200 That might take a little bit, nope, was super fast. 53 00:04:41,200 --> 00:04:47,857 And then I'm going to run pip freeze > requirements.txt to save 54 00:04:47,857 --> 00:04:52,950 our one requirement, which is just SQLAlchemy. 55 00:04:54,240 --> 00:04:57,443 So create that file, and you can give it a check by clicking on it, 56 00:04:57,443 --> 00:04:59,210 SQLAlchemy is in there, awesome. 57 00:05:00,530 --> 00:05:04,380 We're also going to create a repo for this project on GitHub. 58 00:05:04,380 --> 00:05:11,351 Let's create a file called .gitignore, New File, .gitignore. 59 00:05:13,091 --> 00:05:19,334 Inside, write the name of your environment folder, for me, it's env. 60 00:05:19,334 --> 00:05:23,627 Since we don't wanna send all those files up to our repo, 61 00:05:23,627 --> 00:05:26,280 these files are just for you. 62 00:05:26,280 --> 00:05:28,803 When someone wants to run your program, 63 00:05:28,803 --> 00:05:33,560 they'll use the requirements file to install SQLAlchemy for themselves. 64 00:05:35,040 --> 00:05:38,785 I'm also going to add /__pycache__. 65 00:05:38,785 --> 00:05:45,740 This is a common file that pops up when you're working in Python. 66 00:05:46,920 --> 00:05:51,805 You can also see this .vscode file was added here by Visual Studio. 67 00:05:51,805 --> 00:05:55,113 That one, we can also ignore. 68 00:05:55,113 --> 00:06:02,900 And another helpful one to include if you're on a Mac is .DS_Store. 69 00:06:02,900 --> 00:06:05,878 This is a file Macs throw into projects sometimes, and 70 00:06:05,878 --> 00:06:09,310 it doesn't really hurt anything by being there. 71 00:06:09,310 --> 00:06:11,600 But it doesn't help anything either, so let's just ignore it. 72 00:06:12,820 --> 00:06:20,400 Save the file, and then in the console, let's run git init, awesome. 73 00:06:20,400 --> 00:06:24,591 And now you can see, these are grayed out because they've been ignored. 74 00:06:24,591 --> 00:06:28,900 And these are all green because they are waiting to be added to our repo. 75 00:06:30,320 --> 00:06:32,472 Next is creating the repo on GitHub. 76 00:06:35,241 --> 00:06:40,237 You can navigate straight to github.com/new to create a new 77 00:06:40,237 --> 00:06:42,453 repo if you're logged in. 78 00:06:42,453 --> 00:06:44,623 Choose a name for your repo. 79 00:06:51,642 --> 00:06:54,980 And choose public or private, it's up to you. 80 00:06:59,013 --> 00:07:01,815 When you're ready, click create repository. 81 00:07:05,783 --> 00:07:14,486 Then we'll need to copy this code down here, Ctrl+C. 82 00:07:17,101 --> 00:07:23,541 And let's jump back into our terminal, Ctrl+V. 83 00:07:25,951 --> 00:07:33,560 Now let's run git add ., to add all of the files we've created so far. 84 00:07:34,660 --> 00:07:39,775 Then we're going to run git commit -m, 85 00:07:39,775 --> 00:07:46,040 and then our message, which is initial commit. 86 00:07:46,040 --> 00:07:47,750 This just means it's the first thing we've added. 87 00:07:49,340 --> 00:07:52,280 And it'll show you the five files that we've added. 88 00:07:53,790 --> 00:07:59,742 And then you'll want to run git push origin master. 89 00:08:01,683 --> 00:08:05,606 Awesome, back in your GitHub repo, refresh the page. 90 00:08:05,606 --> 00:08:08,616 And ta-da, all your code is here. 91 00:08:08,616 --> 00:08:12,750 Now we're ready to get started on our project.