awacke1 commited on
Commit
18f39f1
·
1 Parent(s): b415203

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -67,27 +67,37 @@ def get_download_link(file):
67
  return href
68
 
69
  def show_file_operations(file_path):
70
- st.write(f"File: {os.path.basename(file_path)}")
 
 
71
 
72
- # Edit button and text area for file content
73
- if st.button(f"✏️ Edit {os.path.basename(file_path)}"):
74
- with open(file_path, "r") as f:
75
- file_content = f.read()
76
- file_content = st.text_area("Edit the file content:", value=file_content, height=250)
 
 
 
 
 
77
 
78
  # Save button
79
  if st.button(f"💾 Save {os.path.basename(file_path)}"):
80
  with open(file_path, "w") as f:
81
- f.write(file_content)
82
  new_file_size = os.path.getsize(file_path)
83
  download_link = get_download_link(file_path)
84
  st.markdown(f"✅ File **{os.path.basename(file_path)}** saved! ([{download_link}]) - New size: {new_file_size} bytes", unsafe_allow_html=True)
 
85
 
86
  # Delete button
87
  if st.button(f"🗑️ Delete {os.path.basename(file_path)}"):
88
  os.remove(file_path)
89
  st.markdown(f"🎉 File {os.path.basename(file_path)} deleted!")
90
-
 
 
91
 
92
  def main():
93
  st.sidebar.title('Web Datasets Bulk Downloader')
 
67
  return href
68
 
69
  def show_file_operations(file_path):
70
+ # Unique keys for each file based on their path
71
+ edit_key = f"edit_{file_path}"
72
+ content_key = f"content_{file_path}"
73
 
74
+ # Start Edit operation
75
+ if st.button(f"✏️ Edit {os.path.basename(file_path)}", key=edit_key):
76
+ if edit_key not in st.session_state:
77
+ with open(file_path, "r") as f:
78
+ st.session_state[content_key] = f.read()
79
+ st.session_state[edit_key] = True
80
+
81
+ # Display text area for editing if in edit mode
82
+ if st.session_state.get(edit_key, False):
83
+ edited_content = st.text_area("Edit the file content:", value=st.session_state.get(content_key, ""), height=250, key=content_key)
84
 
85
  # Save button
86
  if st.button(f"💾 Save {os.path.basename(file_path)}"):
87
  with open(file_path, "w") as f:
88
+ f.write(edited_content)
89
  new_file_size = os.path.getsize(file_path)
90
  download_link = get_download_link(file_path)
91
  st.markdown(f"✅ File **{os.path.basename(file_path)}** saved! ([{download_link}]) - New size: {new_file_size} bytes", unsafe_allow_html=True)
92
+ st.session_state[edit_key] = False # Exit edit mode
93
 
94
  # Delete button
95
  if st.button(f"🗑️ Delete {os.path.basename(file_path)}"):
96
  os.remove(file_path)
97
  st.markdown(f"🎉 File {os.path.basename(file_path)} deleted!")
98
+ # Remove state variables related to the deleted file
99
+ st.session_state.pop(edit_key, None)
100
+ st.session_state.pop(content_key, None)
101
 
102
  def main():
103
  st.sidebar.title('Web Datasets Bulk Downloader')