Spaces:
Sleeping
Sleeping
File size: 3,052 Bytes
8542abb 475174a 8542abb d22a7f8 10f2e01 f970fd2 8fb7a79 8542abb 10f2e01 8fb7a79 8542abb 5b0bd42 8542abb bec9209 8542abb d8e6110 8542abb d8e6110 8542abb 10f2e01 8542abb 4fdeb8f 8542abb 10f2e01 8542abb 10f2e01 8542abb 10f2e01 8542abb 10f2e01 8542abb ac7741f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import dash
from dash import dcc, html, Input, Output, State, dash_table
import pandas as pd
import os
import datetime
import base64
import io
from google_sheet import fetch_leaderboard
from google_drive import upload_to_drive
# Make sure submissions folder exists
os.makedirs("submissions", exist_ok=True)
app = dash.Dash(__name__)
app.title = "π Hackathon Leaderboard"
app.layout = html.Div([
html.H2("π Hackathon Leaderboard"),
dcc.Upload(
id='upload-data',
children=html.Div([
'π Drag and drop or click to upload a .zip file'
]),
style={
'width': '50%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '10px',
'textAlign': 'center',
'margin': '20px'
},
multiple=False,
accept='.zip'
),
html.Button('Submit', id='submit-btn', n_clicks=0),
html.Div(id='upload-status', style={'margin': '20px', 'fontWeight': 'bold'}),
html.H3("Leaderboard"),
html.Div(id='leaderboard-table', style={'margin': '20px'})
])
def save_file_and_upload(content, filename):
timestamp = datetime.datetime.now().isoformat().replace(':', '_')
saved_filename = f"{timestamp}_{filename}"
path = os.path.join("submissions", saved_filename)
# Decode and save the file
content_string = content.split(',')[1]
decoded = base64.b64decode(content_string)
with open(path, 'wb') as f:
f.write(decoded)
# Upload to Google Drive
try:
drive_file_id = upload_to_drive(path, saved_filename)
return f"β
Uploaded to Google Drive [File ID: {drive_file_id}]"
except Exception as e:
return f"β οΈ Failed to upload to Google Drive: {e}"
def generate_leaderboard():
try:
df = fetch_leaderboard()
if df.empty:
return html.Div("No submissions yet.")
df_sorted = df.sort_values(by="score", ascending=False)
return dash_table.DataTable(
data=df_sorted.to_dict("records"),
columns=[{"name": i, "id": i} for i in df_sorted.columns],
style_table={'overflowX': 'auto'},
style_cell={'textAlign': 'left', 'padding': '5px'},
style_header={'backgroundColor': 'lightgrey', 'fontWeight': 'bold'}
)
except Exception as e:
return html.Div(f"Could not load leaderboard: {e}")
@app.callback(
Output('upload-status', 'children'),
Output('leaderboard-table', 'children'),
Input('submit-btn', 'n_clicks'),
State('upload-data', 'contents'),
State('upload-data', 'filename'),
prevent_initial_call=True
)
def handle_submission(n_clicks, contents, filename):
if contents is None:
return "β No file uploaded.", generate_leaderboard()
status = save_file_and_upload(contents, filename)
leaderboard = generate_leaderboard()
return status, leaderboard
if __name__ == '__main__':
app.run(debug=True)
|