File size: 4,184 Bytes
110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 948b2a6 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 110ab61 c8b4c67 948b2a6 c8b4c67 110ab61 c8b4c67 948b2a6 110ab61 c8b4c67 948b2a6 c8b4c67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import streamlit as st
import os
from pathlib import Path
# Use relative path for better portability
DATA_DIR = Path("data") # Create a 'data' directory in the same folder as the script
def get_subjects():
"""Returns a list of subject directories."""
DATA_DIR.mkdir(exist_ok=True) # Create data directory if it doesn't exist
return sorted([
name.name
for name in DATA_DIR.iterdir()
if name.is_dir()
])
def get_units(subject):
"""Returns a list of unit directories within a subject."""
subject_path = DATA_DIR / subject
return sorted([
name.name
for name in subject_path.iterdir()
if name.is_dir()
]) if subject_path.exists() else []
def get_notes(subject, unit):
"""Returns a list of note files within a unit."""
unit_path = DATA_DIR / subject / unit
return sorted([
name.name
for name in unit_path.iterdir()
if name.suffix == '.md'
]) if unit_path.exists() else []
def load_note(subject, unit, note):
"""Loads the content of a note file."""
note_path = DATA_DIR / subject / unit / note
try:
return note_path.read_text(encoding="utf-8")
except Exception as e:
st.error(f"Error loading note: {e}")
return None
st.set_page_config(
page_title="Notes App",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded",
)
# Custom CSS with simplified styling
st.markdown("""
<style>
.main > div {
padding: 2rem;
max-width: 100%;
}
.stMarkdown {
font-size: 1rem;
}
</style>
""", unsafe_allow_html=True)
st.title("π Notes App")
# Initialize session state
for key in ['selected_subject', 'selected_unit', 'selected_note']:
if key not in st.session_state:
st.session_state[key] = None
# Sidebar navigation
with st.sidebar:
st.header("Navigation")
# Subject selection
subjects = get_subjects()
if subjects:
selected_subject = st.selectbox(
"Select Subject",
subjects,
key="subject_selectbox",
index=subjects.index(st.session_state['selected_subject']) if st.session_state['selected_subject'] in subjects else 0
)
st.session_state['selected_subject'] = selected_subject
# Unit selection
units = get_units(selected_subject)
if units:
selected_unit = st.selectbox(
"Select Unit",
units,
key="unit_selectbox",
index=units.index(st.session_state['selected_unit']) if st.session_state['selected_unit'] in units else 0
)
st.session_state['selected_unit'] = selected_unit
# Note selection
notes = get_notes(selected_subject, selected_unit)
if notes:
selected_note = st.selectbox(
"Select Note",
notes,
key="note_selectbox",
index=notes.index(st.session_state['selected_note']) if st.session_state['selected_note'] in notes else 0
)
st.session_state['selected_note'] = selected_note
else:
st.info("No notes found in this unit.")
st.session_state['selected_note'] = None
else:
st.info("No units found in this subject.")
st.session_state['selected_unit'] = None
else:
st.info("No subjects found. Add some notes to get started!")
# Main content area
if all([st.session_state['selected_subject'],
st.session_state['selected_unit'],
st.session_state['selected_note']]):
st.header(f"{st.session_state['selected_subject']} / {st.session_state['selected_unit']} / {Path(st.session_state['selected_note']).stem}")
note_content = load_note(
st.session_state['selected_subject'],
st.session_state['selected_unit'],
st.session_state['selected_note']
)
if note_content:
st.markdown(note_content)
else:
st.info("Select a subject, unit, and note from the sidebar to view content.") |