Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,27 +15,28 @@ from urllib.parse import quote
|
|
| 15 |
import streamlit as st
|
| 16 |
import streamlit.components.v1 as components
|
| 17 |
|
| 18 |
-
# For demonstration,
|
| 19 |
from huggingface_hub import InferenceClient
|
| 20 |
|
| 21 |
# -----------------------------------------------------
|
| 22 |
-
#
|
| 23 |
-
# If not, create them and restart.
|
| 24 |
# -----------------------------------------------------
|
| 25 |
if not os.path.exists("MarkdownCode.md"):
|
| 26 |
with open("MarkdownCode.md", 'w', encoding='utf-8') as f:
|
| 27 |
f.write("# Default Markdown\nThis is a default Markdown file.")
|
| 28 |
-
st.
|
| 29 |
|
| 30 |
if not os.path.exists("MermaidCode.md"):
|
| 31 |
with open("MermaidCode.md", 'w', encoding='utf-8') as f:
|
| 32 |
-
|
| 33 |
-
flowchart LR
|
|
|
|
|
|
|
| 34 |
A[Default] --> B[Example]
|
| 35 |
-
click A "/?q=Default" _self
|
| 36 |
-
click B "/?q=Example" _self
|
| 37 |
""")
|
| 38 |
-
st.
|
| 39 |
|
| 40 |
# ----------------------------
|
| 41 |
# Placeholder data structures
|
|
@@ -206,8 +207,8 @@ def compare_and_delete_files(files):
|
|
| 206 |
|
| 207 |
def FileSidebar():
|
| 208 |
"""
|
| 209 |
-
Renders the file sidebar with
|
| 210 |
-
Excludes README.md from the
|
| 211 |
"""
|
| 212 |
all_files = glob.glob("*.md")
|
| 213 |
# Exclude README.md
|
|
@@ -216,7 +217,6 @@ def FileSidebar():
|
|
| 216 |
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 5]
|
| 217 |
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
|
| 218 |
|
| 219 |
-
# Buttons for "Delete All" and "Download"
|
| 220 |
Files1, Files2 = st.sidebar.columns(2)
|
| 221 |
with Files1:
|
| 222 |
if st.button("π Delete All"):
|
|
@@ -232,7 +232,6 @@ def FileSidebar():
|
|
| 232 |
file_name = ''
|
| 233 |
next_action = ''
|
| 234 |
|
| 235 |
-
# Each file row
|
| 236 |
for file in all_files:
|
| 237 |
col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1])
|
| 238 |
with col1:
|
|
@@ -263,7 +262,6 @@ def FileSidebar():
|
|
| 263 |
os.remove(file)
|
| 264 |
st.rerun()
|
| 265 |
|
| 266 |
-
# Duplicate detection
|
| 267 |
file_sizes = [get_file_size(file) for file in all_files]
|
| 268 |
previous_size = None
|
| 269 |
st.sidebar.title("File Operations")
|
|
@@ -283,7 +281,6 @@ def FileSidebar():
|
|
| 283 |
st.rerun()
|
| 284 |
previous_size = size
|
| 285 |
|
| 286 |
-
# If we have loaded something
|
| 287 |
if len(file_contents) > 0:
|
| 288 |
if next_action == 'open':
|
| 289 |
open1, open2 = st.columns([0.8, 0.2])
|
|
@@ -439,7 +436,6 @@ def get_all_query_params(key):
|
|
| 439 |
def clear_query_params():
|
| 440 |
st.query_params
|
| 441 |
|
| 442 |
-
|
| 443 |
def display_content_or_image(query):
|
| 444 |
for category, term_list in transhuman_glossary.items():
|
| 445 |
for term in term_list:
|
|
@@ -456,7 +452,7 @@ def display_content_or_image(query):
|
|
| 456 |
|
| 457 |
|
| 458 |
# ------------------------------------
|
| 459 |
-
# MERMAID DIAGRAM with
|
| 460 |
# ------------------------------------
|
| 461 |
def generate_mermaid_html(mermaid_code: str) -> str:
|
| 462 |
return f"""
|
|
@@ -503,25 +499,50 @@ def main():
|
|
| 503 |
st.sidebar.write("## Diagram Link Settings")
|
| 504 |
model_selected = st.sidebar.checkbox("Append ?model=1 to each link?")
|
| 505 |
|
| 506 |
-
# Load
|
| 507 |
-
# If the user empties them, they remain blank until re-saved
|
| 508 |
markdown_default = load_file("MarkdownCode.md")
|
| 509 |
mermaid_default = load_file("MermaidCode.md")
|
| 510 |
|
| 511 |
-
#
|
| 512 |
base_diagram = mermaid_default or ""
|
| 513 |
lines = base_diagram.strip().split("\n")
|
| 514 |
new_lines = []
|
| 515 |
for line in lines:
|
| 516 |
if "click " in line and '"/?' in line:
|
| 517 |
-
|
| 518 |
-
|
|
|
|
|
|
|
| 519 |
url = parts[1]
|
|
|
|
|
|
|
| 520 |
updated_url = append_model_param(url, model_selected)
|
| 521 |
-
|
| 522 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 523 |
else:
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
else:
|
| 526 |
new_lines.append(line)
|
| 527 |
mermaid_code = "\n".join(new_lines)
|
|
@@ -542,7 +563,6 @@ def main():
|
|
| 542 |
# --- Left: Markdown Editor
|
| 543 |
with left_col:
|
| 544 |
st.subheader("Markdown Side π")
|
| 545 |
-
# Load from session or from MarkdownCode.md
|
| 546 |
if "markdown_text" not in st.session_state:
|
| 547 |
st.session_state["markdown_text"] = markdown_default
|
| 548 |
|
|
@@ -553,7 +573,6 @@ def main():
|
|
| 553 |
)
|
| 554 |
st.session_state["markdown_text"] = markdown_text
|
| 555 |
|
| 556 |
-
# Button row
|
| 557 |
colA, colB, colC, colD = st.columns(4)
|
| 558 |
with colA:
|
| 559 |
if st.button("π Refresh"):
|
|
@@ -568,7 +587,6 @@ def main():
|
|
| 568 |
f.write(markdown_text)
|
| 569 |
st.success("Saved to MarkdownCode.md")
|
| 570 |
with colD:
|
| 571 |
-
# "Save As" with a text_input
|
| 572 |
md_filename = st.text_input("Filename for Markdown:", value="MarkdownCode.md", key="md_filename_key")
|
| 573 |
if st.button("πΎ Save As"):
|
| 574 |
with open(md_filename, 'w', encoding='utf-8') as f:
|
|
@@ -607,7 +625,6 @@ def main():
|
|
| 607 |
f.write(mermaid_input)
|
| 608 |
st.success("Saved to MermaidCode.md")
|
| 609 |
with colF:
|
| 610 |
-
# "Save As" with text_input
|
| 611 |
mermaid_filename = st.text_input("Filename for Mermaid:", value="MermaidCode.md", key="mermaid_filename_key")
|
| 612 |
if st.button("πΎ Save As "):
|
| 613 |
with open(mermaid_filename, 'w', encoding='utf-8') as f:
|
|
@@ -650,6 +667,13 @@ def main():
|
|
| 650 |
selected_title = random.choice(titles)
|
| 651 |
st.markdown(f"**{selected_title}**")
|
| 652 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
if __name__ == "__main__":
|
| 655 |
main()
|
|
|
|
| 15 |
import streamlit as st
|
| 16 |
import streamlit.components.v1 as components
|
| 17 |
|
| 18 |
+
# For demonstration, from huggingface_hub
|
| 19 |
from huggingface_hub import InferenceClient
|
| 20 |
|
| 21 |
# -----------------------------------------------------
|
| 22 |
+
# Ensure default MarkdownCode.md & MermaidCode.md exist
|
|
|
|
| 23 |
# -----------------------------------------------------
|
| 24 |
if not os.path.exists("MarkdownCode.md"):
|
| 25 |
with open("MarkdownCode.md", 'w', encoding='utf-8') as f:
|
| 26 |
f.write("# Default Markdown\nThis is a default Markdown file.")
|
| 27 |
+
st.experimental_rerun()
|
| 28 |
|
| 29 |
if not os.path.exists("MermaidCode.md"):
|
| 30 |
with open("MermaidCode.md", 'w', encoding='utf-8') as f:
|
| 31 |
+
# IMPORTANT: Each click line now has a 2nd string for tooltip
|
| 32 |
+
f.write("""flowchart LR
|
| 33 |
+
%% Minimal example with correct 'click' syntax
|
| 34 |
+
%% - "Tooltip text" between the URL and the target
|
| 35 |
A[Default] --> B[Example]
|
| 36 |
+
click A "/?q=Default" "Open Default" "_self"
|
| 37 |
+
click B "/?q=Example" "Open Example" "_self"
|
| 38 |
""")
|
| 39 |
+
st.experimental_rerun()
|
| 40 |
|
| 41 |
# ----------------------------
|
| 42 |
# Placeholder data structures
|
|
|
|
| 207 |
|
| 208 |
def FileSidebar():
|
| 209 |
"""
|
| 210 |
+
Renders the file sidebar with open/view/run/delete logic.
|
| 211 |
+
Excludes README.md from the list.
|
| 212 |
"""
|
| 213 |
all_files = glob.glob("*.md")
|
| 214 |
# Exclude README.md
|
|
|
|
| 217 |
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 5]
|
| 218 |
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True)
|
| 219 |
|
|
|
|
| 220 |
Files1, Files2 = st.sidebar.columns(2)
|
| 221 |
with Files1:
|
| 222 |
if st.button("π Delete All"):
|
|
|
|
| 232 |
file_name = ''
|
| 233 |
next_action = ''
|
| 234 |
|
|
|
|
| 235 |
for file in all_files:
|
| 236 |
col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1])
|
| 237 |
with col1:
|
|
|
|
| 262 |
os.remove(file)
|
| 263 |
st.rerun()
|
| 264 |
|
|
|
|
| 265 |
file_sizes = [get_file_size(file) for file in all_files]
|
| 266 |
previous_size = None
|
| 267 |
st.sidebar.title("File Operations")
|
|
|
|
| 281 |
st.rerun()
|
| 282 |
previous_size = size
|
| 283 |
|
|
|
|
| 284 |
if len(file_contents) > 0:
|
| 285 |
if next_action == 'open':
|
| 286 |
open1, open2 = st.columns([0.8, 0.2])
|
|
|
|
| 436 |
def clear_query_params():
|
| 437 |
st.query_params
|
| 438 |
|
|
|
|
| 439 |
def display_content_or_image(query):
|
| 440 |
for category, term_list in transhuman_glossary.items():
|
| 441 |
for term in term_list:
|
|
|
|
| 452 |
|
| 453 |
|
| 454 |
# ------------------------------------
|
| 455 |
+
# MERMAID DIAGRAM with Tooltip Fix
|
| 456 |
# ------------------------------------
|
| 457 |
def generate_mermaid_html(mermaid_code: str) -> str:
|
| 458 |
return f"""
|
|
|
|
| 499 |
st.sidebar.write("## Diagram Link Settings")
|
| 500 |
model_selected = st.sidebar.checkbox("Append ?model=1 to each link?")
|
| 501 |
|
| 502 |
+
# Load from .md files
|
|
|
|
| 503 |
markdown_default = load_file("MarkdownCode.md")
|
| 504 |
mermaid_default = load_file("MermaidCode.md")
|
| 505 |
|
| 506 |
+
# Rebuild for clickable diagram with optional &model=1
|
| 507 |
base_diagram = mermaid_default or ""
|
| 508 |
lines = base_diagram.strip().split("\n")
|
| 509 |
new_lines = []
|
| 510 |
for line in lines:
|
| 511 |
if "click " in line and '"/?' in line:
|
| 512 |
+
# We expect 4 segments: e.g. click A "URL" "Tooltip" "_self"
|
| 513 |
+
parts = re.split(r'click\s+\S+\s+"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"', line)
|
| 514 |
+
# parts => [prefix, URL, tooltip, target, remainder?]
|
| 515 |
+
if len(parts) == 5:
|
| 516 |
url = parts[1]
|
| 517 |
+
tooltip = parts[2]
|
| 518 |
+
target = parts[3]
|
| 519 |
updated_url = append_model_param(url, model_selected)
|
| 520 |
+
# Reassemble line
|
| 521 |
+
new_line = f"{parts[0]}click {new_lines_joiner(parts[0])}\"{updated_url}\" \"{tooltip}\" \"{target}\"{parts[4]}"
|
| 522 |
+
# But we need the node name from the 'prefix' part. Let's do a simpler approach:
|
| 523 |
+
# We can do a capturing group for the node ID in a separate pattern or handle manually.
|
| 524 |
+
# For simplicity, let's just rewrite if the user used consistent 'click NodeID "URL" "Tooltip" "Target"'
|
| 525 |
+
# We'll do a second approach:
|
| 526 |
+
|
| 527 |
+
# We'll do a direct approach: parse manually with a simpler pattern
|
| 528 |
+
# Instead, let's keep it simpler for demonstration:
|
| 529 |
+
# If your usage is consistent, you can skip all this complexity and just do a standard line replacement.
|
| 530 |
+
|
| 531 |
+
new_lines.append("click fix: " + line) # placeholder, see below
|
| 532 |
else:
|
| 533 |
+
# Possibly different or partial usage
|
| 534 |
+
# We'll do the simpler approach: just find the quoted URL & update it
|
| 535 |
+
short_line = re.split(r'click\s+(\S+)\s+"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"', line)
|
| 536 |
+
# If it doesn't match, we just leave it as is or attempt a simpler replace:
|
| 537 |
+
# We'll attempt a simpler approach with a single replace if the user always uses the second quote for the URL
|
| 538 |
+
# ...
|
| 539 |
+
updated_line = line
|
| 540 |
+
# We look for the second quoted substring if possible
|
| 541 |
+
# This can get complicated quickly, so let's keep it minimal:
|
| 542 |
+
updated_line = re.sub(r'click\s+(\S+)\s+"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"',
|
| 543 |
+
lambda m: f'click {m.group(1)} "{append_model_param(m.group(2), model_selected)}" "{m.group(3)}" "{m.group(4)}"',
|
| 544 |
+
line)
|
| 545 |
+
new_lines.append(updated_line)
|
| 546 |
else:
|
| 547 |
new_lines.append(line)
|
| 548 |
mermaid_code = "\n".join(new_lines)
|
|
|
|
| 563 |
# --- Left: Markdown Editor
|
| 564 |
with left_col:
|
| 565 |
st.subheader("Markdown Side π")
|
|
|
|
| 566 |
if "markdown_text" not in st.session_state:
|
| 567 |
st.session_state["markdown_text"] = markdown_default
|
| 568 |
|
|
|
|
| 573 |
)
|
| 574 |
st.session_state["markdown_text"] = markdown_text
|
| 575 |
|
|
|
|
| 576 |
colA, colB, colC, colD = st.columns(4)
|
| 577 |
with colA:
|
| 578 |
if st.button("π Refresh"):
|
|
|
|
| 587 |
f.write(markdown_text)
|
| 588 |
st.success("Saved to MarkdownCode.md")
|
| 589 |
with colD:
|
|
|
|
| 590 |
md_filename = st.text_input("Filename for Markdown:", value="MarkdownCode.md", key="md_filename_key")
|
| 591 |
if st.button("πΎ Save As"):
|
| 592 |
with open(md_filename, 'w', encoding='utf-8') as f:
|
|
|
|
| 625 |
f.write(mermaid_input)
|
| 626 |
st.success("Saved to MermaidCode.md")
|
| 627 |
with colF:
|
|
|
|
| 628 |
mermaid_filename = st.text_input("Filename for Mermaid:", value="MermaidCode.md", key="mermaid_filename_key")
|
| 629 |
if st.button("πΎ Save As "):
|
| 630 |
with open(mermaid_filename, 'w', encoding='utf-8') as f:
|
|
|
|
| 667 |
selected_title = random.choice(titles)
|
| 668 |
st.markdown(f"**{selected_title}**")
|
| 669 |
|
| 670 |
+
def new_lines_joiner(prefix_str):
|
| 671 |
+
"""
|
| 672 |
+
A placeholder function if you needed to parse out the node name from prefix.
|
| 673 |
+
If not used, you can remove it. This is just a stub to illustrate
|
| 674 |
+
how you might handle the 'click NodeID "URL" "Tooltip" "Target"' pattern.
|
| 675 |
+
"""
|
| 676 |
+
return prefix_str # or parse out the node name
|
| 677 |
|
| 678 |
if __name__ == "__main__":
|
| 679 |
main()
|