I'm working on the buy() method. I've created an input form allowing the user to enter the share (what we call "stocks" in the UK) symbol and the number of shares the user wants to buy.
I then want to go a little beyond the spec and add an intermediate step that:
- Looks up the shares (what we call "stocks" in the UK)
- Displays an HTML table showing the symbol, company name, share price, the number of shares the user wants to buy, the total cost (share price * number) and the user's current cash balance (from finance.db)
- Have a button saying "Please Confirm you Want to Buy" [or something like that]
I've created the HTML table and can pass in the symbol, company name and current share price - via the lookup() method. But I don't know how to pass in the number of shares and total cost variables into the same table - though I know they are calculated using the #'d out print() commands you'll see below. [NB, I haven't started on getting the current cash balance from finance.db yet].
My screens look like:
My python and html code are below. Many thanks in advance for your help.
buy() - in application.py
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock."""
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure share symbol was submitted
if not request.form.get("symbol"):
return apology("You must submit a share symbol")
# ensure number of shares to buy was submitted
elif not request.form.get("num_shares_buy"):
return apology ("You must submit a number of shares to buy")
# retrieve the number of shares to buy
num_shares_buy = int(request.form.get("num_shares_buy"))
# ensure number of shares to buy is a positive integer
if num_shares_buy < 1:
return apology ("You must submit a positive whole number of shares to buy")
#print("XXXXXXX YOU ARE HERE XXXXXXXX number of shares to buy is {}".format(num_shares_buy))
# lookup symbol using lookup() method
buy = lookup(request.form.get("symbol"))
#print("XXXXXXXX YOU ARE HERE XXXXXXXXXX price is {}".format(buy["price"]))
# calculate cost of shares to buy (number x price)
cost = num_shares_buy * buy["price"]
#print("XXXXXXX YOU ARE HERE XXXXXXX Total Cost is {}".format(cost))
# ensure symbol entered is valid (i.e. returns data when lookup() queries yahoo finance)
if buy == None:
return apology("You must submit a valid share symbol")
# print the details of the desired share purchase
else:
return render_template("buying.html", placeholder = buy)
# else if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("buy.html")
buying.html
{% extends "layout.html" %}
{% block title %}
Share Purchase In Progress
{% endblock %}
{% block main %}
<table>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price ($US)</th>
<th>Number of Shares to Buy</th>
<th>Cost ($US)</th>
<th>Cash Available ($US)</th>
</tr>
<tr>
<td>{{placeholder.symbol}}</td>
<td>{{placeholder.name}}</td>
<td>{{placeholder.price}}</td>
<td>{{num_shares_buy}}</td>
<td>{{cost}}</td>
<td>CASH - TO COME</td>
</tr>
</table>
{% endblock %}