Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| # === Google Sheets === | |
| MAIN_SHEET_ID = "1cUBISFNCHqS9Ru_jBlYZgF8vb-e25wq8gC0PoT3cHlg" | |
| MAIN_SHEET_NAME = "Sheet1" | |
| ANNOUNCEMENT_SHEET_ID = "1B-Vhgi0BSi8iKR4_Y5ICOcf5cDd73atzq71wU17cCTs" | |
| ANNOUNCEMENT_SHEET_NAME = "Sheet1" | |
| # === URLs === | |
| main_csv_url = f"https://docs.google.com/spreadsheets/d/{MAIN_SHEET_ID}/gviz/tq?tqx=out:csv&sheet={MAIN_SHEET_NAME}" | |
| announcement_csv_url = f"https://docs.google.com/spreadsheets/d/{ANNOUNCEMENT_SHEET_ID}/gviz/tq?tqx=out:csv&sheet={ANNOUNCEMENT_SHEET_NAME}" | |
| # === Password === | |
| ADMIN_PASSWORD = "1234" # change this if needed | |
| # === Fetch Functions === | |
| def fetch_data(): | |
| try: | |
| df = pd.read_csv(main_csv_url) | |
| df = df.head(10) | |
| return df | |
| except Exception as e: | |
| return pd.DataFrame({"Error": [f"Failed to fetch main data: {e}"]}) | |
| def fetch_announcements(): | |
| try: | |
| df = pd.read_csv(announcement_csv_url) | |
| return df | |
| except Exception as e: | |
| return pd.DataFrame({"Error": [f"Failed to fetch announcements: {e}"]}) | |
| # === Convert DataFrame to Cards === | |
| def df_to_cards(df, page_name="Main"): | |
| if df.empty: | |
| return "<p style='color:red;'>No data found.</p>" | |
| html = "<div style='display:flex;flex-wrap:wrap;gap:10px;justify-content:center;'>" | |
| for _, row in df.iterrows(): | |
| html += f""" | |
| <div style='width:300px;background:#ffffff;border-radius:15px;padding:15px; | |
| box-shadow:0 3px 8px rgba(0,0,0,0.15);font-family:sans-serif;'> | |
| <h3 style='color:#333;margin-bottom:5px;'>{row.iloc[0]}</h3> | |
| <p style='margin:4px 0;color:#555;font-size:14px;'><b>Details:</b> {', '.join(map(str, row.values[1:]))}</p> | |
| </div> | |
| """ | |
| html += "</div>" | |
| return html | |
| # === Interface === | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| # --- HEADER --- | |
| with gr.Row(): | |
| gr.Markdown("### π₯ Blaze Dashboard", elem_id="title") | |
| admin_btn = gr.Button("π¨βπΌ ADMIN", elem_id="admin_button") | |
| # --- MAIN PAGE --- | |
| with gr.Column(visible=True) as main_page: | |
| gr.Markdown("### π Main Data") | |
| main_cards = gr.HTML() | |
| refresh_btn = gr.Button("π Refresh Data") | |
| # --- PASSWORD PAGE --- | |
| with gr.Column(visible=False) as password_page: | |
| gr.Markdown("### π Admin Login") | |
| password_input = gr.Textbox(label="Enter Password", type="password") | |
| submit_pass = gr.Button("Submit") | |
| # --- ADMIN PAGE --- | |
| with gr.Column(visible=False) as admin_page: | |
| gr.Markdown("### π’ Important Announcements") | |
| admin_cards = gr.HTML() | |
| back_btn = gr.Button("β¬ Back") | |
| # --- Logic --- | |
| def refresh_main(): | |
| df = fetch_data() | |
| return df_to_cards(df, "Main") | |
| def go_admin(): | |
| return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False) | |
| def verify_password(pw): | |
| if pw == ADMIN_PASSWORD: | |
| df = fetch_announcements() | |
| return ( | |
| gr.update(visible=False), | |
| gr.update(visible=False), | |
| gr.update(visible=True), | |
| df_to_cards(df, "Announcements"), | |
| ) | |
| else: | |
| return ( | |
| gr.update(visible=False), | |
| gr.update(visible=True), | |
| gr.update(visible=False), | |
| "<p style='color:red;'>β Incorrect password.</p>", | |
| ) | |
| refresh_btn.click(refresh_main, outputs=main_cards) | |
| admin_btn.click(go_admin, outputs=[main_page, password_page, admin_page]) | |
| submit_pass.click(verify_password, inputs=password_input, outputs=[main_page, password_page, admin_page, admin_cards]) | |
| back_btn.click(lambda: (gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)), outputs=[main_page, password_page, admin_page]) | |
| # Load initial data | |
| demo.load(refresh_main, outputs=main_cards) | |
| demo.launch(share=True) | |