Update app.py
Browse files
app.py
CHANGED
|
@@ -1,184 +1,132 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
def get_subjects():
|
| 7 |
"""Returns a list of subject directories."""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
def get_units(subject):
|
| 15 |
"""Returns a list of unit directories within a subject."""
|
| 16 |
-
subject_path =
|
| 17 |
-
return [
|
| 18 |
-
name
|
| 19 |
-
for name in
|
| 20 |
-
if
|
| 21 |
-
]
|
| 22 |
|
| 23 |
def get_notes(subject, unit):
|
| 24 |
"""Returns a list of note files within a unit."""
|
| 25 |
-
unit_path =
|
| 26 |
-
return [
|
| 27 |
-
name
|
| 28 |
-
for name in
|
| 29 |
-
if name.
|
| 30 |
-
]
|
| 31 |
|
| 32 |
def load_note(subject, unit, note):
|
| 33 |
"""Loads the content of a note file."""
|
| 34 |
-
note_path =
|
| 35 |
try:
|
| 36 |
-
|
| 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="
|
| 44 |
page_icon="π",
|
| 45 |
-
layout="wide",
|
| 46 |
initial_sidebar_state="expanded",
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
with st.sidebar:
|
| 61 |
st.header("Navigation")
|
|
|
|
|
|
|
| 62 |
subjects = get_subjects()
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if units:
|
| 69 |
-
selected_unit = st.selectbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
st.session_state['selected_unit'] = selected_unit
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
-
st.info("No
|
| 81 |
st.session_state['selected_note'] = None
|
| 82 |
else:
|
| 83 |
st.info("No units found in this subject.")
|
| 84 |
-
st.session_state['
|
| 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("
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
st.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|