awacke1 commited on
Commit
cfa921b
1 Parent(s): b21a71c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -26
app.py CHANGED
@@ -36,59 +36,75 @@ 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
 
47
- # Automatic save function
48
  def auto_save():
49
- song_name = st.session_state['song_name']
50
- artist_name = st.session_state['artist_name']
51
- chord_sheet = st.session_state['chord_sheet']
52
 
53
- if song_name and artist_name:
54
- filename = f"{song_name} by {artist_name}.txt".replace(" ", "_")
55
  with open(filename, "w") as file:
56
- file.write(chord_sheet)
57
-
58
- # Display the character count below the text area
 
 
59
  st.session_state['char_count'] = len(chord_sheet)
 
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
 
67
  col1, col2 = st.columns([4, 1])
 
68
  with col1:
69
- song_name_input = st.text_input("🎵 Song Name", key='song_name')
70
- artist_name_input = st.text_input("🎤 Artist Name", key='artist_name')
 
 
 
 
 
 
 
71
 
72
- # Load chord sheet from selected file into the text area
73
- if selected_file:
74
- with open(selected_file, "r") as file:
75
- chord_sheet_input = file.read()
76
- st.session_state['chord_sheet'] = chord_sheet_input
77
- chord_sheet_area = st.text_area("Chord Sheet", value=chord_sheet_input, height=300, key='chord_sheet', on_change=auto_save)
78
- else:
79
- chord_sheet_area = st.text_area("Chord Sheet", height=300, key='chord_sheet', on_change=auto_save)
80
-
81
- # Display the character count below the text area
82
  char_count_msg = f"Character Count: {st.session_state.get('char_count', 0)}"
83
  st.write(char_count_msg)
84
 
85
  # Save functionality
86
  if st.button("💾 Save", key="save_song"):
87
  if song_name_input and artist_name_input:
88
- filename = f"{song_name_input} by {artist_name_input}.txt".replace(" ", "_")
89
  with open(filename, "w") as file:
90
  file.write(chord_sheet_area)
91
- st.success("Chord sheet saved.")
92
  else:
93
  st.error("Both Song Name and Artist Name are required.")
94
 
@@ -116,6 +132,11 @@ def main():
116
  st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})")
117
  st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})")
118
 
 
 
 
 
 
119
 
120
  if __name__ == '__main__':
121
  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 # Load the content into session state
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
+ # Instead of directly modifying the session state, return the chord sheet content
50
+ return chord_sheet
51
+
52
+ def song_update():
53
+ if 'selected_file' in st.session_state:
54
+ song_name, artist_name = parse_filename(st.session_state.selected_file)
55
+ st.session_state.song_name = song_name
56
+ st.session_state.artist_name = artist_name
57
+
58
+ def parse_filename(filename):
59
+ base_name = os.path.splitext(filename)[0]
60
+ song_name, artist_name = base_name.split(' by ')
61
+ return song_name.replace("_", " "), artist_name.replace("_", " ")
62
 
 
63
  def auto_save():
64
+ song_name = st.session_state.get('song_name', '')
65
+ artist_name = st.session_state.get('artist_name', '')
66
+ chord_sheet = st.session_state.get('chord_sheet', '')
67
 
68
+ if song_name and artist_name and chord_sheet:
69
+ filename = song_name + " by " + artist_name + ".txt"
70
  with open(filename, "w") as file:
71
+
72
+
73
+ chord_sheet_text = st.session_state.get('chord_sheet', '')
74
+ file.write(chord_sheet_text)
75
+
76
  st.session_state['char_count'] = len(chord_sheet)
77
+ st.success(f"Auto-saved to {filename}")
78
 
79
  def main():
80
  st.title('🎵 Song Files')
81
  with st.expander("Select Song File", expanded=True):
82
  all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
83
+ selected_file = st.selectbox("Choose a file", all_files, on_change=song_update, key='selected_file')
84
 
85
  col1, col2 = st.columns([4, 1])
86
+
87
  with col1:
88
+ song_name_input = st.text_input("🎵 Song Name", key='song_name', on_change=auto_save)
89
+ artist_name_input = st.text_input("🎤 Artist Name", 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
+ # Now create the text area widget with the updated session state
96
+ chord_sheet_area = st.text_area("Chord Sheet", value=st.session_state.get('chord_sheet', ''), height=300, key='chord_sheet', on_change=auto_save)
97
 
 
 
 
 
 
 
 
 
 
 
98
  char_count_msg = f"Character Count: {st.session_state.get('char_count', 0)}"
99
  st.write(char_count_msg)
100
 
101
  # Save functionality
102
  if st.button("💾 Save", key="save_song"):
103
  if song_name_input and artist_name_input:
104
+ filename = song_name_input + " by " + artist_name_input + ".txt"
105
  with open(filename, "w") as file:
106
  file.write(chord_sheet_area)
107
+ st.success("Chord sheet saved to file: " + filename)
108
  else:
109
  st.error("Both Song Name and Artist Name are required.")
110
 
 
132
  st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})")
133
  st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})")
134
 
135
+
136
+ # Load chord sheet from selected file into the text area
137
+ if 'selected_file' in st.session_state and st.session_state.selected_file:
138
+ load_song_file(st.session_state.selected_file)
139
+
140
 
141
  if __name__ == '__main__':
142
  main()