Spaces:
Sleeping
Sleeping
import panel as pn | |
import pandas as pd | |
import os | |
import datetime | |
import io | |
from google_sheet import fetch_leaderboard | |
from google_drive import upload_to_drive | |
pn.extension() | |
# File upload widget | |
file_input = pn.widgets.FileInput(accept='.zip', multiple=False) | |
# Status message | |
status = pn.pane.Markdown("") | |
# Leaderboard display | |
leaderboard = pn.pane.DataFrame(pd.DataFrame(), width=600) | |
def submit_file(event): | |
if file_input.value is None: | |
status.object = "β οΈ Please upload a .zip file before submitting." | |
return | |
# Save uploaded file | |
timestamp = datetime.datetime.now().isoformat().replace(":", "_") | |
filename = f"{timestamp}_{file_input.filename}" | |
submission_path = os.path.join("submissions", filename) | |
os.makedirs("submissions", exist_ok=True) | |
with open(submission_path, "wb") as f: | |
f.write(file_input.value) | |
try: | |
drive_file_id = upload_to_drive(submission_path, filename) | |
status.object = f"β Uploaded to Google Drive [File ID: {drive_file_id}]" | |
except Exception as e: | |
status.object = f"β Failed to upload to Google Drive: {e}" | |
# Update leaderboard | |
try: | |
df = fetch_leaderboard() | |
if not df.empty: | |
df_sorted = df.sort_values(by="score", ascending=False) | |
leaderboard.object = df_sorted | |
else: | |
leaderboard.object = pd.DataFrame() | |
except Exception as e: | |
status.object += f"\nβ οΈ Could not load leaderboard: {e}" | |
submit_button = pn.widgets.Button(name="Submit", button_type="primary") | |
submit_button.on_click(submit_file) | |
# Layout | |
app = pn.Column( | |
"## π Hackathon Leaderboard", | |
file_input, | |
submit_button, | |
status, | |
"### Leaderboard", | |
leaderboard | |
) | |
app.servable() | |