awacke1 commited on
Commit
d8070db
1 Parent(s): d429406

Update backup5.sidebarrefactorapp.py

Browse files
Files changed (1) hide show
  1. backup5.sidebarrefactorapp.py +71 -31
backup5.sidebarrefactorapp.py CHANGED
@@ -89,6 +89,13 @@ def get_display_name(filename):
89
  return match.group(1)
90
  return filename
91
 
 
 
 
 
 
 
 
92
  def sanitize_filename(text):
93
  """Create a safe filename from text while preserving spaces."""
94
  # First replace unsafe characters with spaces
@@ -117,12 +124,31 @@ def generate_timestamp_filename(query):
117
 
118
  return filename
119
 
120
- def save_ai_interaction(query, ai_result):
 
 
 
 
 
 
 
 
 
121
  """Save AI interaction to a markdown file with new filename format."""
122
  filename = generate_timestamp_filename(query)
123
 
124
- # Format the content
125
- content = f"""# Query: {query}
 
 
 
 
 
 
 
 
 
 
126
 
127
  ## AI Response
128
  {ai_result}
@@ -143,7 +169,8 @@ def get_file_download_link(file_path):
143
  with open(file_path, 'r', encoding='utf-8') as f:
144
  content = f.read()
145
  b64 = base64.b64encode(content.encode()).decode()
146
- return f"data:text/markdown;base64,{b64}"
 
147
  except Exception as e:
148
  st.error(f"Error creating download link: {e}")
149
  return None
@@ -201,24 +228,25 @@ def display_file_content(file_path):
201
  with open(file_path, 'r', encoding='utf-8') as f:
202
  content = f.read()
203
 
204
- # Display as code with line numbers
205
- st.code(content, line_numbers=True)
206
-
207
- # Edit functionality
208
- edited_content = st.text_area(
209
- "Edit content",
210
- content,
211
- height=400,
212
- key=f"edit_{os.path.basename(file_path)}"
213
- )
214
-
215
- if st.button("Save Changes", key=f"save_{os.path.basename(file_path)}"):
216
- try:
217
- with open(file_path, 'w', encoding='utf-8') as f:
218
- f.write(edited_content)
219
- st.success(f"Successfully saved changes to {file_path}")
220
- except Exception as e:
221
- st.error(f"Error saving changes: {e}")
 
222
  except Exception as e:
223
  st.error(f"Error reading file: {e}")
224
 
@@ -236,17 +264,16 @@ def file_management_sidebar():
236
  for idx, file in enumerate(md_files):
237
  st.sidebar.markdown("---") # Separator between files
238
 
239
- # Display filename
240
- st.sidebar.text(file)
241
 
242
  # Display download link with simplified text
243
- display_name = get_display_name(file)
244
  download_link = get_file_download_link(file)
245
  if download_link:
246
- st.sidebar.markdown(f"📥 [{display_name}]({download_link})", unsafe_allow_html=True)
247
 
248
  # Action buttons in a row
249
- col1, col2, col3 = st.sidebar.columns(3)
250
 
251
  with col1:
252
  if st.button("📄 View", key=f"view_{idx}"):
@@ -272,7 +299,7 @@ def file_management_sidebar():
272
 
273
  # Perform AI lookup and save results
274
  ai_result = perform_ai_lookup(full_prompt)
275
- saved_file = save_ai_interaction(f"Rerun of {file}", ai_result)
276
 
277
  if saved_file:
278
  st.success(f"Created fun version in {saved_file}")
@@ -281,6 +308,14 @@ def file_management_sidebar():
281
 
282
  except Exception as e:
283
  st.error(f"Error during rerun: {e}")
 
 
 
 
 
 
 
 
284
 
285
  st.sidebar.markdown("---")
286
  # Option to create a new markdown file
@@ -331,10 +366,15 @@ def main():
331
  # Second priority: Display selected file content if any
332
  if st.session_state.selected_file:
333
  show_initial_content = False # Hide initial content when showing file content
334
- st.markdown(f"### Current File: {st.session_state.selected_file}")
335
- display_file_content(st.session_state.selected_file)
 
 
 
 
 
336
 
337
- # Show initial content: Either when first landing or when no interactive elements are active
338
  if show_initial_content:
339
  # First show the clickable terms with links
340
  terms1 = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge)
 
89
  return match.group(1)
90
  return filename
91
 
92
+ def get_time_display(filename):
93
+ """Extract just the time portion from the filename."""
94
+ time_match = re.match(r'(\d{2}\d{2}[AP]M)', filename)
95
+ if time_match:
96
+ return time_match.group(1)
97
+ return filename
98
+
99
  def sanitize_filename(text):
100
  """Create a safe filename from text while preserving spaces."""
101
  # First replace unsafe characters with spaces
 
124
 
125
  return filename
126
 
127
+ def delete_file(file_path):
128
+ """Delete a file and return success status."""
129
+ try:
130
+ os.remove(file_path)
131
+ return True
132
+ except Exception as e:
133
+ st.error(f"Error deleting file: {e}")
134
+ return False
135
+
136
+ def save_ai_interaction(query, ai_result, is_rerun=False):
137
  """Save AI interaction to a markdown file with new filename format."""
138
  filename = generate_timestamp_filename(query)
139
 
140
+ # Format the content differently for rerun vs normal query
141
+ if is_rerun:
142
+ content = f"""# Rerun Query
143
+ Original file content used for rerun:
144
+
145
+ {query}
146
+
147
+ # AI Response (Fun Version)
148
+ {ai_result}
149
+ """
150
+ else:
151
+ content = f"""# Query: {query}
152
 
153
  ## AI Response
154
  {ai_result}
 
169
  with open(file_path, 'r', encoding='utf-8') as f:
170
  content = f.read()
171
  b64 = base64.b64encode(content.encode()).decode()
172
+ filename = os.path.basename(file_path)
173
+ return f'<a href="data:text/markdown;base64,{b64}" download="{filename}">{get_display_name(filename)}</a>'
174
  except Exception as e:
175
  st.error(f"Error creating download link: {e}")
176
  return None
 
228
  with open(file_path, 'r', encoding='utf-8') as f:
229
  content = f.read()
230
 
231
+ if st.session_state.view_mode == 'view':
232
+ # Display as markdown when viewing
233
+ st.markdown(content)
234
+ else:
235
+ # Edit functionality
236
+ edited_content = st.text_area(
237
+ "Edit content",
238
+ content,
239
+ height=400,
240
+ key=f"edit_{os.path.basename(file_path)}"
241
+ )
242
+
243
+ if st.button("Save Changes", key=f"save_{os.path.basename(file_path)}"):
244
+ try:
245
+ with open(file_path, 'w', encoding='utf-8') as f:
246
+ f.write(edited_content)
247
+ st.success(f"Successfully saved changes to {file_path}")
248
+ except Exception as e:
249
+ st.error(f"Error saving changes: {e}")
250
  except Exception as e:
251
  st.error(f"Error reading file: {e}")
252
 
 
264
  for idx, file in enumerate(md_files):
265
  st.sidebar.markdown("---") # Separator between files
266
 
267
+ # Display time
268
+ st.sidebar.text(get_time_display(file))
269
 
270
  # Display download link with simplified text
 
271
  download_link = get_file_download_link(file)
272
  if download_link:
273
+ st.sidebar.markdown(download_link, unsafe_allow_html=True)
274
 
275
  # Action buttons in a row
276
+ col1, col2, col3, col4 = st.sidebar.columns(4)
277
 
278
  with col1:
279
  if st.button("📄 View", key=f"view_{idx}"):
 
299
 
300
  # Perform AI lookup and save results
301
  ai_result = perform_ai_lookup(full_prompt)
302
+ saved_file = save_ai_interaction(content, ai_result, is_rerun=True)
303
 
304
  if saved_file:
305
  st.success(f"Created fun version in {saved_file}")
 
308
 
309
  except Exception as e:
310
  st.error(f"Error during rerun: {e}")
311
+
312
+ with col4:
313
+ if st.button("🗑️ Delete", key=f"delete_{idx}"):
314
+ if delete_file(file):
315
+ st.success(f"Deleted {file}")
316
+ st.rerun()
317
+ else:
318
+ st.error(f"Failed to delete {file}")
319
 
320
  st.sidebar.markdown("---")
321
  # Option to create a new markdown file
 
366
  # Second priority: Display selected file content if any
367
  if st.session_state.selected_file:
368
  show_initial_content = False # Hide initial content when showing file content
369
+ if os.path.exists(st.session_state.selected_file):
370
+ st.markdown(f"### Current File: {st.session_state.selected_file}")
371
+ display_file_content(st.session_state.selected_file)
372
+ else:
373
+ st.error("Selected file no longer exists.")
374
+ st.session_state.selected_file = None
375
+ st.rerun()
376
 
377
+ # Show initial content: Either when first landing or when no interactive elements are active
378
  if show_initial_content:
379
  # First show the clickable terms with links
380
  terms1 = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge)