问题
Okay, so I am trying to get prices from an API, now the prices are found in "sell_summary", and "buy_summary", I managed to return these values to my HTML but the issue is that every item from the API, has multiple 'pricePerUnit's, so there could be up to 10 pricePerUnits in one "sell_summary" for example, I only want the top one (because that's the latest updated price).
Here is my Python code:
import requests
from flask import Flask, render_template, url_for
product_names = [this array just consists of products name, removed to make thread shorter]
app = Flask(__name__)
@app.route('/')
def price():
f = requests.get(
"https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2").json()
products = [
{
"id": product["product_id"],
"sell_price": product["sell_summary"], #Here is the priceperUnit I am grabbing
"buy_price": product["buy_summary"], #Here is the priceperUnit I am grabbing
"sell_volume": product["quick_status"]["sellVolume"],
"buy_volume": product["quick_status"]["buyVolume"],
"buy_orders": product["quick_status"]["buyOrders"],
"sell_orders": product["quick_status"]["sellOrders"]
}
for product in f["products"].values()
]
return render_template("index.html", products=products)
if __name__ == "__main__":
app.run(debug=True)
And here is my HTML code:
<table
style="font-family: sans-serif; border-collapse: collapse; width: 50%;"
id="result"
>
<thead>
<tr>
<th
onclick="sortTable(0)"
style="border: 1px solid #dddddd; text-align: left; padding: 8px;"
>
Product ID
</th>
<th
onclick="sortTable(1)"
style="border: 1px solid #dddddd; text-align: left; padding: 8px;"
>
Buy Price
</th>
<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
Sell Price
</th>
<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
Buy Volume
</th>
<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
Sell Volume
</th>
<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
Buy Orders
</th>
<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
Sell Orders
</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr class="item">
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{{ product.id|replace("_", ' ')|lower()|title() }}
</td>
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{% for buy in product.buy_price %} {{ buy.pricePerUnit }} {% endfor %}
<!-- Here is where buy price get displayed-->
</td>
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{% for sell in product.sell_price %} {{ sell.pricePerUnit }} {% endfor
%}
<!-- Here is where sell price get displayed-->
</td>
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{{ product.buy_volume }}
</td>
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{{ product.sell_volume }}
</td>
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{{ product.buy_orders }}
</td>
<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">
{{ product.sell_orders }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
This is how it looks on my website: Website
Now the circled numbers are the numbers I wan't to display on the website, the other numbers in that table I don't want to show!
来源:https://stackoverflow.com/questions/61955050/return-values-from-an-api-using-flask