ufkuko/formulaeflashcards
0
1import streamlit as st2import pandas as pd3import random4import os5import base646 7# 1. Page Configuration8st.set_page_config(page_title="IB Economics Formulae", layout="centered")9 10@st.cache_data11def get_base64_image(image_path):12 with open(image_path, "rb") as img_file:13 return base64.b64encode(img_file.read()).decode()14 15def get_background_css(base64_img):16 return f"""17 <style>18 .card-with-logo {{19 position: relative;20 background-color: #ffffff;21 border: 2px solid #e9ecef;22 border-radius: 15px;23 padding: 50px;24 text-align: center;25 box-shadow: 0px 4px 12px rgba(0,0,0,0.1);26 min-height: 250px;27 display: flex;28 align-items: center;29 justify-content: center;30 }}31 .card-with-logo::after {{32 content: "";33 position: absolute;34 top: 10px;35 right: 15px;36 width: 60px;37 height: 60px;38 background-image: url("data:image/png;base64,{base64_img}");39 background-size: contain;40 background-repeat: no-repeat;41 background-position: center;42 opacity: 0.9;43 pointer-events: none;44 }}45 </style>46 """47 48# 2. Load Data49@st.cache_data50def load_data():51 try:52 # Assumes your file is named formulae.csv53 return pd.read_csv("formulae.csv")54 except Exception as e:55 st.error(f"Error loading CSV: {e}")56 return pd.DataFrame()57 58df = load_data()59 60if not df.empty:61 # --- Simplified logic: No SL/HL filtering ---62 display_df = df.copy()63 64 # Initialize Session States65 if 'shuffled_indices' not in st.session_state or st.sidebar.button("Reshuffle Formulae"):66 indices = list(range(len(display_df)))67 random.shuffle(indices)68 st.session_state.shuffled_indices = indices69 st.session_state.index = 070 st.session_state.show_formula = False71 72 current_pos = st.session_state.index73 actual_idx = st.session_state.shuffled_indices[current_pos]74 current_card = display_df.iloc[actual_idx]75 76 # Logo Logic77 logo_path = "logo.png"78 if os.path.exists(logo_path):79 try:80 base64_img = get_base64_image(logo_path)81 st.markdown(get_background_css(base64_img), unsafe_allow_html=True)82 card_class = "card-with-logo"83 except:84 card_class = "simple-card"85 else:86 card_class = "simple-card"87 88 # Front of Card89 st.markdown(f'<div class="{card_class}"><h1 style="color: #0d6efd; margin: 0;">{current_card["Term"]}</h1></div>', unsafe_allow_html=True)90 91 # Back of Card (The Formula)92 if st.button("Flip to see Formula", use_container_width=True):93 st.session_state.show_formula = not st.session_state.show_formula94 95 if st.session_state.show_formula:96 st.write("---")97 # Renders the LaTeX formula98 st.latex(current_card['Formula'])99 100 # Navigation101 col1, col2, col3 = st.columns([1,2,1])102 with col1:103 if st.button("⬅️ Prev"):104 st.session_state.index = (st.session_state.index - 1) % len(display_df)105 st.session_state.show_formula = False106 st.rerun()107 with col2:108 st.write(f"Card {st.session_state.index + 1} of {len(display_df)}")109 with col3:110 if st.button("Next ➡️"):111 st.session_state.index = (st.session_state.index + 1) % len(display_df)112 st.session_state.show_formula = False113 st.rerun()114else:115 st.warning("Waiting for formulae.csv to be uploaded or recognized...")