Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from songscope import * | |
import random | |
# !python -m pip install --upgrade pip | |
# main header | |
st.title("SongScope") | |
# web app explainer | |
st.write("Welcome to SongScope. This is a web app that lets you download lyrics and metadata of your favourite artist.\n") | |
st.write("Enter an artist name, and you can even select up to 5 songs to retrieve their data. If you don't specify any songs,\ndata on all songs by the artist will be retrieved.") | |
# delay times | |
delay_min = st.number_input(label = "Minimum delay per iteration. We recommend a value of at least 10 seconds per song, otherwise you may be banned from azlyrics.com for scraping.", | |
step = 1., format = "%.2f", value = float(10)) | |
delay_max = st.number_input(label = "Maximum delay per iteration. We recommend a value of at least 10 seconds per song, otherwise you may be banned from azlyrics.com for scraping.", | |
step = 1., format = "%.2f", value = float(20)) | |
###################################################################################################################################################### | |
# Define a function to handle the state of the input fields | |
def manage_input_fields(session_state): | |
if "input_fields" not in session_state: | |
session_state.input_fields = [{}] | |
# Render the input fields | |
for index, input_field in enumerate(session_state.input_fields): | |
value = st.text_input(f"Text Input {index + 1}", key=f"text_input_{index}") | |
input_field["value"] = value | |
# Check if the last field is empty and add a new field | |
if session_state.input_fields[-1]["value"]: | |
session_state.input_fields.append({}) | |
# Main function | |
def main(): | |
st.title("Dynamic Text Inputs with Streamlit") | |
# Initialize the session state | |
if "session_state" not in st.session_state: | |
st.session_state["session_state"] = {} | |
session_state = st.session_state["session_state"] | |
# Manage input fields | |
manage_input_fields(session_state) | |
# Add a submit button | |
if st.button("Submit"): | |
st.write("Values submitted:") | |
for index, input_field in enumerate(session_state.input_fields[:-1]): | |
st.write(f"Text Input {index + 1}: {input_field['value']}") | |
if __name__ == "__main__": | |
main() | |
###################################################################################################################################################### | |
# user-provided data | |
artist_name = st.text_input("Enter artist name here") | |
song_1 = st.text_input("Enter 1st song title here", value = "jn mayer") # delete this when done | |
if song_1: | |
song_2 = st.text_input("Enter 2nd song title here") | |
if song_2: | |
song_3 = st.text_input("Enter 3rd song title here") | |
if song_3: | |
song_4 = st.text_input("Enter 4th song title here") | |
if song_4: | |
song_5 = st.text_input("Enter 5th song title here") | |
# funnies | |
funnies = [] | |
with open("fun_fax.txt", "r", encoding = "utf-8") as f: | |
for funny in f.read().split("\n"): | |
funnies.append(funny) | |
f.close() # closes connection to file | |
# data is found | |
find_button = st.button('Find Data') | |
if not find_button: # button to make everything run | |
st.write("Once you've entered the above data, click the 'Find Data' button, above, to begin.") | |
else: | |
artist_df = get_all_data(artist_name = artist_name, | |
song_titles = song_1 + song_2 + song_3 + song_4 + song_5, | |
delay = (delay_min, delay_max), # random float value between (delay_min, delay_max) seconds | |
print_progress = True) | |
# download button and download | |
dl_button = st.download_button('Download Data', artist_df, f"{artist_name.lower()}_data.csv") | |
if dl_button: | |
st.write("Your data should be downloading. If not, please reload the page and try again.") | |
# fun joke | |
joke_button = st.button("Want to hear a joke while you wait?") | |
if joke_button: | |
st.write(funnies[random.randint(0, len(funnies) - 1)]) | |
st.write("Click the button again to get another!") | |
# "thank you" blurb | |
st.write("\nThanks for checking out SongScope! If you have any feedback or requests for additions to this app, shoot me an email at kmaurinjones@gmail.com.") |