|
|
import streamlit as st |
|
|
import os |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
DATA_DIR = Path("data") |
|
|
|
|
|
def get_subjects(): |
|
|
"""Returns a list of subject directories.""" |
|
|
DATA_DIR.mkdir(exist_ok=True) |
|
|
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", |
|
|
) |
|
|
|
|
|
|
|
|
st.markdown(""" |
|
|
<style> |
|
|
.main > div { |
|
|
padding: 2rem; |
|
|
max-width: 100%; |
|
|
} |
|
|
.stMarkdown { |
|
|
font-size: 1rem; |
|
|
} |
|
|
</style> |
|
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.title("π Notes App") |
|
|
|
|
|
|
|
|
for key in ['selected_subject', 'selected_unit', 'selected_note']: |
|
|
if key not in st.session_state: |
|
|
st.session_state[key] = None |
|
|
|
|
|
|
|
|
with st.sidebar: |
|
|
st.header("Navigation") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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!") |
|
|
|
|
|
|
|
|
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.") |