Reputation:
I have a website that shows the prices of items in a video game.
Currently, I have an "auto-refresh" script that refreshes the page every 5 seconds, but it is a bit annoying as every time you search for a product, it removes your search because the page refreshes.
I would like to update the numbers in my table without refreshing the page for the user. I read something about 'updating the DOM', in javascript but didn't get it.
Here is the link to my website: http://xeltool.com/
And here is my python code:
@app.route('/bprices', methods=['GET'])
def bPrices():
f = requests.get(
'https://api.hypixel.net/skyblock/bazaar?key=[cannot show]').json()
products = [
{
"id": product["product_id"],
"sell_price": product["sell_summary"][:1],
"buy_price": product["buy_summary"][:1],
"sell_volume": product["quick_status"]["sellVolume"],
"buy_volume": product["quick_status"]["buyVolume"],
}
for product in f["products"].values()
]
return render_template("bprices.html", products=products)
And here is my HTML code:
<div class="container">
<div class="search_div">
<input
type="text"
onkeyup="myFunction()"
id="myInput"
title="Type in a product"
class="search-box"
placeholder="Search for a product..."
/>
<button class="search-btn"><i class="fas fa-search"></i></button>
</div>
<table
id="myTable"
class="table table-striped table-bordered table-sm table-dark sortable"
cellspacing="0"
>
<thead>
<tr>
<th aria-label="Product Name" data-balloon-pos="up">Product</th>
<th aria-label="Product's buy price" data-balloon-pos="up">
Buy Price
</th>
<th aria-label="Product's sell price" data-balloon-pos="up">
Sell Price
</th>
<th aria-label="Product's buy volume" data-balloon-pos="up">
Buy Volume
</th>
<th aria-label="Product's sell volume" data-balloon-pos="up">
Sell Volume
</th>
<th>
Margin
</th>
</tr>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.id|replace("_", ' ')|lower()|title() }}</td>
{% for buy in product.buy_price %}
<td>{{ buy.pricePerUnit }}</td>
{% for sell in product.sell_price %}
<td>{{ sell.pricePerUnit }}</td>
<td>{{ product.buy_volume| numberFormat }}</td>
<td>{{ product.sell_volume| numberFormat }}</td>
{% set margin = buy.pricePerUnit - sell.pricePerUnit%} {% set marginPer
= margin/buy.pricePerUnit * 100%}
<td
aria-label="{{ marginPer|round(1, 'floor') }} % "
data-balloon-pos="right"
>
{{ margin|round(1, 'floor')}}
</td>
{% endfor %}{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
If you NEED the API to test this out, I can provide a link to it :)
Upvotes: 9
Views: 30514
Reputation: 53
Use AJAX to ask for new prices. The API will return the values and then in the JS code just change the previous price to the new price.
AJAX will definitely help.
Have a look at the simple tutorial here: https://www.w3schools.com/xml/ajax_intro.asp.
Upvotes: 3
Reputation: 23815
You have 3 options:
SSE - https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Websocket - https://developer.mozilla.org/en-US/docs/Glossary/WebSockets
I think the best option in your case is SSE since the server knows that the price was changed so it can push it to the clients.
Upvotes: 13