ChordPrompter / backupapp.py
awacke1's picture
Update backupapp.py
c61962c verified
raw
history blame
4.26 kB
import streamlit as st
import re
import os
import glob
# Set Streamlit page configuration to wide mode
st.set_page_config(layout="wide")
# Function to process each line of the chord sheet
def process_line(line):
if re.search(r'\b[A-G][#b]?m?\b', line):
line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
return line
# Function to process the entire chord sheet
def process_chord_sheet(chord_sheet):
processed_lines = []
for line in chord_sheet.split('\n'):
processed_line = process_line(line)
processed_lines.append(processed_line)
return '<br>'.join(processed_lines)
# Functions to create search URLs
def create_search_url_wikipedia(artist_song):
base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
def create_search_url_youtube(artist_song):
base_url = "https://www.youtube.com/results?search_query="
return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
def create_search_url_chords(artist_song):
base_url = "https://www.ultimate-guitar.com/search.php?search_type=title&value="
return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')
def create_search_url_lyrics(artist_song):
base_url = "https://www.google.com/search?q="
return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and') + '+lyrics'
# Streamlit app
def main():
# Sidebar with file loader
with st.sidebar:
st.title('🎵 Song Files')
with st.expander("Select Song File"):
all_files = glob.glob("*.txt")
selected_file = st.selectbox("Choose a file", all_files)
# Main area layout
col1, col2 = st.columns([2, 1])
with col1:
with st.expander("🎶 Song and Artist"):
song_name = st.text_input("🎵 Song Name")
artist_name = st.text_input("🎤 Artist Name")
chord_sheet_input = st.text_area("Chord Sheet", height=300)
if st.button("💾 Save", key="save_song"):
if song_name and artist_name:
filename = f"{song_name} by {artist_name}.txt".replace(" ", "_")
with open(filename, "w") as file:
file.write(chord_sheet_input)
st.success("Chord sheet saved.")
else:
st.error("Both Song Name and Artist Name are required.")
with col2:
if selected_file:
song_name_artist = os.path.splitext(selected_file)[0].replace("_", " ")
st.markdown(f"**Selected Song:** {song_name_artist}")
st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_name_artist)})")
st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_name_artist)})")
st.markdown(f"[🎸Chords]({create_search_url_chords(song_name_artist)})")
st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_name_artist)})")
load_song_file(selected_file)
# Displaying song files
st.header("🎼 Available Songs")
for file in all_files:
song_info = os.path.splitext(file)[0].replace("_", " ")
col1, col2, col3, col4, col5 = st.columns([4, 1, 1, 1, 1])
with col1:
st.markdown(f"* {song_info}")
with col2:
st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_info)})")
with col3:
st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_info)})")
with col4:
st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})")
with col5:
st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})")
# Function to load and display the selected song file
def load_song_file(filename):
with open(filename, "r") as file:
chord_sheet = file.read()
st.text_area("Chord Sheet", chord_sheet, height=300)
processed_sheet = process_chord_sheet(chord_sheet)
st.markdown(processed_sheet, unsafe_allow_html=True)
if __name__ == '__main__':
main()