Возвращать только положительные значения в Flask - PullRequest
1 голос
/ 28 мая 2020

Итак, я работаю над проектом, таблицей, которая может показать вам, от каких предметов вы можете получить прибыль. Проблема в том, что я хочу, чтобы он отображал элементы, превышающие 1, то есть все положительные значения. Прямо сейчас он показывает продукты, от которых вы тоже не можете получить прибыль!

Вот изображение веб-сайта: picture

Вот мой python код:

@app.route('/bresell')
def reSell():
    farmingMerchantPrices = [
        5,  # cocoa beans
        12,  # brown mushroom
        2.33,  # carrot
        8,  # pumpkin
        2.33,  # wheat
        12,  # red mushroom
        2.33,  # potato
        4,  # sand
        5,  # sugar cane
        2,  # melon
    ]
    farmingMerchantName = [
        "Cocoa Beans",
        "Brown Mushroom",
        "Carrot",
        "Pumpkin",
        "Wheat",
        "Red Mushroom",
        "Potato",
        "Sand",
        "Sugar Cane",
        "Melon"
    ]
    sellPrice = []
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2').json()
    for x in farmingProducts:
        sellPrice.append(f["products"][x]["sell_summary"][0]["pricePerUnit"])

    profit = []
    zip_object = zip(farmingMerchantPrices, sellPrice)
    for farmingMerchantPrices_i, sellPrice_i in zip_object:
        profit.append(sellPrice_i - farmingMerchantPrices_i)

Мой оператор возврата находится ниже по коду.

А вот мой HTML:

<h1 class="npc-title">Farming Merchant</h1>
  <table
    id="myTable"
    class="table table-striped table-bordered table-sm table-dark"
    cellspacing="0"
  >
    <thead>
      <tr>
        <th aria-label="Product Name" data-balloon-pos="up">Product</th>
        <th aria-label="How much you will earn" data-balloon-pos="up">
          Profit (x640)
        </th>
        <th aria-label="How much you will earn" data-balloon-pos="up">
          Profit (x1)
        </th>
        <th aria-label="NPC buy price" data-balloon-pos="up">
          NPC Buy Price
        </th>
        <th aria-label="Bazaar sell price" data-balloon-pos="up">
          Bazaar Sell Price
        </th>
      </tr>
    </thead>
    <tbody>
      {% for name, npcBuy, price, profit in zip(farmingMerchantName,
      farmingMerchantPrices,sellPrice, profit) %}
      <tr>
        <td>{{ name }}</td>
        {% set profit2 = profit * 640%}
        <td>{{profit2|round(1, 'floor')}}</td>
        <td>{{ profit|round(1, 'floor') }}</td>
        <td>{{ npcBuy|round(1, 'floor') }}</td>
        <td>{{ price }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>

Я не уверен, как я могу это сделать, я думаю, мне нужно сделать оператор IF, чтобы проверить, меньше ли общая прибыль, чем 1, но, как вы можете видеть, я вычисляю прибыль в моем HTML!

Ответы [ 2 ]

0 голосов
/ 28 мая 2020

Предполагая, что вы проверяете только profit, вы можете использовать условие if, как показано ниже:

{% for name, npcBuy, price, profit in zip(farmingMerchantName, farmingMerchantPrices,sellPrice, profit) %}
  {% if profit > 1 %}
    <tr>
      <td>{{ name }}</td>
      {% set profit2 = profit * 640 %}
      <td>{{ profit2|round(1, 'floor') }}</td>
      <td>{{ profit|round(1, 'floor') }}</td>
      <td>{{ npcBuy|round(1, 'floor') }}</td>
      <td>{{ price }}</td>
    </tr>
  {% endif %}
{% endfor %}
0 голосов
/ 28 мая 2020

Jinja также позволяет использовать операторы if для html, поэтому добавление оператора if, проверяющего вашу прибыль на вашем html, должно помочь:

<tbody>
  {% for name, npcBuy, price, profit in zip(farmingMerchantName,
  farmingMerchantPrices,sellPrice, profit) %}
  {% if profit > 1 %}
  <tr>
    <td>{{ name }}</td>
    {% set profit2 = profit * 640%}
    <td>{{profit2|round(1, 'floor')}}</td>
    <td>{{ profit|round(1, 'floor') }}</td>
    <td>{{ npcBuy|round(1, 'floor') }}</td>
    <td>{{ price }}</td>
  </tr>
  {% endif %}
  {% endfor %}
</tbody>
...