awacke1 commited on
Commit
10cda6f
·
verified ·
1 Parent(s): 5edd789

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -61
app.py CHANGED
@@ -7,6 +7,12 @@ from gradio_client import Client
7
  import json
8
  from datetime import datetime
9
 
 
 
 
 
 
 
10
  # Define the markdown variables
11
  Boxing_and_MMA_Commentary_and_Knowledge = """
12
  # Boxing and UFC Study of 1971 - 2024 The Greatest Fights History
@@ -95,7 +101,7 @@ def display_terms_with_links(terms):
95
  }
96
  for term in terms:
97
  links_md = ' '.join([f"[{emoji}]({url(term)})" for emoji, url in search_urls.items()])
98
- st.markdown(f"**{term}** {links_md}", unsafe_allow_html=True)
99
 
100
  # Function to perform AI lookup using Gradio client
101
  def perform_ai_lookup(query):
@@ -167,53 +173,46 @@ def file_management_sidebar():
167
  md_files.sort()
168
 
169
  if md_files:
170
- selected_file = st.sidebar.selectbox("Select a markdown file to view/edit", md_files)
171
-
172
- # Navigation buttons
173
- file_index = md_files.index(selected_file)
174
- col1, col2 = st.sidebar.columns([1,1])
175
- if col1.button("Previous"):
176
- if file_index > 0:
177
- selected_file = md_files[file_index -1]
178
- st.experimental_set_query_params(selected_file=selected_file)
179
- st.experimental_rerun()
180
- if col2.button("Next"):
181
- if file_index < len(md_files) -1:
182
- selected_file = md_files[file_index +1]
183
- st.experimental_set_query_params(selected_file=selected_file)
184
- st.experimental_rerun()
185
-
186
- # Load file content
187
- with open(selected_file, 'r', encoding='utf-8') as f:
188
- file_content = f.read()
189
-
190
- # Tabs for Markdown View and Code Editor
191
- tab1, tab2 = st.tabs(["Markdown View", "Code Editor"])
192
-
193
- with tab1:
194
- st.markdown(f"### {selected_file}")
195
- st.markdown(file_content)
196
-
197
- with tab2:
198
- edited_content = st.text_area("Edit the markdown content", file_content, height=400)
199
- if st.button("Save Changes"):
200
- with open(selected_file, 'w', encoding='utf-8') as f:
201
- f.write(edited_content)
202
- st.success(f"Changes saved to {selected_file}")
203
- st.experimental_rerun()
204
  else:
205
  st.sidebar.write("No markdown files found.")
206
-
207
- # Option to create a new markdown file
208
- if st.sidebar.button("Create New Markdown File"):
209
- # Generate automatic filename
210
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
211
- new_filename = f"note_{timestamp}.md"
212
- with open(new_filename, 'w', encoding='utf-8') as f:
213
- f.write("# New Markdown File\n")
214
- st.sidebar.success(f"Created new file: {new_filename}")
215
- st.experimental_set_query_params(selected_file=new_filename)
216
- st.experimental_rerun()
217
 
218
  # Main application logic
219
  def main():
@@ -232,24 +231,70 @@ def main():
232
  display_terms_with_links(all_terms)
233
 
234
  # Process 'q' query parameter from the URL
235
- query_params = st.experimental_get_query_params()
236
- if 'q' in query_params:
237
- search_query = query_params['q'][0]
238
- st.write(f"### Search query received: {search_query}")
239
- # Perform AI lookup
240
- ai_result = perform_ai_lookup(search_query)
241
- # Extract URLs from AI result
242
- markdown_text = extract_urls(ai_result)
243
- st.markdown("## Extracted URLs")
244
- st.markdown(markdown_text)
245
- # Save the result as markdown file
246
- filename = generate_filename("AI_Result", search_query)
247
- with open(filename, 'w', encoding='utf-8') as f:
248
- f.write(markdown_text)
249
- st.write(f"Generated file **{filename}** with AI lookup results.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
  # File management sidebar
252
  file_management_sidebar()
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
  if __name__ == "__main__":
255
  main()
 
7
  import json
8
  from datetime import datetime
9
 
10
+ # Initialize session state variables
11
+ if 'selected_file' not in st.session_state:
12
+ st.session_state.selected_file = None
13
+ if 'view_mode' not in st.session_state:
14
+ st.session_state.view_mode = 'view'
15
+
16
  # Define the markdown variables
17
  Boxing_and_MMA_Commentary_and_Knowledge = """
18
  # Boxing and UFC Study of 1971 - 2024 The Greatest Fights History
 
101
  }
102
  for term in terms:
103
  links_md = ' '.join([f"[{emoji}]({url(term)})" for emoji, url in search_urls.items()])
104
+ st.markdown(f"- **{term}** {links_md}", unsafe_allow_html=True)
105
 
106
  # Function to perform AI lookup using Gradio client
107
  def perform_ai_lookup(query):
 
173
  md_files.sort()
174
 
175
  if md_files:
176
+ st.sidebar.markdown("### Markdown Files")
177
+ for idx, file in enumerate(md_files):
178
+ # Create a unique key for each file
179
+ key_base = f"file_{idx}_{file}"
180
+ col1, col2, col3 = st.sidebar.columns([6, 1, 1])
181
+ with col1:
182
+ st.write(file)
183
+ with col2:
184
+ if st.sidebar.button("📄", key=f"view_{key_base}"):
185
+ st.session_state.selected_file = file
186
+ st.session_state.view_mode = 'view'
187
+ st.experimental_rerun()
188
+ with col3:
189
+ if st.sidebar.button("✏️", key=f"edit_{key_base}"):
190
+ st.session_state.selected_file = file
191
+ st.session_state.view_mode = 'edit'
192
+ st.experimental_rerun()
193
+ # Option to create a new markdown file
194
+ if st.sidebar.button("Create New Markdown File"):
195
+ # Generate automatic filename
196
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
197
+ new_filename = f"note_{timestamp}.md"
198
+ with open(new_filename, 'w', encoding='utf-8') as f:
199
+ f.write("# New Markdown File\n")
200
+ st.sidebar.success(f"Created new file: {new_filename}")
201
+ st.session_state.selected_file = new_filename
202
+ st.session_state.view_mode = 'edit'
203
+ st.experimental_rerun()
 
 
 
 
 
 
204
  else:
205
  st.sidebar.write("No markdown files found.")
206
+ if st.sidebar.button("Create New Markdown File"):
207
+ # Generate automatic filename
208
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
209
+ new_filename = f"note_{timestamp}.md"
210
+ with open(new_filename, 'w', encoding='utf-8') as f:
211
+ f.write("# New Markdown File\n")
212
+ st.sidebar.success(f"Created new file: {new_filename}")
213
+ st.session_state.selected_file = new_filename
214
+ st.session_state.view_mode = 'edit'
215
+ st.experimental_rerun()
 
216
 
217
  # Main application logic
218
  def main():
 
231
  display_terms_with_links(all_terms)
232
 
233
  # Process 'q' query parameter from the URL
234
+ try:
235
+ query_params = st.query_params
236
+ query_list = (query_params.get('q') or query_params.get('query') or [''])
237
+ if query_list:
238
+ search_query = query_list[0]
239
+ if len(search_query) > 1:
240
+ st.write(f"### Search query received: {search_query}")
241
+ # Perform AI lookup
242
+ ai_result = perform_ai_lookup(search_query)
243
+ # Extract URLs from AI result
244
+ markdown_text = extract_urls(ai_result)
245
+ st.markdown("## Extracted URLs")
246
+ st.markdown(markdown_text)
247
+ # Save the result as markdown file
248
+ filename = generate_filename("AI_Result", search_query)
249
+ with open(filename, 'w', encoding='utf-8') as f:
250
+ f.write(markdown_text)
251
+ st.write(f"Generated file **{filename}** with AI lookup results.")
252
+ # Update file management sidebar
253
+ st.experimental_rerun()
254
+ except Exception as e:
255
+ st.write(f"An error occurred while processing query parameters: {e}")
256
+
257
+ # Handle 'action' and 'query' parameters
258
+ if 'action' in st.query_params:
259
+ action_list = st.query_params['action']
260
+ if action_list:
261
+ action = action_list[0]
262
+ if action == 'show_message':
263
+ st.success("Showing a message because 'action=show_message' was found in the URL.")
264
+ elif action == 'clear':
265
+ # Clear query parameters
266
+ st.experimental_set_query_params()
267
+ st.experimental_rerun()
268
+ if 'query' in st.query_params:
269
+ query_list = st.query_params['query']
270
+ if query_list:
271
+ query = query_list[0]
272
+ # Display content or image based on the query
273
+ st.write(f"Displaying content for query: {query}")
274
+ # Implement your display logic here
275
 
276
  # File management sidebar
277
  file_management_sidebar()
278
 
279
+ # Display the selected file
280
+ if 'selected_file' in st.session_state:
281
+ selected_file = st.session_state.selected_file
282
+ view_mode = st.session_state.get('view_mode', 'view')
283
+ if view_mode == 'view':
284
+ st.markdown(f"### Viewing {selected_file}")
285
+ with open(selected_file, 'r', encoding='utf-8') as f:
286
+ file_content = f.read()
287
+ st.markdown(file_content)
288
+ elif view_mode == 'edit':
289
+ st.markdown(f"### Editing {selected_file}")
290
+ with open(selected_file, 'r', encoding='utf-8') as f:
291
+ file_content = f.read()
292
+ edited_content = st.text_area("Edit the markdown content", file_content, height=400)
293
+ if st.button("Save Changes"):
294
+ with open(selected_file, 'w', encoding='utf-8') as f:
295
+ f.write(edited_content)
296
+ st.success(f"Changes saved to {selected_file}")
297
+ st.experimental_rerun()
298
+
299
  if __name__ == "__main__":
300
  main()