awacke1 commited on
Commit
d412991
1 Parent(s): d938531

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -12
app.py CHANGED
@@ -1,5 +1,11 @@
1
  import streamlit as st
 
 
 
 
 
2
 
 
3
  Boxing_and_MMA_Commentary_and_Knowledge = """
4
  # Boxing and UFC Study of 1971 - 2024 The Greatest Fights History
5
 
@@ -26,20 +32,12 @@ Boxing_and_MMA_Commentary_and_Knowledge = """
26
  5. Connor McGregor
27
  6. Leg Breaking - Shin calcification and breaking baseball bats
28
 
29
-
30
  # References:
31
  1. Joe Rogan - Interview #2219
32
  2. Donald J Trump
33
  """
34
 
35
- st.markdown("""
36
-
37
- {Boxing_and_MMA_Commentary_and_Knowledge}
38
-
39
- """)
40
-
41
-
42
- Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds="""
43
 
44
  # Multiplayer Simulated Worlds
45
 
@@ -75,6 +73,146 @@ Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds="""
75
 
76
  """
77
 
78
- st.markdown(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
79
-
80
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ import glob
4
+ import re
5
+ import datetime
6
+ from urllib.parse import quote
7
 
8
+ # Define the markdown variables
9
  Boxing_and_MMA_Commentary_and_Knowledge = """
10
  # Boxing and UFC Study of 1971 - 2024 The Greatest Fights History
11
 
 
32
  5. Connor McGregor
33
  6. Leg Breaking - Shin calcification and breaking baseball bats
34
 
 
35
  # References:
36
  1. Joe Rogan - Interview #2219
37
  2. Donald J Trump
38
  """
39
 
40
+ Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds = """
 
 
 
 
 
 
 
41
 
42
  # Multiplayer Simulated Worlds
43
 
 
73
 
74
  """
75
 
76
+ # Function to parse markdown text and create external links for terms
77
+ def display_external_links(term):
78
+ search_urls = {
79
+ "ArXiv": lambda k: f"https://arxiv.org/search/?query={quote(k)}",
80
+ "Wikipedia": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
81
+ "Google": lambda k: f"https://www.google.com/search?q={quote(k)}",
82
+ "YouTube": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
83
+ }
84
+ links_md = ' | '.join([f"[{name}]({url(term)})" for name, url in search_urls.items()])
85
+ st.markdown(f"- **{term}** - {links_md}")
86
+
87
+ # Function to parse markdown text and extract terms
88
+ def extract_terms(markdown_text):
89
+ # Split text into lines
90
+ lines = markdown_text.strip().split('\n')
91
+ terms = []
92
+ for line in lines:
93
+ # Remove markdown special characters
94
+ line = re.sub(r'^[#*\->\d\.\s]+', '', line).strip()
95
+ if line:
96
+ terms.append(line)
97
+ return terms
98
+
99
+ # Function to create internal links with query parameters
100
+ def display_internal_links(term):
101
+ app_url = st.experimental_get_query_params()
102
+ # Reconstruct the app URL without parameters
103
+ base_url = st.request.host_url
104
+ link = f"{base_url}?q={quote(term)}"
105
+ st.markdown(f"- [{term}]({link})")
106
+
107
+ # Function to automatically generate filenames based on date and content
108
+ def generate_filename(prefix, content):
109
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
110
+ safe_content = re.sub(r'\W+', '_', content[:50])
111
+ filename = f"{prefix}_{timestamp}_{safe_content}.md"
112
+ return filename
113
+
114
+ # Sidebar for file management
115
+ def file_management_sidebar():
116
+ st.sidebar.title("📁 File Management")
117
+
118
+ # Get list of .md files excluding README.md
119
+ md_files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md']
120
+ md_files.sort()
121
+
122
+ if md_files:
123
+ selected_file = st.sidebar.selectbox("Select a markdown file to view/edit", md_files)
124
+
125
+ # Navigation buttons
126
+ file_index = md_files.index(selected_file)
127
+ col1, col2 = st.sidebar.columns([1,1])
128
+ if col1.button("Previous"):
129
+ if file_index > 0:
130
+ selected_file = md_files[file_index -1]
131
+ st.experimental_set_query_params(selected_file=selected_file)
132
+ st.experimental_rerun()
133
+ if col2.button("Next"):
134
+ if file_index < len(md_files) -1:
135
+ selected_file = md_files[file_index +1]
136
+ st.experimental_set_query_params(selected_file=selected_file)
137
+ st.experimental_rerun()
138
+
139
+ # Load file content
140
+ with open(selected_file, 'r', encoding='utf-8') as f:
141
+ file_content = f.read()
142
+
143
+ # Tabs for Markdown View and Code Editor
144
+ tab1, tab2 = st.tabs(["Markdown View", "Code Editor"])
145
+
146
+ with tab1:
147
+ st.markdown(f"### {selected_file}")
148
+ st.markdown(file_content)
149
+
150
+ with tab2:
151
+ edited_content = st.text_area("Edit the markdown content", file_content, height=400)
152
+ if st.button("Save Changes"):
153
+ with open(selected_file, 'w', encoding='utf-8') as f:
154
+ f.write(edited_content)
155
+ st.success(f"Changes saved to {selected_file}")
156
+ st.experimental_rerun()
157
+ else:
158
+ st.sidebar.write("No markdown files found.")
159
+
160
+ # Option to create a new markdown file
161
+ if st.sidebar.button("Create New Markdown File"):
162
+ # Generate automatic filename
163
+ timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
164
+ new_filename = f"note_{timestamp}.md"
165
+ with open(new_filename, 'w', encoding='utf-8') as f:
166
+ f.write("# New Markdown File\n")
167
+ st.sidebar.success(f"Created new file: {new_filename}")
168
+ st.experimental_set_query_params(selected_file=new_filename)
169
+ st.experimental_rerun()
170
+
171
+ # Main application logic
172
+ def main():
173
+ st.title("Markdown Content with Links and File Management")
174
+
175
+ # Display the original markdown content
176
+ st.markdown("## Original Markdown Content")
177
+ st.markdown(Boxing_and_MMA_Commentary_and_Knowledge)
178
+ st.markdown(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
179
+
180
+ # Parse and display external links
181
+ st.markdown("## External Links Generated from Markdown Content")
182
+ terms = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge)
183
+ for term in terms:
184
+ display_external_links(term)
185
+
186
+ terms = extract_terms(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
187
+ for term in terms:
188
+ display_external_links(term)
189
+
190
+ # Parse and display internal links
191
+ st.markdown("## Internal Links Generated from Markdown Content")
192
+ terms = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge)
193
+ for term in terms:
194
+ display_internal_links(term)
195
+
196
+ terms = extract_terms(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds)
197
+ for term in terms:
198
+ display_internal_links(term)
199
+
200
+ # Process 'q' query parameter from the URL
201
+ query_params = st.experimental_get_query_params()
202
+ if 'q' in query_params:
203
+ search_query = query_params['q'][0]
204
+ st.write(f"### Search query received: {search_query}")
205
+ # Here you can implement your search logic
206
+ # For demonstration, we'll create a markdown file with the search query
207
+ filename = generate_filename("search", search_query)
208
+ content = f"# Search Results for '{search_query}'\n\n"
209
+ content += f"Here are the results for your search query: '{search_query}'.\n"
210
+ with open(filename, 'w', encoding='utf-8') as f:
211
+ f.write(content)
212
+ st.write(f"Generated file **{filename}** with search results.")
213
+
214
+ # File management sidebar
215
+ file_management_sidebar()
216
+
217
+ if __name__ == "__main__":
218
+ main()