1 00:00:00,000 --> 00:00:00,970 2 00:00:00,970 --> 00:00:03,520 SPEAKER 1: This is see as CS50 Finance, where 3 00:00:03,520 --> 00:00:08,320 we'll make a website that allows users to sign up, register, log in, and buy 4 00:00:08,320 --> 00:00:11,052 and sell stocks or shares that they're interested in. 5 00:00:11,052 --> 00:00:13,510 Well, first, there's going to be a lot of distribution code 6 00:00:13,510 --> 00:00:14,980 for us to read over. 7 00:00:14,980 --> 00:00:19,711 There are Python files, HTML files, and even one database file, .db. 8 00:00:19,711 --> 00:00:21,460 And we have to understand how all of those 9 00:00:21,460 --> 00:00:23,560 interact with one another and the functions 10 00:00:23,560 --> 00:00:28,480 that we've provided that will make this problem a lot easier to handle. 11 00:00:28,480 --> 00:00:31,120 Then, once we read through all of that, we'll 12 00:00:31,120 --> 00:00:34,750 implement register, where users can sign up for the website. 13 00:00:34,750 --> 00:00:36,910 Then, we'll implement quote, which allows 14 00:00:36,910 --> 00:00:39,220 users to look up the price for a stock. 15 00:00:39,220 --> 00:00:43,690 Buy allows them to buy a certain number of shares of that stock. 16 00:00:43,690 --> 00:00:45,940 Then we'll edit the landing page and index, 17 00:00:45,940 --> 00:00:50,050 such that logged in users can see their transaction history as well 18 00:00:50,050 --> 00:00:53,080 as some other information about the cash that they have on hand 19 00:00:53,080 --> 00:00:55,490 and the total value of their holdings. 20 00:00:55,490 --> 00:00:59,320 We'll of course allow them to sell any shares that they have 21 00:00:59,320 --> 00:01:01,820 and view the history of those transactions. 22 00:01:01,820 --> 00:01:04,030 Finally, you'll have the freedom to implement 23 00:01:04,030 --> 00:01:07,220 an additional feature of your choosing. 24 00:01:07,220 --> 00:01:09,760 Now, this may seem like a lot of to-dos. 25 00:01:09,760 --> 00:01:14,780 And I'm also going to add some subtasks under each category. 26 00:01:14,780 --> 00:01:17,440 So it might be a little overwhelming, but as always, we're 27 00:01:17,440 --> 00:01:22,950 going to take this bit by bit and break it down into manageable bites. 28 00:01:22,950 --> 00:01:25,740 First, let's open application.py. 29 00:01:25,740 --> 00:01:28,410 You'll notice at the very top, we have a lot of dependencies. 30 00:01:28,410 --> 00:01:30,720 We import a lot of packages and modules. 31 00:01:30,720 --> 00:01:34,590 This will allow us easier functionality as we progress through this problem. 32 00:01:34,590 --> 00:01:39,360 Feel free to check out the documentation for all of these dependencies. 33 00:01:39,360 --> 00:01:41,910 Then we ensure that responses aren't cached. 34 00:01:41,910 --> 00:01:44,820 This means that whenever we make a request from the server, 35 00:01:44,820 --> 00:01:48,870 it actually fetches from the server as opposed to going into the cache. 36 00:01:48,870 --> 00:01:51,390 So we get fresh data every time. 37 00:01:51,390 --> 00:01:55,410 Then we configure the Flask session and configure the CS50 library 38 00:01:55,410 --> 00:01:57,810 to use the SQLite database. 39 00:01:57,810 --> 00:02:01,050 Then you'll notice that we have login required 40 00:02:01,050 --> 00:02:03,780 before every definition of these pages. 41 00:02:03,780 --> 00:02:09,030 That means that the user must be logged in order to access the given route. 42 00:02:09,030 --> 00:02:12,240 More on login required later on. 43 00:02:12,240 --> 00:02:15,000 As we continue through application.py, we'll 44 00:02:15,000 --> 00:02:18,960 notice that login and logout are both implemented for us. 45 00:02:18,960 --> 00:02:22,110 So these are going to be important to understand how they work 46 00:02:22,110 --> 00:02:24,900 but also to use as a framework to understand how 47 00:02:24,900 --> 00:02:27,090 we might implement the other routes. 48 00:02:27,090 --> 00:02:31,830 Also notice how we call the apology function and the database 49 00:02:31,830 --> 00:02:33,510 execute function. 50 00:02:33,510 --> 00:02:36,360 Some other things to take note of, whenever 51 00:02:36,360 --> 00:02:41,470 we have a form submission via post, we can retrieve it as follows. 52 00:02:41,470 --> 00:02:46,260 In our HTML template within the templates folder, if we have our HTML 53 00:02:46,260 --> 00:02:51,720 and we've specify the name of our input box, then within Python, 54 00:02:51,720 --> 00:02:55,980 we can then request that via request.form.get 55 00:02:55,980 --> 00:02:59,220 and then specify the name of that input in order 56 00:02:59,220 --> 00:03:02,800 to retrieve the variable that was submitted from the form. 57 00:03:02,800 --> 00:03:06,330 Note that the execute function for the SQL database 58 00:03:06,330 --> 00:03:08,640 always will return an array. 59 00:03:08,640 --> 00:03:12,900 So that means that even though I only selected one specific cash 60 00:03:12,900 --> 00:03:16,380 value for one specific user, in order to access that value, 61 00:03:16,380 --> 00:03:21,090 I'll have to access the very first row of the array rows 62 00:03:21,090 --> 00:03:25,020 and then go to the index of cash in order to get that value. 63 00:03:25,020 --> 00:03:28,680 Feel free to take some time and read through application.py so you really 64 00:03:28,680 --> 00:03:30,240 understand how it's working. 65 00:03:30,240 --> 00:03:33,300 For now, I'm going to move on to helpers.py, 66 00:03:33,300 --> 00:03:37,750 where we'll see the implementation for apology and login required. 67 00:03:37,750 --> 00:03:42,120 We'll also see the lockup function, which is going to be useful. 68 00:03:42,120 --> 00:03:45,930 Lookup allows us to take a stock symbol and then returns 69 00:03:45,930 --> 00:03:50,760 the name, price, and symbol of that stock encapsulated within a dict. 70 00:03:50,760 --> 00:03:53,250 It does this using Yahoo Finance, which allows us 71 00:03:53,250 --> 00:03:57,240 to fetch stock information via a URL. 72 00:03:57,240 --> 00:04:01,630 Then we have usd, which nicely formats the prices for us. 73 00:04:01,630 --> 00:04:04,140 Now we can move on to the templates directory. 74 00:04:04,140 --> 00:04:08,790 There are only three files in here, apology, layout, and login, 75 00:04:08,790 --> 00:04:10,740 all HTML files. 76 00:04:10,740 --> 00:04:13,050 So take a look at these and figure out how 77 00:04:13,050 --> 00:04:17,970 they interact with their respective routes with an application.py. 78 00:04:17,970 --> 00:04:20,250 And surely, you'll be adding additional templates 79 00:04:20,250 --> 00:04:22,800 as you progress through this problem. 80 00:04:22,800 --> 00:04:26,430 Finally, we have the requirements.txt and then 81 00:04:26,430 --> 00:04:31,080 the CCS style sheet which you can feel free to edit as you see fit. 82 00:04:31,080 --> 00:04:35,070 Once you're comfy with the distribution code, and I mean really comfy, 83 00:04:35,070 --> 00:04:38,240 then we can start implementing register. 84 00:04:38,240 --> 00:04:39,358