Spaces:
Running
Running
import streamlit as st | |
import os | |
import glob | |
import re | |
import datetime | |
from urllib.parse import quote | |
# Define the markdown variables | |
Boxing_and_MMA_Commentary_and_Knowledge = """ | |
# Boxing and UFC Study of 1971 - 2024 The Greatest Fights History | |
1. In Boxing, the most heart breaking fight in Boxing was the Boom Boom Mancini fight with Duku Kim. | |
2. After changes to Boxing made it more safe due to the heart break. | |
3. Rehydration of the brain after weight ins loss preparation for a match is life saving change. | |
4. Fighting went from 15 rounds to 12. | |
# UFC By Contrast.. | |
1. 5 Rounds of 5 Minutes each. | |
2. Greatest UFC Fighters: | |
- Jon Jones could be the greatest of all time (GOAT) since he never lost. | |
- George St. Pierre | |
- BJ Penn | |
- Anderson Silva | |
- Mighty Mouse MMA's heart at 125 pounds | |
- Kabib retired 29 and 0 | |
- Fedor Milliano | |
- Alex Pereira | |
- James Tony | |
- Randy Couture | |
3. You have to Judge them in their Championship Peak | |
4. Chris Weidman | |
5. Connor McGregor | |
6. Leg Breaking - Shin calcification and breaking baseball bats | |
# References: | |
1. Joe Rogan - Interview #2219 | |
2. Donald J Trump | |
""" | |
Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds = """ | |
# Multiplayer Simulated Worlds | |
1. 7 Days To Die PC | |
2. ARK: Survival Evolved PC | |
3. Arma 3 PC | |
4. Atlas PC | |
5. Conan Exiles PC | |
6. Craftopia PC | |
7. DayZ PC | |
8. Eco - Global Survival PC | |
9. Empyrion - Galactic Survival PC | |
10. Factorio PC | |
11. Farming Simulator 19 PC | |
12. Crossplay | |
13. Farming Simulator 22 | |
14. Last Oasis PC | |
15. Last Oasis Classic PC | |
16. Minecraft (Vanilla) PC | |
17. Crossplay | |
18. Path of Titans | |
19. Rust PC | |
20. SCP: Secret Laboratory PC | |
21. SCUM PC | |
22. Satisfactory PC | |
23. Satisfactory (Experimental) PC | |
24. Crossplay | |
25. Space Engineers | |
26. Terraria (tShock & Vanilla) PC | |
27. The Forest PC | |
28. Crossplay | |
29. Valheim | |
""" | |
# Function to parse markdown text and create external links for terms | |
def display_external_links(term): | |
search_urls = { | |
"ArXiv": lambda k: f"https://arxiv.org/search/?query={quote(k)}", | |
"Wikipedia": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}", | |
"Google": lambda k: f"https://www.google.com/search?q={quote(k)}", | |
"YouTube": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}", | |
} | |
links_md = ' | '.join([f"[{name}]({url(term)})" for name, url in search_urls.items()]) | |
st.markdown(f"- **{term}** - {links_md}") | |
# Function to parse markdown text and extract terms | |
def extract_terms(markdown_text): | |
# Split text into lines | |
lines = markdown_text.strip().split('\n') | |
terms = [] | |
for line in lines: | |
# Remove markdown special characters | |
line = re.sub(r'^[#*\->\d\.\s]+', '', line).strip() | |
if line: | |
terms.append(line) | |
return terms | |
# Function to create internal links with query parameters | |
def display_internal_links(term): | |
app_url = st.experimental_get_query_params() | |
# Reconstruct the app URL without parameters | |
base_url = st.request.host_url | |
link = f"{base_url}?q={quote(term)}" | |
st.markdown(f"- [{term}]({link})") | |
# Function to automatically generate filenames based on date and content | |
def generate_filename(prefix, content): | |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
safe_content = re.sub(r'\W+', '_', content[:50]) | |
filename = f"{prefix}_{timestamp}_{safe_content}.md" | |
return filename | |
# Sidebar for file management | |
def file_management_sidebar(): | |
st.sidebar.title("📁 File Management") | |
# Get list of .md files excluding README.md | |
md_files = [file for file in glob.glob("*.md") if os.path.basename(file).lower() != 'readme.md'] | |
md_files.sort() | |
if md_files: | |
selected_file = st.sidebar.selectbox("Select a markdown file to view/edit", md_files) | |
# Navigation buttons | |
file_index = md_files.index(selected_file) | |
col1, col2 = st.sidebar.columns([1,1]) | |
if col1.button("Previous"): | |
if file_index > 0: | |
selected_file = md_files[file_index -1] | |
st.experimental_set_query_params(selected_file=selected_file) | |
st.experimental_rerun() | |
if col2.button("Next"): | |
if file_index < len(md_files) -1: | |
selected_file = md_files[file_index +1] | |
st.experimental_set_query_params(selected_file=selected_file) | |
st.experimental_rerun() | |
# Load file content | |
with open(selected_file, 'r', encoding='utf-8') as f: | |
file_content = f.read() | |
# Tabs for Markdown View and Code Editor | |
tab1, tab2 = st.tabs(["Markdown View", "Code Editor"]) | |
with tab1: | |
st.markdown(f"### {selected_file}") | |
st.markdown(file_content) | |
with tab2: | |
edited_content = st.text_area("Edit the markdown content", file_content, height=400) | |
if st.button("Save Changes"): | |
with open(selected_file, 'w', encoding='utf-8') as f: | |
f.write(edited_content) | |
st.success(f"Changes saved to {selected_file}") | |
st.experimental_rerun() | |
else: | |
st.sidebar.write("No markdown files found.") | |
# Option to create a new markdown file | |
if st.sidebar.button("Create New Markdown File"): | |
# Generate automatic filename | |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | |
new_filename = f"note_{timestamp}.md" | |
with open(new_filename, 'w', encoding='utf-8') as f: | |
f.write("# New Markdown File\n") | |
st.sidebar.success(f"Created new file: {new_filename}") | |
st.experimental_set_query_params(selected_file=new_filename) | |
st.experimental_rerun() | |
# Main application logic | |
def main(): | |
st.title("Markdown Content with Links and File Management") | |
# Display the original markdown content | |
st.markdown("## Original Markdown Content") | |
st.markdown(Boxing_and_MMA_Commentary_and_Knowledge) | |
st.markdown(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds) | |
# Parse and display external links | |
st.markdown("## External Links Generated from Markdown Content") | |
terms = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge) | |
for term in terms: | |
display_external_links(term) | |
terms = extract_terms(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds) | |
for term in terms: | |
display_external_links(term) | |
# Parse and display internal links | |
st.markdown("## Internal Links Generated from Markdown Content") | |
terms = extract_terms(Boxing_and_MMA_Commentary_and_Knowledge) | |
for term in terms: | |
display_internal_links(term) | |
terms = extract_terms(Multiplayer_Custom_Hosting_Game_Servers_For_Simulated_Worlds) | |
for term in terms: | |
display_internal_links(term) | |
# Process 'q' query parameter from the URL | |
query_params = st.experimental_get_query_params() | |
if 'q' in query_params: | |
search_query = query_params['q'][0] | |
st.write(f"### Search query received: {search_query}") | |
# Here you can implement your search logic | |
# For demonstration, we'll create a markdown file with the search query | |
filename = generate_filename("search", search_query) | |
content = f"# Search Results for '{search_query}'\n\n" | |
content += f"Here are the results for your search query: '{search_query}'.\n" | |
with open(filename, 'w', encoding='utf-8') as f: | |
f.write(content) | |
st.write(f"Generated file **{filename}** with search results.") | |
# File management sidebar | |
file_management_sidebar() | |
if __name__ == "__main__": | |
main() | |