Lenylvt commited on
Commit
b972f61
β€’
1 Parent(s): 9b50500

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import streamlit as st
2
  import os
 
3
 
4
- # Improved function to read markdown files with error handling
5
  def load_markdown_file(path):
6
  try:
7
  with open(path, 'r', encoding='utf-8') as file:
@@ -10,27 +11,33 @@ def load_markdown_file(path):
10
  except Exception as e:
11
  return f"Error reading file: {e}"
12
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Streamlit UI Enhancements
14
  st.title('πŸ“š Documentation')
15
 
16
- # Assuming your documentation is in the 'docs' folder
17
  docs_base_path = './docs'
18
 
19
- # Enhanced error handling for directory listing
20
  if not os.path.exists(docs_base_path):
21
  st.error('Documentation directory does not exist.')
22
  else:
23
- # List categories based on folder names with improved error handling
24
  try:
25
  categories = [d for d in os.listdir(docs_base_path) if os.path.isdir(os.path.join(docs_base_path, d))]
26
  if not categories:
27
  st.error('No categories found in the documentation directory.')
28
  else:
29
- st.sidebar.markdown('## 🧷 Navigation')
30
-
31
  category = st.sidebar.selectbox('πŸ“ Select the Space', categories)
32
 
33
- # List pages based on markdown files in the selected category folder
34
  pages_path = os.path.join(docs_base_path, category)
35
  pages = [f for f in os.listdir(pages_path) if os.path.isfile(os.path.join(pages_path, f)) and f.endswith('.md')]
36
  if not pages:
@@ -38,9 +45,18 @@ else:
38
  else:
39
  page = st.sidebar.selectbox('πŸ“œ Select the Page', pages)
40
 
41
- # Load and display the selected markdown file
42
  markdown_path = os.path.join(pages_path, page)
43
  markdown_content = load_markdown_file(markdown_path)
44
- st.markdown(markdown_content, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
45
  except Exception as e:
46
- st.error(f"Error listing categories or pages: {e}")
 
1
  import streamlit as st
2
  import os
3
+ import re
4
 
5
+ # Function to read markdown files with error handling
6
  def load_markdown_file(path):
7
  try:
8
  with open(path, 'r', encoding='utf-8') as file:
 
11
  except Exception as e:
12
  return f"Error reading file: {e}"
13
 
14
+ # Function to create a simple TOC from markdown content
15
+ def create_toc(markdown_content):
16
+ toc = []
17
+ for line in markdown_content.split('\n'):
18
+ if line.startswith('#'):
19
+ level = line.count('#')
20
+ title = line.strip('# ').strip()
21
+ anchor = title.lower().replace(' ', '-').replace('/', '-').replace('.', '')
22
+ toc.append((level, title, anchor))
23
+ return toc
24
+
25
  # Streamlit UI Enhancements
26
  st.title('πŸ“š Documentation')
27
 
 
28
  docs_base_path = './docs'
29
 
 
30
  if not os.path.exists(docs_base_path):
31
  st.error('Documentation directory does not exist.')
32
  else:
 
33
  try:
34
  categories = [d for d in os.listdir(docs_base_path) if os.path.isdir(os.path.join(docs_base_path, d))]
35
  if not categories:
36
  st.error('No categories found in the documentation directory.')
37
  else:
38
+ st.sidebar.markdown('# 🧷 Navigation')
 
39
  category = st.sidebar.selectbox('πŸ“ Select the Space', categories)
40
 
 
41
  pages_path = os.path.join(docs_base_path, category)
42
  pages = [f for f in os.listdir(pages_path) if os.path.isfile(os.path.join(pages_path, f)) and f.endswith('.md')]
43
  if not pages:
 
45
  else:
46
  page = st.sidebar.selectbox('πŸ“œ Select the Page', pages)
47
 
 
48
  markdown_path = os.path.join(pages_path, page)
49
  markdown_content = load_markdown_file(markdown_path)
50
+
51
+ # Create TOC
52
+ toc = create_toc(markdown_content)
53
+ if toc:
54
+ st.sidebar.markdown('# πŸ“‘ Table of Contents')
55
+ for level, title, anchor in toc:
56
+ st.sidebar.markdown(f"{' ' * (level-1) * 2}- [{title}](#{anchor})")
57
+
58
+ # Display markdown with potential anchors
59
+ modified_markdown_content = re.sub(r'(#{1,6}) (.*)', r'\1 <a name="\2"></a>\2', markdown_content)
60
+ st.markdown(modified_markdown_content, unsafe_allow_html=True)
61
  except Exception as e:
62
+ st.error(f"Error listing categories or pages: {e}")