Lenylvt commited on
Commit
49bea70
β€’
1 Parent(s): b3d53ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -40
app.py CHANGED
@@ -1,49 +1,61 @@
1
  import streamlit as st
2
  import os
3
 
4
- # Custom CSS to style the markdown content
5
- def local_css(file_name):
6
- with open(file_name, "r") as f:
7
- st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
8
-
9
- # Function to read markdown files
10
  def load_markdown_file(path):
11
- with open(path, "r") as file:
12
- content = file.read()
13
- return content
14
-
15
- # Initialize the app with a nicer layout
16
- st.set_page_config(layout="wide")
17
-
18
- # Assuming you will create a style.css for custom styles
19
- # Make sure to create a 'style.css' file in your project directory
20
- local_css("style.css")
21
 
22
- # Streamlit UI
23
- st.title('πŸ“š Documentation')
24
 
25
  # Assuming your documentation is in the 'docs' folder
26
  docs_base_path = './docs'
27
 
28
- # Create columns for sidebar and main content
29
- col1, col2 = st.columns([1, 3])
30
-
31
- with col1:
32
- st.write("## Navigation")
33
-
34
- # List categories based on folder names
35
- categories = [d for d in os.listdir(docs_base_path) if os.path.isdir(os.path.join(docs_base_path, d))]
36
- category = st.selectbox('Select a Category', categories)
37
-
38
- # List pages based on markdown files in the selected category folder
39
- pages_path = os.path.join(docs_base_path, category)
40
- pages = [f for f in os.listdir(pages_path) if os.path.isfile(os.path.join(pages_path, f))]
41
- page = st.selectbox('Select a Page', pages)
42
-
43
- with col2:
44
- st.write(f"## {page[:-3]}") # Remove .md extension and display as title
45
-
46
- # Load and display the selected markdown file
47
- markdown_path = os.path.join(pages_path, page)
48
- markdown_content = load_markdown_file(markdown_path)
49
- st.markdown(markdown_content, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:
8
+ content = file.read()
9
+ return content
10
+ except Exception as e:
11
+ return f"Error reading file: {e}"
 
 
 
 
12
 
13
+ # Streamlit UI Enhancements
14
+ st.title('πŸ“š Documentation Viewer')
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
+ category = st.sidebar.selectbox('Select a Category', categories)
30
+
31
+ # List pages based on markdown files in the selected category folder
32
+ pages_path = os.path.join(docs_base_path, category)
33
+ pages = [f for f in os.listdir(pages_path) if os.path.isfile(os.path.join(pages_path, f)) and f.endswith('.md')]
34
+ if not pages:
35
+ st.error('No markdown pages found in the selected category.')
36
+ else:
37
+ page = st.sidebar.selectbox('Select a Page', pages)
38
+
39
+ # Load and display the selected markdown file
40
+ markdown_path = os.path.join(pages_path, page)
41
+ markdown_content = load_markdown_file(markdown_path)
42
+ st.markdown(markdown_content, unsafe_allow_html=True)
43
+ except Exception as e:
44
+ st.error(f"Error listing categories or pages: {e}")
45
+
46
+ # Optional: Add a theme or custom CSS for styling
47
+ st.markdown(
48
+ """
49
+ <style>
50
+ .css-18e3th9 {
51
+ background-color: #f0f2f6;
52
+ }
53
+ .css-1d391kg {
54
+ padding-top: 1rem;
55
+ padding-left: 1rem;
56
+ padding-right: 1rem;
57
+ }
58
+ </style>
59
+ """,
60
+ unsafe_allow_html=True,
61
+ )