0

I'm trying to render my lookup values inside of my index function implementation, but I cannot get these values to render in my index.html page. I added share = lookup(record['share_symbol']) to grab the current lookup of my purchased stock symbols from IEX, in order to get access to the current price. I can get values that I passed inside of the for loop of my index function, but passing the lookup inside of the same for loop and sending that to the render_template('index.html') is not giving me any results for current_price.

Inside of my index function, if I print the share = lookup(record['share_symbol']) I do get the dictionaries for each of the current stocks in the transactions table that I am grabbing in my db.execute. You'll see below a printout of what I'm seeing in the console:

DEBUG:cs50:SELECT share_symbol, share_name, share_qty FROM transactions WHERE user_id=12
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/amat/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1" 200 None
{'name': 'Applied Materials, Inc.', 'price': 65.085, 'symbol': 'AMAT'}
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/ngvt/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  HTTP/1.1" 200 None
{'name': 'Ingevity Corp.', 'price': 58.22, 'symbol': 'NGVT'}
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/ngvt/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  HTTP/1.1" 200 None
{'name': 'Ingevity Corp.', 'price': 58.22, 'symbol': 'NGVT'}
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): cloud-sse.iexapis.com:443
DEBUG:urllib3.connectionpool:https://cloud-sse.iexapis.com:443 "GET /stable/stock/ngvt/quote?token=pk_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  HTTP/1.1" 200 None
{'name': 'Ingevity Corp.', 'price': 58.22, 'symbol': 'NGVT'}

But when I pass this to my render_template('index.html') nothing shows up. I even checked the dev console in the browser and there is no value for the td element I'm trying to render with <td>{{ record.current_price }}</td>

dev console

My index function:

@app.route("/", methods=["GET"])
@login_required
def index():
    """Show portfolio of stocks"""

    # print(session["user_id"])
    if request.method == "GET":

        portfolio = db.execute("SELECT share_symbol, share_name, share_qty FROM transactions WHERE user_id=:user_id",
        user_id=session["user_id"])

        for record in portfolio:
            share_symbol = record['share_symbol']
            share_name = record['share_name']
            share_qty = record['share_qty']
            share = lookup(record['share_symbol'])
            # name = share['name']
            current_price = share['price']
            # current_price = share.get('price')
            # symbol = share['symbol']
            print(share)
            # print(f"Current Price: {current_price}")

        return render_template("index.html", portfolio=portfolio, share=share, current_price=current_price)
        # {'name': 'Ingevity Corp.', 'price': 58.48, 'symbol': 'NGVT'}

My index.html page

{% extends "layout.html" %}{% block title %} Purchased {% endblock %} {% block main %}
<table class="table" style="width:100%">

    <thead>
        <tr>
            <th scope="col">Symbol</th>
            <th scope="col">Name</th>
            <th scope="col">Shares</th>
            <th scope="col">Price</th>
            <th scope="col">Total</th>
        </tr>
    </thead>
    {% for record in portfolio %}
    <tbody>
        <tr>
            <td>{{ record.share_symbol | upper }}</td>
            <td>{{ record.share_name }}</td>
            <td>{{ record.share_qty }}</td>
            <td>{{ record.current_price }}</td>
        </tr>
    </tbody>
    {% endfor %}
    <tr>
        <th scope="col">CASH</th>
        <th scope="col"></th>
        <th scope="col"></th>
        <th scope="col"></th>
    </tr>
    </tbody>
</table>

{% endblock %}

finance app index page

What am I missing or doing wrong that I can't iterate through the lookup dictionary?

sqlite db transactions table

1

Each dict in portfolio contains 3 keys as returned from the execute. There is no key current_price, so the html does not render anything for that column. If the key gets created in the for loop in index, then it will be available to the html. Something like record['current_price'] = share['price'].

| improve this answer | |

You must log in to answer this question.

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