Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -75
src/streamlit_app.py
CHANGED
|
@@ -1,98 +1,64 @@
|
|
| 1 |
-
import
|
| 2 |
-
from dash import dcc, html, Input, Output, State, dash_table
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
| 5 |
import datetime
|
| 6 |
-
import base64
|
| 7 |
import io
|
| 8 |
|
| 9 |
from google_sheet import fetch_leaderboard
|
| 10 |
from google_drive import upload_to_drive
|
| 11 |
|
| 12 |
-
|
| 13 |
-
os.makedirs("submissions", exist_ok=True)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
children=html.Div([
|
| 24 |
-
'π Drag and drop or click to upload a .zip file'
|
| 25 |
-
]),
|
| 26 |
-
style={
|
| 27 |
-
'width': '50%',
|
| 28 |
-
'height': '60px',
|
| 29 |
-
'lineHeight': '60px',
|
| 30 |
-
'borderWidth': '1px',
|
| 31 |
-
'borderStyle': 'dashed',
|
| 32 |
-
'borderRadius': '10px',
|
| 33 |
-
'textAlign': 'center',
|
| 34 |
-
'margin': '20px'
|
| 35 |
-
},
|
| 36 |
-
multiple=False,
|
| 37 |
-
accept='.zip'
|
| 38 |
-
),
|
| 39 |
-
html.Button('Submit', id='submit-btn', n_clicks=0),
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
timestamp = datetime.datetime.now().isoformat().replace(
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# Decode and save the file
|
| 52 |
-
content_string = content.split(',')[1]
|
| 53 |
-
decoded = base64.b64decode(content_string)
|
| 54 |
-
|
| 55 |
-
with open(path, 'wb') as f:
|
| 56 |
-
f.write(decoded)
|
| 57 |
-
|
| 58 |
-
# Upload to Google Drive
|
| 59 |
try:
|
| 60 |
-
drive_file_id = upload_to_drive(
|
| 61 |
-
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
try:
|
| 67 |
df = fetch_leaderboard()
|
| 68 |
-
if df.empty:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
columns=[{"name": i, "id": i} for i in df_sorted.columns],
|
| 74 |
-
style_table={'overflowX': 'auto'},
|
| 75 |
-
style_cell={'textAlign': 'left', 'padding': '5px'},
|
| 76 |
-
style_header={'backgroundColor': 'lightgrey', 'fontWeight': 'bold'}
|
| 77 |
-
)
|
| 78 |
except Exception as e:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
)
|
| 89 |
-
def handle_submission(n_clicks, contents, filename):
|
| 90 |
-
if contents is None:
|
| 91 |
-
return "β No file uploaded.", generate_leaderboard()
|
| 92 |
-
|
| 93 |
-
status = save_file_and_upload(contents, filename)
|
| 94 |
-
leaderboard = generate_leaderboard()
|
| 95 |
-
return status, leaderboard
|
| 96 |
|
| 97 |
-
|
| 98 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
import panel as pn
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
import datetime
|
|
|
|
| 5 |
import io
|
| 6 |
|
| 7 |
from google_sheet import fetch_leaderboard
|
| 8 |
from google_drive import upload_to_drive
|
| 9 |
|
| 10 |
+
pn.extension()
|
|
|
|
| 11 |
|
| 12 |
+
# File upload widget
|
| 13 |
+
file_input = pn.widgets.FileInput(accept='.zip', multiple=False)
|
| 14 |
|
| 15 |
+
# Status message
|
| 16 |
+
status = pn.pane.Markdown("")
|
| 17 |
|
| 18 |
+
# Leaderboard display
|
| 19 |
+
leaderboard = pn.pane.DataFrame(pd.DataFrame(), width=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def submit_file(event):
|
| 22 |
+
if file_input.value is None:
|
| 23 |
+
status.object = "β οΈ Please upload a .zip file before submitting."
|
| 24 |
+
return
|
| 25 |
|
| 26 |
+
# Save uploaded file
|
| 27 |
+
timestamp = datetime.datetime.now().isoformat().replace(":", "_")
|
| 28 |
+
filename = f"{timestamp}_{file_input.filename}"
|
| 29 |
+
submission_path = os.path.join("submissions", filename)
|
| 30 |
+
os.makedirs("submissions", exist_ok=True)
|
| 31 |
+
with open(submission_path, "wb") as f:
|
| 32 |
+
f.write(file_input.value)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
drive_file_id = upload_to_drive(submission_path, filename)
|
| 36 |
+
status.object = f"β
Uploaded to Google Drive [File ID: {drive_file_id}]"
|
| 37 |
except Exception as e:
|
| 38 |
+
status.object = f"β Failed to upload to Google Drive: {e}"
|
| 39 |
|
| 40 |
+
# Update leaderboard
|
| 41 |
try:
|
| 42 |
df = fetch_leaderboard()
|
| 43 |
+
if not df.empty:
|
| 44 |
+
df_sorted = df.sort_values(by="score", ascending=False)
|
| 45 |
+
leaderboard.object = df_sorted
|
| 46 |
+
else:
|
| 47 |
+
leaderboard.object = pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
status.object += f"\nβ οΈ Could not load leaderboard: {e}"
|
| 50 |
+
|
| 51 |
+
submit_button = pn.widgets.Button(name="Submit", button_type="primary")
|
| 52 |
+
submit_button.on_click(submit_file)
|
| 53 |
|
| 54 |
+
# Layout
|
| 55 |
+
app = pn.Column(
|
| 56 |
+
"## π Hackathon Leaderboard",
|
| 57 |
+
file_input,
|
| 58 |
+
submit_button,
|
| 59 |
+
status,
|
| 60 |
+
"### Leaderboard",
|
| 61 |
+
leaderboard
|
| 62 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
app.servable()
|
|
|