SPEAKER: Let's make some money in CS50 Finance.
In this problem, we will create a website
that allows users to sign up, register, and log in
in order to buy and sell stocks and shares that they're interested in.
Now, there's a lot of distribution code in this problem.
So before we go into our to-dos, let's open the distribution code
and walk ourselves through.
Within these folders you'll find Python files, HTML files, CSS, text files,
even a .db file.
So we need to understand how all of these files and functions
interact with one another, and how do we use all of the functions provided?
Let's open up application.py first.
You'll see that, at the top, we import a lot of packages and modules.
This will allow us easier functionality as we progress through this problem.
Now, whenever a website makes a request from the server,
we are going to ensure that it actually fetches from the server as opposed
to going into the cache so that we get fresh data every time.
See if you can find where this is located.
Then we'll configure the Flask section and the CS50 library
in order to use the SQLite database.
Now, we only want registered and logged in users
to access certain pages of our web site, so that's
where login_required will come in handy.
Go back to the relevant sections in application.py
and see if you understand what's going on.
Within application.py you'll find that login and logout are implemented,
as well as apology and db.execute.
Now even though login and logout are already implemented for you,
it's going to be important to understand how
they work because it's going to be quite useful to use these as frameworks
as we complete the rest of the problem.
It's also important to understand how apology and db.execute are
used because we're certainly going to be using these functions.
Another thing that we'll be dealing with a lot in this problem
is form submissions via HTML templates.
How do we retrieve that information?
Well, within the templates folder, we'll have our HTML template file.
So within the HTML, if we specified the name of our input box,
then within Python we can request that via request.form.get
and then entering the name of that input in order
to retrieve that as a variable.
Now let's look at how we might execute a query to our SQL database.
Here, I'm selecting the column cash from the user's table
where the given condition is that that ID of the user
is the ID of the user that's currently logged in.
Now db.execute will always return a list.
So even though we know that there's only one row that satisfies this given
query, it will still return us a list.
So we'll have to access the first element of that list
to get to the relevant row, and then we can access the "cash" index in order
to get that value.
Once we're a little more comfortable with application.py,
let's move on to helpers.
Within helpers.py, we'll find apology that
allows us to essentially render error messages to our user, login_required,
which we've talked about already, and lookup.
lookup is a function that will use an API in order
to return us a dict with three keys, the name of the share that we're
inquiring about, the current price as well as its symbol.
Then, finally, we have usd, which allows us to format prices nicely.
Now let's move on to templates.
The templates folder currently has three files--
apology, layout, and login.
So we need to figure out how these templates interact
with their respective routes within applications.py.
Now, we will be adding additional templates to this folder.
So take a look at these three HTML files,
and figure out how they interact with their respective routes
within application.py.
There is even more distribution code to look into.
So take a look at requirements.txt and the styles.css file, which, by the way,
you're free to edit yourself.
Once you're comfortable with all of this distribution code, let's
start making our website.