Most of the remaining code was added to the following files:
mixtape.py
: so that tracks_detail
, imported into main.py
, contains the necessary data for our template templates/track_section.html
: so that the cards for each track are automatically populated using this data Collection of the data from the API of course requires a valid access_token
, which was updated in auth.py
.
The reset_votes()
statement in main.py
has also been commented out after the first execution of main.py
, so that the vote counts are retained even if the repl is re-started.
Let's go through mixtape.py
:
from auth import access_token
from tracks import get_track_data, track_summary
from replit import db
track_ids = [...]
tracks_detail = []
.py
filestrack_ids
that we want to collect data fortracks_detail
for track_id in track_ids:
if track_id in db.keys():
data = db[track_id]
else:
data = get_track_data(access_token, track_id)
if data:
db[track_id] = data
...
track_ids
using a for
looptrack_id
is a key in the db
(meaning it has been stored previously)db
we collect the data from thereget_track_data()
to request it from the Spotify APIdb
...
if not data:
print(f'Something went wrong with track {track_id}')
else:
tracks_detail.append(track_summary(data))
data
is not "truthy", i.e. it is not None
, empty, zero, etc (if there has been an issue with collecting the data from the API, get_track_data()
would have returned None
)data
is truthy, we process it using tracks_detail()
and .append()
the result (which will be a dictionary) to our tracks_detail
list, which is imported by main.py
Take a look at tracks_section.html
in /templates
.
{% for track in tracks_detail %}
...
<img class="card-img-top img-responsive full-width" src="{{ track['image_url'] }}">
The Jinja for
statement was already in place, to create a card for each item in tracks_detail
.
data-src
was used with the holder.js
image placeholder servicesrc
, and the value is changed to use the image_url
for the given track
<p class="card-text">
<strong>{{ track['track_name'] }}</strong><br>
{{ track['artist_name'] }}
</p>
card-text
paragraph, we've added the track_name
and artist_name
values from each track
dictionary in track_details
strong
tag to make the text for artist_name
more prominent