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

Update app.py

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