Spaces:
Sleeping
Sleeping
Download Linked_list/playlist_app.py from swetha-coding/Dsa-hub: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/spaces/swetha-coding/Dsa-hub/resolve/main/Linked_list/playlist_app.py
- Command line
-
hf download hf://spaces/swetha-coding/Dsa-hub/Linked_list/playlist_app.py
-
curl -L -o playlist_app.py https://huggingface.co/spaces/swetha-coding/Dsa-hub/resolve/main/Linked_list/playlist_app.py
2.16 kB
| from flask import Blueprint, render_template, request | |
| playlist_app = Blueprint("playlist_app", __name__, template_folder="templates") | |
| class Song: | |
| def __init__(self, title): | |
| self.title = title | |
| self.next = None | |
| self.prev = None | |
| class CircularPlaylist: | |
| def __init__(self): | |
| self.current = None | |
| def add_song(self, title): | |
| new_song = Song(title) | |
| if self.current is None: | |
| self.current = new_song | |
| new_song.next = new_song | |
| new_song.prev = new_song | |
| else: | |
| last = self.current.prev | |
| last.next = new_song | |
| new_song.prev = last | |
| new_song.next = self.current | |
| self.current.prev = new_song | |
| def play(self): | |
| if self.current: | |
| return f"🎶 Now Playing: {self.current.title}" | |
| return "No songs in playlist." | |
| def next_song(self): | |
| if self.current: | |
| self.current = self.current.next | |
| return self.play() | |
| def prev_song(self): | |
| if self.current: | |
| self.current = self.current.prev | |
| return self.play() | |
| def get_playlist(self): | |
| if not self.current: | |
| return [] | |
| temp = self.current | |
| songs = [] | |
| while True: | |
| songs.append(temp.title) | |
| temp = temp.next | |
| if temp == self.current: | |
| break | |
| return songs | |
| playlist = CircularPlaylist() | |
| def playlist_index(): | |
| if request.method == "POST": | |
| title = request.form["song"] | |
| playlist.add_song(title) | |
| songs = playlist.get_playlist() | |
| status = playlist.play() | |
| return render_template("playlist.html", songs=songs, status=status) | |
| def playlist_next(): | |
| status = playlist.next_song() | |
| songs = playlist.get_playlist() | |
| return render_template("playlist.html", songs=songs, status=status) | |
| def playlist_prev(): | |
| status = playlist.prev_song() | |
| songs = playlist.get_playlist() | |
| return render_template("playlist.html", songs=songs, status=status) | |