0

I'm currently struggling with index(). In buy() I created the custom table with, one might say, too many columns. Among others are symbol, name, shares, price, total.

So far I was able to plug some of the necessary inputs in index.html. except for the current price per share. It somehow takes only one current price and fills the table with it.

@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
stocks = db.execute("SELECT symbol, name, shares, total FROM transactions WHERE user_id = :user_id GROUP BY symbol",
            user_id=session['user_id'])

for stock in stocks:
    prices = lookup(stock['symbol'])


return render_template("index.html",stocks=stocks, prices=prices['price'])

I've tried different variations, one where I create a variable with the current price, putting an indent on returning the template and trying to put the current prices into a new list/dict and somehow adding it to the stocks. This seemed a lot more complicated than it should be I'm sure. Or maybe I've been looking at the screen for too long and am going blank. Thanks for your help!

{% extends "layout.html" %}

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

{% block main %}
    <table class = "table table-striped">
     <thead>
        <tr>
            <th>Symbol</th>
            <th>Name</th>
            <th>Shares</th>
            <th>Price</th>
            <th>Total</th>
        </tr>
     </thead>
    {% for items in stocks %}
    <tbody>
        <tr>
            <td>{{ items['symbol'] }}</td>
            <td>{{ items['name'] }}</td>
            <td>{{ items['shares'] }}</td>
            <td>{{ prices }}</td>
            <td>{{ items['total'] |usd }}</td>
        </tr>
    </tbody>
    {% endfor %}
    <tr>
</table>
{% endblock %}

Finance Website

1

This prices = lookup(stock['symbol']) holds the values from the last symbol in the stocks list. This "put the current prices into a new list/dict and somehow adding it to the stocks." is on the right track, but there is a simpler approach. In the for loop, declare a "price" key in stock dict and assign a value to it, presumably the price returned from lookup.

2
  • is this in the for loop? stock is a dict; it cannot be used as a subscript. To add a key to a dict, simply declare it. ie (in the loop) stock["price"] = something. Sep 4 '20 at 18:48
  • I just figured that out before your answer to my former comment popped up that's why I ended up deleting it. I solved it by using the update() function and plugging in the new key:value pair. Your option's a lot cleaner though. Thanks for your help!
    – abbanator
    Sep 4 '20 at 18:54

You must log in to answer this question.

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