Create a streamlit app, which take a spotify api key, fetch my top listenied songs, and create vizualization about it, correlation between the them, ........
To upload files, please first save the app
import pandas as pd
import requests
import matplotlib.pyplot as plt
st.title("Spotify Top Songs Visualization")
# Input for Spotify API Key
api_key = st.text_input("Enter your Spotify API Key:")
if api_key:
# Fetch user's top tracks
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.get("https://api.spotify.com/v1/me/top/tracks", headers=headers)
if response.status_code == 200:
data = response.json()
tracks = data["items"]
# Extract track names and popularity
track_names = [track["name"] for track in tracks]
popularity = [track["popularity"] for track in tracks]
# Create a DataFrame
top_songs_df = pd.DataFrame({"Track": track_names, "Popularity": popularity})
# Display the DataFrame
st.write(top_songs_df)
# Visualization
st.subheader("Popularity of Top Songs")
plt.figure(figsize=(10, 5))
plt.barh(top_songs_df["Track"], top_songs_df["Popularity"], color='skyblue')
plt.xlabel("Popularity")
plt.title("Top 10 Spotify Tracks by Popularity")
plt.gca().invert_yaxis() # invert y axis to show the most popular track at the top
st.pyplot(plt)
else:
st.error(f"Error fetching data: {response.status_code}")
else:
st.write("Please enter your Spotify API Key to get started.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?