notes / app.py
r3hab's picture
Update app.py
c8b4c67 verified
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.")