1 00:00:00,000 --> 00:00:00,500 2 00:00:00,500 --> 00:00:02,850 SPEAKER: Let's make some money in CS50 Finance. 3 00:00:02,850 --> 00:00:04,980 In this problem, we will create a website 4 00:00:04,980 --> 00:00:07,995 that allows users to sign up, register, and log in 5 00:00:07,995 --> 00:00:11,520 in order to buy and sell stocks and shares that they're interested in. 6 00:00:11,520 --> 00:00:14,070 Now, there's a lot of distribution code in this problem. 7 00:00:14,070 --> 00:00:18,240 So before we go into our to-dos, let's open the distribution code 8 00:00:18,240 --> 00:00:20,790 and walk ourselves through. 9 00:00:20,790 --> 00:00:26,850 Within these folders you'll find Python files, HTML files, CSS, text files, 10 00:00:26,850 --> 00:00:28,840 even a .db file. 11 00:00:28,840 --> 00:00:31,890 So we need to understand how all of these files and functions 12 00:00:31,890 --> 00:00:36,610 interact with one another, and how do we use all of the functions provided? 13 00:00:36,610 --> 00:00:39,450 Let's open up application.py first. 14 00:00:39,450 --> 00:00:43,290 You'll see that, at the top, we import a lot of packages and modules. 15 00:00:43,290 --> 00:00:47,190 This will allow us easier functionality as we progress through this problem. 16 00:00:47,190 --> 00:00:50,210 Now, whenever a website makes a request from the server, 17 00:00:50,210 --> 00:00:53,610 we are going to ensure that it actually fetches from the server as opposed 18 00:00:53,610 --> 00:00:57,240 to going into the cache so that we get fresh data every time. 19 00:00:57,240 --> 00:00:59,730 See if you can find where this is located. 20 00:00:59,730 --> 00:01:03,660 Then we'll configure the Flask section and the CS50 library 21 00:01:03,660 --> 00:01:06,390 in order to use the SQLite database. 22 00:01:06,390 --> 00:01:09,090 Now, we only want registered and logged in users 23 00:01:09,090 --> 00:01:11,700 to access certain pages of our web site, so that's 24 00:01:11,700 --> 00:01:13,740 where login_required will come in handy. 25 00:01:13,740 --> 00:01:16,920 Go back to the relevant sections in application.py 26 00:01:16,920 --> 00:01:20,040 and see if you understand what's going on. 27 00:01:20,040 --> 00:01:24,540 Within application.py you'll find that login and logout are implemented, 28 00:01:24,540 --> 00:01:27,490 as well as apology and db.execute. 29 00:01:27,490 --> 00:01:30,597 Now even though login and logout are already implemented for you, 30 00:01:30,597 --> 00:01:32,430 it's going to be important to understand how 31 00:01:32,430 --> 00:01:37,020 they work because it's going to be quite useful to use these as frameworks 32 00:01:37,020 --> 00:01:39,060 as we complete the rest of the problem. 33 00:01:39,060 --> 00:01:42,810 It's also important to understand how apology and db.execute are 34 00:01:42,810 --> 00:01:46,527 used because we're certainly going to be using these functions. 35 00:01:46,527 --> 00:01:49,110 Another thing that we'll be dealing with a lot in this problem 36 00:01:49,110 --> 00:01:52,440 is form submissions via HTML templates. 37 00:01:52,440 --> 00:01:54,690 How do we retrieve that information? 38 00:01:54,690 --> 00:01:58,650 Well, within the templates folder, we'll have our HTML template file. 39 00:01:58,650 --> 00:02:03,090 So within the HTML, if we specified the name of our input box, 40 00:02:03,090 --> 00:02:08,230 then within Python we can request that via request.form.get 41 00:02:08,230 --> 00:02:11,580 and then entering the name of that input in order 42 00:02:11,580 --> 00:02:13,660 to retrieve that as a variable. 43 00:02:13,660 --> 00:02:17,470 Now let's look at how we might execute a query to our SQL database. 44 00:02:17,470 --> 00:02:23,130 Here, I'm selecting the column cash from the user's table 45 00:02:23,130 --> 00:02:27,120 where the given condition is that that ID of the user 46 00:02:27,120 --> 00:02:30,210 is the ID of the user that's currently logged in. 47 00:02:30,210 --> 00:02:32,860 Now db.execute will always return a list. 48 00:02:32,860 --> 00:02:36,480 So even though we know that there's only one row that satisfies this given 49 00:02:36,480 --> 00:02:38,860 query, it will still return us a list. 50 00:02:38,860 --> 00:02:41,640 So we'll have to access the first element of that list 51 00:02:41,640 --> 00:02:47,190 to get to the relevant row, and then we can access the "cash" index in order 52 00:02:47,190 --> 00:02:48,300 to get that value. 53 00:02:48,300 --> 00:02:51,570 Once we're a little more comfortable with application.py, 54 00:02:51,570 --> 00:02:53,100 let's move on to helpers. 55 00:02:53,100 --> 00:02:55,620 Within helpers.py, we'll find apology that 56 00:02:55,620 --> 00:03:00,750 allows us to essentially render error messages to our user, login_required, 57 00:03:00,750 --> 00:03:03,740 which we've talked about already, and lookup. 58 00:03:03,740 --> 00:03:06,810 lookup is a function that will use an API in order 59 00:03:06,810 --> 00:03:11,040 to return us a dict with three keys, the name of the share that we're 60 00:03:11,040 --> 00:03:15,270 inquiring about, the current price as well as its symbol. 61 00:03:15,270 --> 00:03:19,860 Then, finally, we have usd, which allows us to format prices nicely. 62 00:03:19,860 --> 00:03:21,810 Now let's move on to templates. 63 00:03:21,810 --> 00:03:24,840 The templates folder currently has three files-- 64 00:03:24,840 --> 00:03:27,120 apology, layout, and login. 65 00:03:27,120 --> 00:03:30,060 So we need to figure out how these templates interact 66 00:03:30,060 --> 00:03:33,330 with their respective routes within applications.py. 67 00:03:33,330 --> 00:03:36,670 Now, we will be adding additional templates to this folder. 68 00:03:36,670 --> 00:03:38,970 So take a look at these three HTML files, 69 00:03:38,970 --> 00:03:42,240 and figure out how they interact with their respective routes 70 00:03:42,240 --> 00:03:44,450 within application.py. 71 00:03:44,450 --> 00:03:47,050 There is even more distribution code to look into. 72 00:03:47,050 --> 00:03:52,500 So take a look at requirements.txt and the styles.css file, which, by the way, 73 00:03:52,500 --> 00:03:53,880 you're free to edit yourself. 74 00:03:53,880 --> 00:03:57,180 Once you're comfortable with all of this distribution code, let's 75 00:03:57,180 --> 00:03:59,450 start making our website. 76 00:03:59,450 --> 00:04:00,488