[FLASK] Iteracja po słowniku w kodzie html + python

0

Witam,

mam następujący słownik

{1: {'followers': {'href': None, 'total': 6948654}, 'images': [{'height': 640, 'url': 'https://i.scdn.co/image/5a06711d7fc48d5e0e3f9a3274ffed3f0af1bd91', 'width': 640}, {'height': 320, 'url': 'https://i.scdn.co/image/0c22030833eb55c14013bb36eb6a429328868c29', 'width': 320}, {'height': 160, 'url': 'https://i.scdn.co/image/c1fb4d88de092b5617e649bd4c406b5cab7d3ddd', 'width': 160}], 'name': 'Metallica'}, 2: {'followers': {'href': None, 'total': 108}, 'images': [], 'name': 'Made famous by Metallica'}, 3: {'followers': {'href': None, 'total': 175}, 'images': [], 'name': 'Metallica Tribute Band'}} 

i chciałbym wyrzucić w ładnej formie dane z niego, np.

  1. Name: Metallica
    Followers, total: 6948654
    Image: https://i.scdn.co/image/5a06711d7fc48d5e0e3f9a3274ffed3f0af1bd91

i tak dalej.
Dla pozycji, dla których klucz nie ma wartości, wyrzucić np. none.

Coś nie idzie mi iteracja w kodzie html. Dane przekazuje jako data i kombinuje tak, żeby wyciągnąć name, dla przykładu:

<br>
      Found {{ dict_len }} results!
        <br>
        <br>
        {% for key, value in data.items() %}
            <h1>{{ key }}</h1>
            {% for x in value.items() %}
                <b>{{ x.name }}</b>
            {% endfor %}
        {% endfor %}

Może ktoś pokazać mi na przykładzie tego słownika, efektowne iterowanie tego typu danych?
Nie chce robić żadnych ifów, a wydaje mi się, że i bez tego da radę.

Pozdrawiam

0

Nikt Ci tutaj niestety nie pomoże, bo nie masz jakichkolwiek praw do wykorzystywania tych obrazków. No chyba że kontaktowałeś się z managerem Metallicy?

2

Zamiast cudować z logiką w widoku jak w jakimś php, to weź po prostu napis w backendzie swoim funkcje która to sparsuje i zwróci ci do widoku po prostu dane do wypisania na stronie...

0

No tak, trochę przekombinowane, ale ostatecznie wybrałem taką drogę, że nie filtrowałem sobie zwróconego JSON'a w żaden sposób, tylko zrobiłem z tego słownik.

data = resp.json()

Potem, w pliku html zapdałem sobie to co chciałem wyciągnąć:

<h1><font color="red">Found {{ data['artists']['items']|length }} results!</font></h1>
        <br>
        <br>
        {% for value in data['artists']['items'] %}
            <h1>Pos {{ loop.index }}</h1>
            Name: {{ value['name'] }}<br>
            Spotify link: <a href="{{ value['external_urls']['spotify'] }}">Click for Spotify!</a><br>
            Total followers: {{ value['followers']['total'] }}<br>
            Genres:
                {% if value['genres']|length > 0 %}
                  {% for i in value['genres'] %}
                    {{ i }},
                  {% endfor %}
                {% else %}
                  No info!
                {% endif %}
            <br>
            {% if (value['images'])[0]|length > 0 %}
              Img: <br><img src="{{ ((value['images'])[0])['url'] }}"><br>
            {% else %}
              Img: No images found!<br><br>
            {% endif %}
        {% endfor %}

a sama zawartość odpowiedzi JSON to:

{'artists': {'href': 'https://api.spotify.com/v1/search?query=Metallica&type=artist&offset=0&limit=20', 'items': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/2ye2Wgw4gimLv2eAKyk1NB'}, 'followers': {'href': None, 'total': 6957903}, 'genres': ['alternative metal', 'hard rock', 'metal', 'rock', 'speed metal', 'thrash metal'], 'href': 'https://api.spotify.com/v1/artists/2ye2Wgw4gimLv2eAKyk1NB', 'id': '2ye2Wgw4gimLv2eAKyk1NB', 'images': [{'height': 640, 'url': 'https://i.scdn.co/image/5a06711d7fc48d5e0e3f9a3274ffed3f0af1bd91', 'width': 640}, {'height': 320, 'url': 'https://i.scdn.co/image/0c22030833eb55c14013bb36eb6a429328868c29', 'width': 320}, {'height': 160, 'url': 'https://i.scdn.co/image/c1fb4d88de092b5617e649bd4c406b5cab7d3ddd', 'width': 160}], 'name': 'Metallica', 'popularity': 82, 'type': 'artist', 'uri': 'spotify:artist:2ye2Wgw4gimLv2eAKyk1NB'}, {'external_urls': {'spotify': 'https://open.spotify.com/artist/7xXiZGPcqDEijirJubILoZ'}, 'followers': {'href': None, 'total': 108}, 'genres': [], 'href': 'https://api.spotify.com/v1/artists/7xXiZGPcqDEijirJubILoZ', 'id': '7xXiZGPcqDEijirJubILoZ', 'images': [], 'name': 'Made famous by Metallica', 'popularity': 4, 'type': 'artist', 'uri': 'spotify:artist:7xXiZGPcqDEijirJubILoZ'}, {'external_urls': {'spotify': 'https://open.spotify.com/artist/61OPB1XJSYA5JoAvWZUQ4Z'}, 'followers': {'href': None, 'total': 175}, 'genres': [], 'href': 'https://api.spotify.com/v1/artists/61OPB1XJSYA5JoAvWZUQ4Z', 'id': '61OPB1XJSYA5JoAvWZUQ4Z', 'images': [], 'name': 'Metallica Tribute Band', 'popularity': 5, 'type': 'artist', 'uri': 'spotify:artist:61OPB1XJSYA5JoAvWZUQ4Z'}], 'limit': 20, 'next': None, 'offset': 0, 'previous': None, 'total': 3}} 

1 użytkowników online, w tym zalogowanych: 0, gości: 1