import streamlit as st import pandas as pd from datetime import datetime import os import base64 from io import BytesIO from PIL import Image # 🌟πŸ”₯ Initialize session state like a galactic DJ spinning tracks! if 'file_history' not in st.session_state: st.session_state['file_history'] = [] # πŸ“œπŸ’Ύ Save to history like a time-traveling scribe! | πŸ“…βœ¨ save_to_history("πŸ–ΌοΈ Image", "pic.jpg", img_data) - Stamps a pic in the history books like a boss! def save_to_history(file_type, file_path, img_data): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(file_path, "wb") as f: f.write(base64.b64decode(img_data.split(',')[1])) st.session_state['file_history'].append({ "Timestamp": timestamp, "Type": file_type, "Path": file_path }) # πŸŽ›οΈ Sidebar config like a spaceship control panel! with st.sidebar: st.header("πŸŽšοΈπŸ“Έ Snap Shack") st.subheader("πŸ“œ Snap Stash") if st.session_state['file_history']: images = [f for f in st.session_state['file_history'] if f['Type'] == "πŸ–ΌοΈ Image"] if images: st.write("πŸ–ΌοΈ Images") for img in images: st.write(f"- {img['Path']} @ {img['Timestamp']}") else: st.write("πŸ•³οΈ Empty Stash!") # 🌍🎨 Main UI kicks off like a cosmic art show! st.title("πŸ“Έ Live Camera Snap Craze") # πŸ“ΈπŸ“· JS live camera zone! st.header("πŸ“ΈπŸŽ₯ Live Snap Zone") live_camera_html = """
""" st.markdown(live_camera_html, unsafe_allow_html=True) # πŸ“ΈπŸ“₯ Handle snapshots from JS def handle_snapshot(): if "snapshot" in st.session_state: snapshot_data = st.session_state["snapshot"] filename = f"snap_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" save_to_history("πŸ–ΌοΈ Image", filename, snapshot_data) st.image(Image.open(BytesIO(base64.b64decode(snapshot_data.split(',')[1]))), caption="Latest Snap", use_container_width=True) del st.session_state["snapshot"] st.components.v1.html( """ """, height=0 ) handle_snapshot() # πŸ“‚ Upload zone like a media drop party! st.header("πŸ“₯πŸŽ‰ Drop Zone") uploaded_files = st.file_uploader("πŸ“Έ Toss Pics", accept_multiple_files=True, type=['jpg', 'png']) if uploaded_files: for uploaded_file in uploaded_files: file_path = f"uploaded_{uploaded_file.name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.jpg" save_to_history("πŸ–ΌοΈ Image", file_path, base64.b64encode(uploaded_file.getvalue()).decode()) st.image(Image.open(uploaded_file), caption=uploaded_file.name, use_container_width=True) # πŸ–ΌοΈ Gallery like a media circus! st.header("πŸŽͺ Snap Show") if st.session_state['file_history']: images = [f for f in st.session_state['file_history'] if f['Type'] == "πŸ–ΌοΈ Image"] if images: st.subheader("πŸ–ΌοΈ Pic Parade") cols = st.columns(3) for i, img in enumerate(images): with cols[i % 3]: if os.path.exists(img['Path']): st.image(img['Path'], caption=img['Path'], use_container_width=True) with open(img['Path'], "rb") as f: img_data = f.read() st.markdown(f'πŸ“₯ Snag It!', unsafe_allow_html=True) else: st.warning(f"🚨 Missing file: {img['Path']}") else: st.write("🚫 No snaps yet!") # πŸ“œ History log like a time machine! st.header("⏳ Snap Saga") if st.session_state['file_history']: df = pd.DataFrame(st.session_state['file_history']) st.dataframe(df) else: st.write("πŸ•³οΈ Nothing snapped yet!")