Display YouTube Playlist - Code-A2Z/jarvis GitHub Wiki
🔴 Display Jarvis YouTube Playlist!
To display the Jarvis YouTube Playlist to build an multi-page & multi-functional streamlit interface.
You need to have an YouTube API key to fetch all list of the videos & render them on streamlit interface.
Get your YouTube API key
-
Click on Credentials to create your own youtube API key.
-
Now click on + Create credentials dropdown list.
-
Select API key which helps you to fetch all list of videos in the youtube playlist.
-
Copy the API key and store it as a confidential credentials in
.streamlit/secrets.toml
file.
YOUTUBE_API_KEY="xxx"
NOTE: Later you can also able to see your api key using Show key
option.
Fetch YouTube Playlist Videos
Fetches all videos from a public playlist using its Playlist ID and your API Key.
import requests
PLAYLIST_ID="PLPUts_2rBVRVTrLlcB54Hwi6Ws51UWLXU"
URL=f"https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId={PLAYLIST_ID}"
def youtubePlaylistVideos(API_KEY):
URL2 = f"{URL}&key={API_KEY}"
response = requests.get(URL2)
videos = response.json().get('items', [])
return videos
Display the YouTube Videos in a 2-Column Layout
Renders the video title and embedded video player inside a clean UI. Private videos are skipped.
import streamlit as st
def displayVideos(videos):
filtered_videos = [
video for video in videos
if video['snippet'].get('title', '').lower() != 'private video'
]
for i in range(0, len(filtered_videos), 2):
cols = st.columns(2)
for j in range(2):
if i + j < len(filtered_videos):
video = filtered_videos[i + j]
video_title = video['snippet']['title'].split('|')[0].strip()
video_url = f"https://www.youtube.com/watch?v={video['snippet']['resourceId']['videoId']}"
with cols[j]:
st.markdown(f"##### [{video_title}]({video_url})")
st.video(video_url)
youtubePlaylist()
Page
Final Integration: Brings together the above logic and integrates it into the Jarvis app with user feedback, API key checks, and graceful error handling.
import os
from src.helpers.displayInstructions import showInstructions
from src.helpers.checkKeyExist import isKeyExist
def youtubePlaylist():
st.title("🎬 YouTube Playlist")
st.markdown("Explore the latest videos from the Jarvis YouTube playlist. Watch tutorials, feature demonstrations, and more to get started with Jarvis.")
exists = isKeyExist("YOUTUBE_API_KEY", "api_key")
if not exists["YOUTUBE_API_KEY"]:
showInstructions(markdown_text=api_guide, fields="YOUTUBE_API_KEY")
st.info(f"You can also go through my [blog post]({BLOG_URL}) for a detailed guide on how to get the YouTube API key.", icon="📝")
st.error("YouTube API key not found. Please add your API key to the secrets manager.", icon="🚨")
st.stop()
API_KEY = (st.secrets['api_key']["YOUTUBE_API_KEY"] or os.environ["YOUTUBE_API_KEY"])
if st.button("Show Videos"):
videos = youtubePlaylistVideos(API_KEY)
displayVideos(videos)
youtubePlaylist()