0

In index.html, should I only be passing in one variable with all my information? I currently am passing three. They each contain their own iterable items which I need access to simultaneously. I'm having trouble understanding how to get at them both simultaneously in Jinja.

I have # of shares owned by user in one variable (portfolio). I have current price of a share of that stock in another variable (quotes). I want to get: current value of shares, which means I would need to iterate both variables simultaneously, having access to the iterated # of shares and iterated current price.

The issue is if I use a nested forloop, then I get the current price for one stock applied to the # of shares for each stock, i.e., 4 stocks, 16 "current values" (4 per stock).

Can I get access to both items simultaneously and iterate both simultaneously? If not, I'm not sure how to proceed to calculate Current Value of user's stocks.

Thanks for any help you can offer.

Here's my Python code

@app.route("/")
@login_required
def index():

    # retrieve user info
    user = db.execute("SELECT username, cash FROM users WHERE id=:id", id=session["user_id"])
    user[0]["cash"] = usd(float(user[0]["cash"]))

    # this will include symbols and # of shares currently in user's portfolio
    portfolio = db.execute("SELECT symbol, shares FROM portfolio WHERE id=:id", id=session["user_id"])

    # quotes will include the results of the lookup function for each stock in user's portfolio
    quotes = []

    for line in portfolio:
        q = quotes.append(lookup(str(line["symbol"])))

    return render_template("index.html", user=user, portfolio=portfolio, quotes=quotes)

Here is my Jinja/HTML:

{% block main %}
    <table>
        <tr>
            <td>{{ user[0]["username"] }}</td>
        </tr>
        <tr>
            <td>Cash on hand: {{ user[0]["cash"] }}</td>
        </tr>
        {% for portfoli in portfolio %}
            <tr>
                <td>SYMBOL: {{ portfoli.symbol }}</td>
                <td>SHARES: {{ portfoli.shares }}</td>
            </tr>
        {% endfor %}
    </table>
    <table>
        Current Price of holdings
        {% for quote in quotes %}
            <tr>            
                <td>Sym: {{ quote.symbol }}</td>
                <td>Current price: {{ quote.price }}</td>
                <td>Current value: <b>>>>>>How can I get this: quote.price (current price) * portfoli.shares (# of shares user owns) <<<<</b></td>
            </tr>
        {% endfor %}
    </table>
{% endblock %}

Here's what it looks like currently (I plan to work on aesthetics after figuring out the above question)

a

2

Instead of working with two arrays (portfolio and quote), consider adding a key/value pair to the portfolio array for the value of the holding. In index, where you iterate over portfolio you could try something like: lookup the stock, then add a key to line with the extended price: line["value"] = price (from lookup) * shares (from line). Everything you need is in portfolio.

1
  • Thanks a ton. Had been moving in that direction but couldn't get the syntax down. This works now. – Scott Mar 19 '17 at 19:35

You must log in to answer this question.

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