DEV Community

Cover image for How To Build A Spotify Playlist Analyzer
Chukwuebuka (Gaus Octavio )
Chukwuebuka (Gaus Octavio )

Posted on

How To Build A Spotify Playlist Analyzer

The Spotify Playlist Analyzer is a Streamlit app that allows users to analyze a Spotify playlist using data visualization tools. The app uses the Spotipy library to access data from the Spotify API and the Plotly library to create interactive charts.

To use the app, the user inputs the name of the Spotify playlist they wish to analyze in a text input box on the sidebar. The app then searches for the playlist ID based on the input name and retrieves the playlist data from the Spotify API.
The playlist data is displayed in a table that shows the track name, artist name, album name, release date, popularity, and duration in milliseconds for each track in the playlist.

The app also provides three types of analysis for the playlist data:

Track Popularity Distribution - a histogram that shows the distribution of track popularity in the playlist.

Bivariate Analysis - a scatter plot that allows the user to select two variables (popularity and duration in milliseconds) to plot against each other.

Multivariate Analysis - a scatter plot that allows the user to select one variable to color the data points by (artist name, album name, or release date) and one variable to size the data points by (popularity or duration in milliseconds).

In addition to the data visualizations, the app also provides a playlist summary that shows the most popular and least popular tracks in the playlist and a bar chart that displays the top 10 most popular artists in the playlist.


Prerequisites
To follow along with this tutorial, you will need:
A Spotify account
A Spotify Developer account
Python 3.x installed on your machine
Familiarity with Python programming
Familiarity with the Pandas, Streamlit, and Plotly libraries

Step 1: Setting up a Spotify Developer Account
To use the Spotify API, you need to have a Spotify Developer account. If you don't already have one, go to https://developer.spotify.com/dashboard/ and create an account. Once you've created an account, create a new Spotify application to get your client_id and client_secret.

Step 2: Installing the Required Libraries
We will be using the following libraries for this project:
Streamlit
Spotipy
Pandas
Plotly

You can install these libraries by running the following command in your terminal:


pip install streamlit spotipy pandas plotly
Enter fullscreen mode Exit fullscreen mode

Step 3: Authenticating with the Spotify API
To authenticate with the Spotify API, we will be using the spotipy library. We'll need to create an instance of the Spotify class, passing in our client_id and client_secret . We'll also need to create an instance of the SpotifyClientCredentials class, passing in our client_id and client_secret.

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

client_id = 'YOUR_CLIENT_ID'
client_secret = 'YOUR_CLIENT_SECRET'

client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
Enter fullscreen mode Exit fullscreen mode

Step 4: Creating a Streamlit App
We'll use Streamlit to create a simple UI that allows users to input a Spotify playlist name and displays various analyses of the playlist. To create a Streamlit app, we first need to import the streamlit library and call the title() function to set the title of the app.

import streamlit as st

st.title("Spotify Playlist Analyzer")
Enter fullscreen mode Exit fullscreen mode

Step 5: Adding User Input with Streamlit
We'll use Streamlit's sidebar to add a text input field for the user to enter the name of the Spotify playlist they want to analyze.

playlist_name = st.sidebar.text_input("Enter the name of the Spotify playlist:")
Enter fullscreen mode Exit fullscreen mode

Step 6: Retrieving Data from the Spotify API
If the user enters a playlist name, we'll search for the playlist ID using the search() function of the Spotify class. We'll then retrieve data for each track in the playlist, such as the track name, artist name, popularity, duration, album name, and release date.

if playlist_name:
    playlists = sp.search(playlist_name, type="playlist")["playlists"]["items"]
    if playlists:
        playlist_id = playlists[0]["id"]
    else:
        st.write("No playlists found with that name.")
        playlist_id = None
else:
    playlist_id = None
Enter fullscreen mode Exit fullscreen mode

Step 7: Retrieve Data from the Spotify API
If we have a valid playlist ID, we can use the Spotipy library to retrieve data from the Spotify API.

if playlist_id:
    playlist = sp.playlist(playlist_id)
    tracks = playlist["tracks"]["items"]
    track_names = [track["track"]["name"] for track in tracks]
    track_artists = [", ".join([artist["name"] for artist in track["track"]["artists"]]) for track in tracks]
    track_popularity = [track["track"]["popularity"] for track in tracks]
    track_duration = [track["track"]["duration_ms"] for track in tracks]
    track_album = [track["track"]["album"]["name"] for track in tracks]
    track_release_date = [track["track"]["album"]["release_date"] for track in tracks]
Enter fullscreen mode Exit fullscreen mode

Step 8: Display Playlist Data in a Table
We can use Streamlit to display the playlist data in a table.

# display the playlist data in a table
    st.write(f"## {playlist['name']}")
    st.write(f"**Description:** {playlist['description']}")
    st.write(f"**Number of tracks:** {len(tracks)}")
    st.write("")
    st.write("### Tracklist")
    st.write("| Name | Artist | Album | Release Date | Popularity | Duration (ms) |")
    st.write("| ---- | ------ | ----- | ------------ | ---------- | -------------- |")
    for i in range(len(tracks)):
        st.write(f"| {track_names[i]} | {track_artists[i]} | {track_album[i]} | {t
rack_release_date[i]} | {track_popularity[i]} | {track_duration[i]} |")

Enter fullscreen mode Exit fullscreen mode

Step 9: Create a DataFrame From The Playlist Data
Pandas can be used to the playlist data to a DataFrame that would be used for further analysis

# create a dataframe from the playlist data
    data = {"Name": track_names, "Artist": track_artists, "Album": track_album, "Release Date": track_release_date, "Popularity": track_popularity, "Duration (ms)": track_duration}
    df = pd.DataFrame(data)
Enter fullscreen mode Exit fullscreen mode

Step 10: Display A Histogram Of Tracks Popularity

# display a histogram of track popularity
    fig_popularity = px.histogram(df, x="Popularity", nbins=20, title="Track Popularity Distribution")
    st.plotly_chart(fig_popularity)

Enter fullscreen mode Exit fullscreen mode

11: Bivariate Analysis
Next, the user is prompted to select two variables for a bivariate analysis. The code then creates a scatter plot of the selected variables.

# add a dropdown menu for bivariate analysis
    st.write("#### Bivariate Analysis")
    x_axis = st.selectbox("Select a variable for the x-axis:", ["Popularity", "Duration (ms)"])
    y_axis = st.selectbox("Select a variable for the y-axis:", ["Popularity", "Duration (ms)"])
    fig_bivariate = px.scatter(df, x=x_axis, y=y_axis, title=f"{x_axis} vs. {y_axis}")
    st.plotly_chart(fig_bivariate)
Enter fullscreen mode Exit fullscreen mode

12: Multivariate Analysis
The user is prompted to select two variables for a multivariate analysis. The code then creates a scatter plot of the selected variables, colored by one variable and sized by another variable.

# add a dropdown menu for multivariate analysis
    st.write("#### Multivariate Analysis")
    color_by = st.selectbox("Select a variable to color by:", ["Artist", "Album", "Release Date"])
    size_by = st.selectbox("Select a variable to size by:", ["Popularity", "Duration (ms)"])
    fig_multivariate = px.scatter(df, x="Duration (ms)", y="Popularity", color=color_by, size=size_by, hover_name="Name", title="Duration vs. Popularity Colored by Artist")
    st.plotly_chart(fig_multivariate)
Enter fullscreen mode Exit fullscreen mode

13: A Summary Of The Playlist Data
The code below displays a summary of the playlist data to the user. This summary includes the most popular and least popular tracks in the playlist, as well as a bar chart of the top 10 most popular artists and tracks in the playlist

# add a summary of the playlist data
    st.write("")
    st.write("### Playlist Summary")
    st.write(f"**Most popular track:** {df.iloc[df['Popularity'].idxmax()]['Name']} by {df.iloc[df['Popularity'].idxmax()]['Artist']} ({df['Popularity'].max()} popularity)")
    st.write(f"**Least popular track:** {df.iloc[df['Popularity'].idxmin()]['Name']} by {df.iloc[df['Popularity'].idxmin()]['Artist']} ({df['Popularity'].min()} popularity)")
# display a bar chart of the top 10 most popular artists in the playlist
    st.write("#### Top 10 Artists")
    st.write("The bar chart below shows the top 10 most popular artists in the playlist.")
    top_artists = df.groupby("Artist").mean().sort_values("Popularity", ascending=False).head(10)
    fig_top_artists = px.bar(top_artists, x=top_artists.index, y="Popularity", title="Top 10 Artists")
    st.plotly_chart(fig_top_artists)    
# display a bar chart of the top 10 most popular songs in the playlist
    st.write("#### Top 10 Songs")
    st.write("The bar chart below shows the top 10 most popular songs in the playlist.")
    top_artistss = df.groupby("Name").mean().sort_values("Popularity", ascending=False).head(10)
    fig_top_artistss = px.bar(top_artistss, x=top_artistss.index, y="Popularity", title="Top 10 Songs")
    st.plotly_chart(fig_top_artistss)
Enter fullscreen mode Exit fullscreen mode

14: Running The App
The final step is to run the Streamlit app. This allows the user to interact with the app and analyze their chosen Spotify playlist.


Overall, the Spotify Playlist Analyzer is a useful tool for anyone who wants to explore the data behind their favorite Spotify playlists and gain insights into their music preferences.
It retrieves data from the Spotify API, displays it in a table, and allows users to analyze it using data visualization techniques.
The code is easy to use and provides valuable insights into the data of a Spotify playlist.



Top comments (0)