0

I am working on getting the proper information to display on the index page. I have one piece left, and it is displaying the current value of the stock. When I look at my output for my 'user' and 'rows' queries, they return correctly, and the symbol and shares render correctly on index.html. Where it all goes sideways is when I try to find the current price of the stock. It will return the current price of the last record in the database as the current price for all the stock owned. For example, right now the last stock I "purchased" was 2 shares of MSFT for 569.82. However, this is what is displayed:

![output on index.html

As you can see, it shows that as the current price for all my purchased stock.

Below is my current application.py code:

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""

    user = db.execute("SELECT id, cash FROM users WHERE id=:id", id = session['user_id'])
    rows = db.execute("SELECT * FROM purchases WHERE owner_id = ?", user[0]["id"])

    current_balance = user[0]['cash']

    portfolio = {}

    for row in rows:
        quote = lookup(row["symbol"]) # <-- this row only returning quote for the last purchase
        portfolio["current_price"] = quote["price"]
        portfolio["holding_total"] = portfolio["current_price"] * row["shares"]

    grand_total = portfolio["holding_total"] + current_balance

    return render_template("index.html", portfolio=portfolio, rows=rows, current_balance=current_balance, grand_total=grand_total)

My sqlite schema:

CREATE TABLE users (id INTEGER, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00, PRIMARY KEY(id));
CREATE UNIQUE INDEX username ON users (username);
CREATE TABLE purchases (id INTEGER, owner_id INTEGER, symbol TEXT, shares INTEGER, purchase_price NUMERIC, date TEXT, PRIMARY KEY (id), FOREIGN KEY (owner_id) REFERENCES users(id));

My index.html code:

{% extends "layout.html" %}

{% block title %}
    Portfolio
{% endblock %}

{% block main %}

<table>
        <tbody>
            <thead>
                <tr>
                    <th>Symbol</th>
                    <th>Shares Owned</th>
                    <th>Current Price</th>
                    <th>Holding Total</th>
                </tr>
            </thead>
            {% for row in rows %}
                <tr>
                    <td>{{ row.symbol }}</td>
                    <td>{{ row.shares }}</td>
                    <td>{{ portfolio.current_price | usd }}</td>
                    <td>{{ portfolio.holding_total | usd }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
Current Cash Balance: {{ current_balance | usd }}
Grand Total: {{ grand_total | usd }}

{% endblock %}

Otherwise, all is behaving as it should. I don't understand why lookup() is behaving this way. Where am I going wrong? Any help is greatly appreciated!!!

Update: I have tried various ways to get the lookup function to use the correct symbol. My most recent was trying to store all the symbols returned from the rows query to a list, and then use that list in my for loop. The list contained the correct information, but lookup still just uses the last symbol in the list. Please, I would really appreciate any advice.

0
1

The problem isn't lookup. The problem is portfolio. There are exactly two keys in the portfolio dictionary, "current_price" and "holding_total". They are set to the values in each row in sequence, and end up with the values from the last row.

Instead of adding a new dictionary, consider adding new keys for each row in rows.

1
  • THANK YOU SO MUCH!!!!!
    – Rachel D
    Aug 1 at 23:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .