r3hab commited on
Commit
c8b4c67
Β·
verified Β·
1 Parent(s): 948b2a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -147
app.py CHANGED
@@ -1,184 +1,132 @@
1
  import streamlit as st
2
  import os
 
3
 
4
- DATA_DIR = "/home/user/app" # Replace with your actual data directory
 
5
 
6
  def get_subjects():
7
  """Returns a list of subject directories."""
8
- return [
9
- name
10
- for name in os.listdir(DATA_DIR)
11
- if os.path.isdir(os.path.join(DATA_DIR, name))
12
- ]
 
13
 
14
  def get_units(subject):
15
  """Returns a list of unit directories within a subject."""
16
- subject_path = os.path.join(DATA_DIR, subject)
17
- return [
18
- name
19
- for name in os.listdir(subject_path)
20
- if os.path.isdir(os.path.join(subject_path, name))
21
- ]
22
 
23
  def get_notes(subject, unit):
24
  """Returns a list of note files within a unit."""
25
- unit_path = os.path.join(DATA_DIR, subject, unit)
26
- return [
27
- name
28
- for name in os.listdir(unit_path)
29
- if name.endswith(".md")
30
- ]
31
 
32
  def load_note(subject, unit, note):
33
  """Loads the content of a note file."""
34
- note_path = os.path.join(DATA_DIR, subject, unit, note)
35
  try:
36
- with open(note_path, "r", encoding="utf-8") as f:
37
- return f.read()
38
  except Exception as e:
39
  st.error(f"Error loading note: {e}")
40
  return None
41
 
42
  st.set_page_config(
43
- page_title="Beautiful Notes App",
44
  page_icon="πŸ“",
45
- layout="wide", # Keep wide for potential wider content, adjust with CSS
46
  initial_sidebar_state="expanded",
47
  )
48
 
49
- st.title("πŸ“ Beautiful Notes App")
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # --- State Management ---
52
- if 'selected_subject' not in st.session_state:
53
- st.session_state['selected_subject'] = None
54
- if 'selected_unit' not in st.session_state:
55
- st.session_state['selected_unit'] = None
56
- if 'selected_note' not in st.session_state:
57
- st.session_state['selected_note'] = None
58
 
59
- # Sidebar for navigation
 
 
 
 
 
60
  with st.sidebar:
61
  st.header("Navigation")
 
 
62
  subjects = get_subjects()
63
- selected_subject = st.selectbox("Select Subject", subjects, key="subject_selectbox")
64
- st.session_state['selected_subject'] = selected_subject
65
-
66
- if st.session_state['selected_subject']:
67
- units = get_units(st.session_state['selected_subject'])
 
 
 
 
 
 
68
  if units:
69
- selected_unit = st.selectbox("Select Unit", units, key="unit_selectbox")
 
 
 
 
 
70
  st.session_state['selected_unit'] = selected_unit
71
- if st.session_state['selected_unit']:
72
- notes = get_notes(st.session_state['selected_subject'], st.session_state['selected_unit'])
73
- if notes:
74
- selected_note = st.selectbox("Select Note", notes, key="note_selectbox")
75
- st.session_state['selected_note'] = selected_note
76
- else:
77
- st.info("No notes found in this unit.")
78
- st.session_state['selected_note'] = None
 
 
 
79
  else:
80
- st.info("No units found in this subject.")
81
  st.session_state['selected_note'] = None
82
  else:
83
  st.info("No units found in this subject.")
84
- st.session_state['selected_note'] = None
85
- else:
86
- st.info("No subjects found in the data directory.")
87
- st.session_state['selected_note'] = None
88
-
89
- # Main area to display notes
90
- col1, col2 = st.columns([1, 3]) # Adjust column widths as needed for mobile
91
-
92
- with col2:
93
- if st.session_state['selected_subject'] and st.session_state['selected_unit'] and st.session_state['selected_note']:
94
- st.header(f"{st.session_state['selected_subject']} / {st.session_state['selected_unit']} / {st.session_state['selected_note'][:-3]}") # Remove .md extension
95
- note_content = load_note(st.session_state['selected_subject'], st.session_state['selected_unit'], st.session_state['selected_note'])
96
- if note_content:
97
- st.markdown(note_content, unsafe_allow_html=True)
98
- elif st.session_state['selected_subject'] and st.session_state['selected_unit']:
99
- st.info("Select a note from the sidebar to view it.")
100
- elif st.session_state['selected_subject']:
101
- st.info("Select a unit from the sidebar to view notes.")
102
  else:
103
- st.info("Select a subject from the sidebar to start exploring your notes.")
104
-
105
- # --- Styling for Mobile ---
106
- st.markdown(
107
- """
108
- <style>
109
- /* General Styles */
110
- body {
111
- font-family: sans-serif;
112
- color: #333;
113
- background-color: #f4f4f4;
114
- margin: 0; /* Remove default body margin */
115
- }
116
- .stApp {
117
- max-width: 100%; /* Occupy full width */
118
- padding: 10px; /* Reduced padding for smaller screens */
119
- }
120
- h1 {
121
- color: #007bff;
122
- text-align: center; /* Center the title on mobile */
123
- }
124
- h2, h3, h4, h5, h6 {
125
- color: #0056b3;
126
- }
127
- .st-bf { /* Styling for selectbox labels */
128
- font-weight: bold;
129
- }
130
- .streamlit-expander {
131
- border: 1px solid #ccc;
132
- border-radius: 5px;
133
- margin-bottom: 10px;
134
- }
135
- .streamlit-expander-header {
136
- font-weight: bold;
137
- padding: 8px;
138
- }
139
- .streamlit-expander-content {
140
- padding: 8px;
141
- }
142
-
143
- /* Sidebar Styles */
144
- .stSidebar {
145
- padding-top: 1rem;
146
- }
147
- .stSidebar > div[data-testid="stVerticalBlock"] {
148
- padding: 20px; /* Add padding to sidebar content */
149
- overflow: auto; /* Enable scrolling if content overflows */
150
- max-height: calc(100vh - 50px); /* Limit height and enable scrolling */
151
- }
152
-
153
- /* Main Content Styles */
154
- .block-container {
155
- padding-top: 1rem !important;
156
- padding-bottom: 1rem !important;
157
- padding-left: 1rem !important;
158
- padding-right: 1rem !important;
159
- }
160
-
161
- /* Mobile Specific Styles */
162
- @media (max-width: 768px) {
163
- .stApp {
164
- padding: 5px; /* Further reduce padding on very small screens */
165
- }
166
- h1 {
167
- font-size: 24px; /* Smaller title on mobile */
168
- }
169
- .st-bf {
170
- font-size: 14px; /* Smaller label font */
171
- }
172
- /* Adjust selectbox size and padding */
173
- div[data-baseweb="select"] > div {
174
- padding-top: 6px;
175
- padding-bottom: 6px;
176
- }
177
- .stMarkdown {
178
- font-size: 16px; /* More readable font size for content */
179
- }
180
- }
181
- </style>
182
- """,
183
- unsafe_allow_html=True,
184
- )
 
1
  import streamlit as st
2
  import os
3
+ from pathlib import Path
4
 
5
+ # Use relative path for better portability
6
+ DATA_DIR = Path("data") # Create a 'data' directory in the same folder as the script
7
 
8
  def get_subjects():
9
  """Returns a list of subject directories."""
10
+ DATA_DIR.mkdir(exist_ok=True) # Create data directory if it doesn't exist
11
+ return sorted([
12
+ name.name
13
+ for name in DATA_DIR.iterdir()
14
+ if name.is_dir()
15
+ ])
16
 
17
  def get_units(subject):
18
  """Returns a list of unit directories within a subject."""
19
+ subject_path = DATA_DIR / subject
20
+ return sorted([
21
+ name.name
22
+ for name in subject_path.iterdir()
23
+ if name.is_dir()
24
+ ]) if subject_path.exists() else []
25
 
26
  def get_notes(subject, unit):
27
  """Returns a list of note files within a unit."""
28
+ unit_path = DATA_DIR / subject / unit
29
+ return sorted([
30
+ name.name
31
+ for name in unit_path.iterdir()
32
+ if name.suffix == '.md'
33
+ ]) if unit_path.exists() else []
34
 
35
  def load_note(subject, unit, note):
36
  """Loads the content of a note file."""
37
+ note_path = DATA_DIR / subject / unit / note
38
  try:
39
+ return note_path.read_text(encoding="utf-8")
 
40
  except Exception as e:
41
  st.error(f"Error loading note: {e}")
42
  return None
43
 
44
  st.set_page_config(
45
+ page_title="Notes App",
46
  page_icon="πŸ“",
47
+ layout="wide",
48
  initial_sidebar_state="expanded",
49
  )
50
 
51
+ # Custom CSS with simplified styling
52
+ st.markdown("""
53
+ <style>
54
+ .main > div {
55
+ padding: 2rem;
56
+ max-width: 100%;
57
+ }
58
+ .stMarkdown {
59
+ font-size: 1rem;
60
+ }
61
+ </style>
62
+ """, unsafe_allow_html=True)
63
 
64
+ st.title("πŸ“ Notes App")
 
 
 
 
 
 
65
 
66
+ # Initialize session state
67
+ for key in ['selected_subject', 'selected_unit', 'selected_note']:
68
+ if key not in st.session_state:
69
+ st.session_state[key] = None
70
+
71
+ # Sidebar navigation
72
  with st.sidebar:
73
  st.header("Navigation")
74
+
75
+ # Subject selection
76
  subjects = get_subjects()
77
+ if subjects:
78
+ selected_subject = st.selectbox(
79
+ "Select Subject",
80
+ subjects,
81
+ key="subject_selectbox",
82
+ index=subjects.index(st.session_state['selected_subject']) if st.session_state['selected_subject'] in subjects else 0
83
+ )
84
+ st.session_state['selected_subject'] = selected_subject
85
+
86
+ # Unit selection
87
+ units = get_units(selected_subject)
88
  if units:
89
+ selected_unit = st.selectbox(
90
+ "Select Unit",
91
+ units,
92
+ key="unit_selectbox",
93
+ index=units.index(st.session_state['selected_unit']) if st.session_state['selected_unit'] in units else 0
94
+ )
95
  st.session_state['selected_unit'] = selected_unit
96
+
97
+ # Note selection
98
+ notes = get_notes(selected_subject, selected_unit)
99
+ if notes:
100
+ selected_note = st.selectbox(
101
+ "Select Note",
102
+ notes,
103
+ key="note_selectbox",
104
+ index=notes.index(st.session_state['selected_note']) if st.session_state['selected_note'] in notes else 0
105
+ )
106
+ st.session_state['selected_note'] = selected_note
107
  else:
108
+ st.info("No notes found in this unit.")
109
  st.session_state['selected_note'] = None
110
  else:
111
  st.info("No units found in this subject.")
112
+ st.session_state['selected_unit'] = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  else:
114
+ st.info("No subjects found. Add some notes to get started!")
115
+
116
+ # Main content area
117
+ if all([st.session_state['selected_subject'],
118
+ st.session_state['selected_unit'],
119
+ st.session_state['selected_note']]):
120
+
121
+ st.header(f"{st.session_state['selected_subject']} / {st.session_state['selected_unit']} / {Path(st.session_state['selected_note']).stem}")
122
+
123
+ note_content = load_note(
124
+ st.session_state['selected_subject'],
125
+ st.session_state['selected_unit'],
126
+ st.session_state['selected_note']
127
+ )
128
+
129
+ if note_content:
130
+ st.markdown(note_content)
131
+ else:
132
+ st.info("Select a subject, unit, and note from the sidebar to view content.")