Cole Solo
Cole Solo

Reputation: 21

CS50 finance lookup returning 200 none on valid input

I am stuck trying to get my flask input function to work. I am trying to complete the CS50 2020 finance problem. Documentation for the problem can be found here. The code block bellow is the error message I get in the console when I try to use the index function is run. The api key I am using is valid and when inserting the whole url for the get request I get the expected information. Code may be somewhat messy, I have not cleaned it up and optimized it yet, because I can't get it to work.

DEBUG: SELECT quantity FROM oStocks WHERE userID = 5 AND stock = 'AAPL'
DEBUG: Starting new HTTPS connection (1): cloud.iexapis.com:443
DEBUG: https://cloud.iexapis.com:443 "GET /stable/stock/GOOGL-AF/quote?token=<MY_API_KEY> HTTP/1.1" 200 None

I have tried all I could think of to try and fix this error. changed and unchanged nearly everything about the lookup function. I can't seem to find what is wrong. Can someone please point me in the right direction. Thank you

flask index:

@app.route("/")
@login_required
def index():
    userid = session["user_id"]
    owned = db.execute("SELECT stock FROM oStocks WHERE userID = :userid", userid=userid)
    OwnedStocks = []
    for row in owned:
        OwnedStocks.append(row["stock"])

    stockInfo = {}
    for item in OwnedStocks:
        itemInfo = lookup(item)
        tmpQuantity = db.execute("SELECT quantity FROM oStocks WHERE userID = :userid AND stock = :stock", userid=userid, stock=item)
        quantity = tmpQuantity[0]["quantity"]
        sharePrice = itemInfo["price"]
        name = itemInfo["name"]
        value = quantity * sharePrice

        stockInfo[item] = {}
        stockInfo[item]['symbol'] = item
        stockInfo[item]['name'] = name
        stockInfo[item]['shares'] = quantity
        stockInfo[item]['price'] = sharePrice
        stockInfo[item]['value'] = value
    return render_template("portfolio.html", stocks=stockInfo)

Lookup Function:

def lookup(symbol):
    """Look up quote for symbol."""

    # Contact API
    try:
        api_key = os.environ.get("API_KEY")
        response = requests.get(f"https://cloud.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}")
        response.raise_for_status()
    except requests.RequestException:
        return None

    # Parse response
    try:
        quote = response.json()
        return {
            "name": quote["companyName"],
            "price": float(quote["latestPrice"]),
            "symbol": quote["symbol"]
        }
    except (KeyError, TypeError, ValueError):
        return None

Portfolio html:

{% extends "layout.html" %}

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

{% block main %}

<div>
    <table class="table table-hover">
      <thead>
        <tr class="font-weight-bold">
          <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>
      <tbody>
        {% for stock in stocks %}
          <tr>
            <th scope="row">{{ stock['symbol'] }}</th>
            <td>{{ stock['name'] }}</td>
            <td>{{ stock['shares'] }}</td>
            <td>{{ stock['price'] }}</td>
            <td>{{ stock['total'] }}</td>
          </tr>
        {% endfor %}

      </tbody>
    </table>

</div>
{% endblock %}

Upvotes: 1

Views: 717

Answers (1)

Cole Solo
Cole Solo

Reputation: 21

I have found the solution, now finding it I feel dumb having looked in all the wrong places so long. My mistake was in iterating and calling values from a dictionary passed into a flask template. After much search I ended up realizing and fixing my problem while going through camposha.

My original method was:

{% for stock in stocks %}
    <tr>
        <th scope="row">{{ stock['symbol'] }}</th>
        <td>{{ stock['name'] }}</td>
        <td>{{ stock['shares'] }}</td>
        <td>{{ stock['price'] }}</td>
        <td>{{ stock['total'] }}</td>
    </tr>
{% endfor %}

However the proper way to achieve my goal was:

{% for stock, value in stocks.items() %}
  <tr>
    <th scope="row">{{ value['symbol']['stock'] }}</th>
    <td>{{ value['name'] }}</td>
    <td>{{ value['shares'] }}</td>
    <td>{{ value['price'] }}</td>
    <td>{{ value['value'] }}</td>
  </tr>
{% endfor %}

I aslo made some adjustments to my index:

def index():
    userid = session["user_id"]
    owned = db.execute("SELECT stock, quantity FROM oStocks WHERE userID = :userid", userid=userid)

    stockInfo = {}
    for item in owned:
        print(item)
        itemInfo = lookup(item['stock'])
        quantity = item['quantity']
        name = itemInfo['name']
        sharePrice = float(itemInfo['price'])
        value = quantity * sharePrice

        stockInfo[item['stock']] = {}
        stockInfo[item['stock']]['symbol'] = item
        stockInfo[item['stock']]['name'] = name
        stockInfo[item['stock']]['shares'] = quantity
        stockInfo[item['stock']]['price'] = sharePrice
        stockInfo[item['stock']]['value'] = value
    return render_template("portfolio.html", stocks=stockInfo)

Upvotes: 1

Related Questions