1

I did make a question about this earlier today, but a few hours ago I realized that there is a new API for what I am trying to make. Now the problem is that I need to get every product name, sell price and buy price, and a few more stuff into my website. I have gotten this far so far:

import requests
from flask import Flask, render_template

full_list = list()

app = Flask(__name__)

f = requests.get(
    "https://api.hypixel.net/skyblock/bazaar?key=[key is supposed to be secret]").json()

for x in product:
    buyPrice = f["products"][x]["buy_summary"][0]["pricePerUnit"]

    @app.route('/')
    def price():
        return render_template("index.html", product=product, buyprice=buyPrice)

    if __name__ == "__main__":
        app.run(debug=True)

The product API looks a bit like this, I can't post it all because it's very big:

{
    "products": {
        "product_id": "BROWN_MUSHROOM",
        "sell_summary": [
            "amount": 3865,
            "pricerPerUnit": 14.8,
            "orders": 2
        ],
        "buy_summary": [
            "amount": 704,
            "pricerPerUnit": 15.8,
            "orders": 1
        ],
        "quick_status": {
            "productId": "BROWN_MUSHROOM",
            "sellPrice": 14.527416975007378,
            "sellVolume": 915286,
            "sellMovingWeek": 23745501,
            "sellOrders": 40,
            "buyPrice": 15.898423574723452,
            "buyVolume": 673646,
            "buyMovingWeek": 8011243,
            "buyOrders": 54
    }
}

Now what I want is "product_id", which could either be grabbed from the beginning or from the "quick_status", I also want pricePerUnit, Amount and Orders from buy/sell_summary.

How do I do this? I have tried to store all values in a separate array named "price" and I used "price.append(buyPrice)" to add, but it only added one product price, I would like to have every product price.

It should end up being something like:

  1. PRODUCT_ID
  2. BUY PRICE: XXX
  3. SELL PRICE: XXX
  4. BUY ORDERS: X WITH AMOUNT OF X
  5. BUY ORDERS: X WITH AMOUNT OF X
  6. BUY VOLUME: XXX
  7. SELL VOLUME: XXX

Of course I don't need the code for everything, just need a little help with how I extract these values from the API, and get it into my HTML code.

Currently my HTML looks like this:

{% for item in product %}
    <h1>{{ item }}</h1>
    {% endfor %}
7
  • Your render method is inside for loop? Why? Commented May 20, 2020 at 16:11
  • and for x in product: what is product here? I don't see it being initialized. Commented May 20, 2020 at 16:27
  • Product was an array having every single product name, now that array is not needed as the product names are now in the new API instead, labeled "product_id", I am new to flask and this is my first project :) What I need is to reach well, product_id, priceperunit, order and amount on "sell/buy_summary", and the other stuff I wrote. So I want it to print first BROWN_MUSHROOM, then the information. Then its gonna take the next product, take the information, and so on. Commented May 20, 2020 at 16:42
  • product API looks a bit like this ... Can you post a link to a full sample of the output (pastebin/gist). Commented May 20, 2020 at 17:32
  • I couldn't upload it to Pastebin, as I needed "PRO" for it because it exceeded the limit. But I uploaded it to a formatted instead, link here: jsonblob.com/b136acab-9ac0-11ea-add9-a360c1d2e6bd Commented May 20, 2020 at 17:38

1 Answer 1

1

I am new to flask and this is my first project :)

It looks like this information should be displayed in a table. You could also use the javascript library datatables to quickly add things like pagination and sorting to the table.

I have answered two questions you may wish to read, the first on how to process data like this and another on keeping hard-coded table headers out of the template.

I came up with the repo search-generic-tables (linked in the first of those answers) which implements this functionality. It is also compatible with your data, with some minimal processing on your API's JSON response.

For your data it looks like everything you want to display is in the quick_status object for each product.

So considering you have f: the JSON, converted to a dictionary thanks to requests, you could do something like this:

original_items = [] # Create an empty list
for _, data in f['products'].items():
     original_items.append(data['quick_status'])

out is now a list, where each item is the quick_status JSON object as a python dictionary:

>>> print(out[0]) # To obtain the first dictionary:
{'buyMovingWeek': 8018735,
 'buyOrders': 70,
 'buyPrice': 15.848714783373357,
 'buyVolume': 624894,
 'productId': 'BROWN_MUSHROOM',
 'sellMovingWeek': 23716981,
 'sellOrders': 22,
 'sellPrice': 12.7,
 'sellVolume': 396395}

Of course a quicker way to write that code is with list comprehension which is well documented, so worth reading into:

original_items = [data['quick_status'] for _, data in f['products'].items()]

This can now be used in the linked code to end up with the following on the frontend:

table

Sign up to request clarification or add additional context in comments.

3 Comments

This is really cool! I will make sure to check this out, maybe see if I can re-do my design a bit to make this work! thank you :)
I might need this as I am also gonna add separate pages for each product, so on the "main page" you will see all products + some prices, now when you click on for example "LEATHER", its gonna open up a page with more detailed information, such as margin, demand and a graph etc. I think this you posted here might work perfect for that! :D
I don't really understand how you got the prices etc on HTML, I looked through the codes you shared and don't seem to get it to work, could you share the code maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.