Welcome to Part 2 of my tutorial on the implementation of Finance from CS50’s Problem Set 7. During last week’s post, I covered the implementation of register. In this week’s post, I will cover how to implement the quote function, where a user can get a stock quote before purchasing or selling some stock. I will start out by going through quote.html and its elements. You will notice that this html file is very similar to the register.html file from last week.

In this html file below, we are still using the template language Jinja to extend from our layout.html file. We are also using similar form control elements that we used last week. In this example the form is simply asking for the stock symbol from the user. In the following Python code snippet, we will take over from when the user clicks the submit button.

In our application.py file, we will implement the quote function that will run once the submit button is clicked. The first line of code ensures that the user has arrived via a post method (i.e. that they have submitted the form and not arrived from typing into the URL. Next, we want to pass the symbol that the user has typed in into the lookup function. The lookup function will return a JSON object of the stock that includes the stock price and the symbol. If the user has typed in an invalid stock symbol, the function will return None. Thus, if stock == None, then we will throw an error to the user. Finally, if the user has typed in a valid stock symbol, we want to show that information to the user. In this case we will render the template for quoted.html and passing to it the stock object.

In this next part, we will use Jinja to display the information from the stock object to the user. In this example, I have used simple paragraph tags, however, you could put the information into a table format as well. You will see how we can display information to the user in a table in later weeks. In this simple html file, we first display the stock symbol by using the stock.symbol notation within our Jinja brackets. The next paragraph will show the stock price. Here, we access the price by using the same dot notation (stock.price), however, you will notice a little addition at the end. In Jinja, we use the pipe symbol to modify our variable with a filter.  The filter that we are using in this case, is the usd function from helpers.py that will format the stock price into U.S. Dollars.

And that is all there is to the implementation of quote/quoted. I hope that this tutorial has helped you toward your journey to becoming a programmer. If you have any questions, please leave them in the comment section below. If this post was useful to you, share the love and post on your favorite social media platform by using the buttons below. As always….Happy Programming!!

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s