awacke1 commited on
Commit
14ad63c
1 Parent(s): 5d40007

Update backupapp.py

Browse files
Files changed (1) hide show
  1. backupapp.py +80 -55
backupapp.py CHANGED
@@ -36,86 +36,111 @@ def create_search_url_lyrics(artist_song):
36
  def songupdate():
37
  st.write(st.session_state.EnhancedChordSheet)
38
 
39
- def load_song_file(filename):
40
  with open(filename, "r") as file:
41
  chord_sheet = file.read()
42
- st.text_area(label="Enhanced Chord Sheet", value=chord_sheet, height=300, key="EnhancedChordSheet", help="This text can be read due to alternating chord lines and lyric lines.", label_visibility="visible", on_change=songupdate )
43
  processed_sheet = process_chord_sheet(chord_sheet)
44
  st.markdown(processed_sheet, unsafe_allow_html=True)
45
 
46
- # Automatic save function
47
- def auto_save():
48
- song_name = st.session_state['song_name']
49
- artist_name = st.session_state['artist_name']
50
- chord_sheet = st.session_state['chord_sheet']
51
 
52
- # Display the character count
53
- st.sidebar.write("Character Count:", len(chord_sheet))
 
 
 
54
 
55
- if song_name and artist_name:
56
- filename = f"{song_name} by {artist_name}.txt".replace(" ", "_")
 
 
 
 
 
 
 
 
 
 
57
  with open(filename, "w") as file:
58
- file.write(chord_sheet)
59
- st.sidebar.success("Chord sheet auto-saved.")
 
 
 
 
 
60
 
61
  def main():
62
- st.title('🎵 Song Files')
63
- with st.expander("Select Song File", expanded=True):
64
- all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
65
- selected_file = st.selectbox("Choose a file", all_files)
66
- if selected_file:
67
- song_name, artist_name = os.path.splitext(selected_file)[0].split(' by ')
68
- song_name = song_name.replace("_", " ")
69
- artist_name = artist_name.replace("_", " ")
70
- else:
71
- song_name, artist_name = "", ""
72
- col1, col2 = st.columns([4, 1])
73
  with col1:
74
- song_name_input = st.text_input("🎵 Song Name", key='song_name')
75
- artist_name_input = st.text_input("🎤 Artist Name", key='artist_name')
76
-
77
- # Load chord sheet from selected file into the text area
78
- if selected_file:
79
- with open(selected_file, "r") as file:
80
- chord_sheet_input = file.read()
81
- st.session_state['chord_sheet'] = chord_sheet_input
82
- chord_sheet_area = st.text_area("Chord Sheet", value=chord_sheet_input, height=300, key='chord_sheet', on_change=auto_save)
83
- else:
84
- chord_sheet_area = st.text_area("Chord Sheet", height=300, key='chord_sheet', on_change=auto_save)
85
 
86
  # Save functionality
87
  if st.button("💾 Save", key="save_song"):
88
  if song_name_input and artist_name_input:
89
- filename = f"{song_name_input} by {artist_name_input}.txt".replace(" ", "_")
90
  with open(filename, "w") as file:
91
  file.write(chord_sheet_area)
92
- st.success("Chord sheet saved.")
93
  else:
94
  st.error("Both Song Name and Artist Name are required.")
95
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  st.header("🎼 Available Songs")
97
  for file in all_files:
98
  song_info = os.path.splitext(file)[0].replace("_", " ")
99
- icol1, icol2 = st.columns([4, 1])
100
  with icol1:
101
- st.markdown(f"* {song_info}")
 
 
102
  with icol2:
103
- st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_info)})")
104
- st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_info)})")
105
- st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})")
106
- st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})")
107
-
108
- if selected_file:
109
- load_song_file(selected_file)
110
- song_info = os.path.splitext(selected_file)[0].replace("_", " ")
111
-
112
- with col2:
113
- if selected_file:
114
- st.markdown(f"**Selected Song:** {song_info}")
115
- st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_info)})")
116
- st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_info)})")
117
- st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})")
118
- st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})")
 
 
119
 
120
 
121
  if __name__ == '__main__':
 
36
  def songupdate():
37
  st.write(st.session_state.EnhancedChordSheet)
38
 
39
+ def load_song_file2(filename):
40
  with open(filename, "r") as file:
41
  chord_sheet = file.read()
42
+ st.session_state['chord_sheet'] = chord_sheet
43
  processed_sheet = process_chord_sheet(chord_sheet)
44
  st.markdown(processed_sheet, unsafe_allow_html=True)
45
 
46
+ def load_song_file(filename):
47
+ with open(filename, "r") as file:
48
+ chord_sheet = file.read()
49
+ return chord_sheet
 
50
 
51
+ def song_update():
52
+ if 'selected_file' in st.session_state:
53
+ song_name, artist_name = parse_filename(st.session_state.selected_file)
54
+ st.session_state.song_name = song_name
55
+ st.session_state.artist_name = artist_name
56
 
57
+ def parse_filename(filename):
58
+ base_name = os.path.splitext(filename)[0]
59
+ song_name, artist_name = base_name.split(' by ')
60
+ return song_name.replace("_", " "), artist_name.replace("_", " ")
61
+
62
+ def auto_save():
63
+ song_name = st.session_state.get('song_name', '')
64
+ artist_name = st.session_state.get('artist_name', '')
65
+ chord_sheet = st.session_state.get('chord_sheet', '')
66
+
67
+ if song_name and artist_name and chord_sheet:
68
+ filename = song_name + " by " + artist_name + ".txt"
69
  with open(filename, "w") as file:
70
+
71
+
72
+ chord_sheet_text = st.session_state.get('chord_sheet', '')
73
+ file.write(chord_sheet_text)
74
+
75
+ st.session_state['char_count'] = len(chord_sheet)
76
+ st.success(f"Auto-saved to {filename}")
77
 
78
  def main():
79
+ st.markdown('### 🎵 ChordSheet - Music Playing and Authoring App')
80
+
81
+ col1, col3 = st.columns([3, 5])
82
+
 
 
 
 
 
 
 
83
  with col1:
84
+ with st.expander("Select Song:", expanded=True):
85
+ all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
86
+ selected_file = st.selectbox("Choose: ", all_files, on_change=song_update, key='selected_file')
87
+
88
+ song_name_input = st.text_input("🎵 Song:", key='song_name', on_change=auto_save)
89
+ artist_name_input = st.text_input("🎤 Artist:", key='artist_name', on_change=auto_save)
90
+
91
+ if 'selected_file' in st.session_state and st.session_state.selected_file:
92
+ # Update the session state before creating the text area widget
93
+ st.session_state['chord_sheet'] = load_song_file(st.session_state.selected_file)
 
94
 
95
  # Save functionality
96
  if st.button("💾 Save", key="save_song"):
97
  if song_name_input and artist_name_input:
98
+ filename = song_name_input + " by " + artist_name_input + ".txt"
99
  with open(filename, "w") as file:
100
  file.write(chord_sheet_area)
101
+ st.success("Chord sheet saved to file: " + filename)
102
  else:
103
  st.error("Both Song Name and Artist Name are required.")
104
 
105
+ st.header("🎼 Current Song")
106
+ load_song_file(selected_file)
107
+ song_info = os.path.splitext(selected_file)[0].replace("_", " ")
108
+ st.markdown("**" + song_info + "**")
109
+ table_md = f"""
110
+ | Wikipedia | YouTube | Chords | Lyrics |
111
+ | --------- | ------- | ------ | ------ |
112
+ | [📚]({create_search_url_wikipedia(song_info)}) | [🎥]({create_search_url_youtube(song_info)}) | [🎸]({create_search_url_chords(song_info)}) | [🎶]({create_search_url_lyrics(song_info)}) |
113
+ """
114
+ st.markdown(table_md)
115
+
116
+
117
  st.header("🎼 Available Songs")
118
  for file in all_files:
119
  song_info = os.path.splitext(file)[0].replace("_", " ")
120
+ icol1, icol2 = st.columns([1, 3])
121
  with icol1:
122
+ st.markdown("**" + song_info + "**")
123
+ load_song_file(file)
124
+ song_info = os.path.splitext(file)[0].replace("_", " ")
125
  with icol2:
126
+ # Create a markdown table with links for each song file
127
+ table_md = f"""
128
+ | Wikipedia | YouTube | Chords | Lyrics |
129
+ | --------- | ------- | ------ | ------ |
130
+ | [📚]({create_search_url_wikipedia(song_info)}) | [🎥]({create_search_url_youtube(song_info)}) | [🎸]({create_search_url_chords(song_info)}) | [🎶]({create_search_url_lyrics(song_info)}) |
131
+ """
132
+ st.markdown(table_md)
133
+
134
+ with col3:
135
+ chord_sheet_area = st.text_area("Chord Sheet", value=st.session_state.get('chord_sheet', ''), height=1200, key='chord_sheet', on_change=auto_save)
136
+ char_count_msg = f"Character Count: {st.session_state.get('char_count', 0)}"
137
+ st.write(char_count_msg)
138
+
139
+
140
+
141
+ # Load chord sheet from selected file into the text area
142
+ if 'selected_file' in st.session_state and st.session_state.selected_file:
143
+ load_song_file(st.session_state.selected_file)
144
 
145
 
146
  if __name__ == '__main__':