0

I wrote my own wishlist in python3 flask (with apache mod_wsgi)

this is my data structure for the wishlist items:

{
  },
  "": {
    "images": {
      "pic": ""
    },
    "buylinks": {
      "": {
        "link": "",
        "price": ""
      }
    },
    "want": "",
    "comments": ""
  }
}

for example:

"Nintendo Joy-Con (L/R) - Gray": {
    "images": {
      "pic": "wishlist/joycons-grey.jpg"
    },
    "buylinks": {
      "Amazon": {
        "link": "https://www.amazon.com/dp/B01N6QKT7H",
        "price": "66.99"
      }
    },
    "want": "7/10",
    "comments": "extra joycons for playing with friends on my switch"
  },

right now every time I update the json file containing the wishlist data the apache server jinja template spits it iut in a different order

my jinja template looks like

<table>
        <tr>
            <th>Name</th>
            <th>Image(s)</th>
            <th>Links To Buy</th>
            <th>Want/10</th>
            <th>Comments</th>
        </tr>
        {% for key in wishlist.keys() %}
        <tr>
            <td><b>{{ key }}</b></td>
            <td>{% for image in wishlist[key]["images"].keys() %}
            <img src="{{ url_for('static', filename=wishlist[key]["images"][image]) }}" width="250px">
            {% endfor %}</td>
            <td>{% for merchant in wishlist[key]["buylinks"].keys() %}
            <a href="/redirect/?URL={{ wishlist[key]["buylinks"][merchant]["link"] }}&linkedfrom=wishlist" target="_blank">{{ merchant}} </a>(~${{ wishlist[key]["buylinks"][merchant]["price"] }})
            <br>{% endfor %}</td>
            <td><a> {{ wishlist[key]["want"] }} </a></td>
            <td> {{ wishlist[key]["comments"] }}</td>
        </tr>
        {% endfor %}
    </table>

how do I modify the {% for key in wishlist.keys() %} line to sort the wishlist by the value of the want key such that 10/10 is at the beginning and 0/10 is at the end? (the value to use to sort the example item would be "7/10")

I think I need to use the "sorted" function but I'm not sure how to

cTurtle98
  • 11
  • 2

1 Answers1

0

so to get this to work I had to add

wishlist_keys = sorted(wishlist.keys(), reverse=True, key=lambda x: wishlist[x]["want"] )

to my flask python code and pass wishlist_keys into render_template()

and change the template to

{% for key in wishlist_keys %}
cTurtle98
  • 11
  • 2