com3dian commited on
Commit
e31cd27
Β·
verified Β·
1 Parent(s): 54a351b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +41 -75
src/streamlit_app.py CHANGED
@@ -1,98 +1,64 @@
1
- import dash
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
- # Make sure submissions folder exists
13
- os.makedirs("submissions", exist_ok=True)
14
 
15
- app = dash.Dash(__name__)
16
- app.title = "πŸ† Hackathon Leaderboard"
17
 
18
- app.layout = html.Div([
19
- html.H2("πŸ† Hackathon Leaderboard"),
20
 
21
- dcc.Upload(
22
- id='upload-data',
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
- html.Div(id='upload-status', style={'margin': '20px', 'fontWeight': 'bold'}),
42
- html.H3("Leaderboard"),
43
- html.Div(id='leaderboard-table', style={'margin': '20px'})
44
- ])
45
 
46
- def save_file_and_upload(content, filename):
47
- timestamp = datetime.datetime.now().isoformat().replace(':', '_')
48
- saved_filename = f"{timestamp}_{filename}"
49
- path = os.path.join("submissions", saved_filename)
 
 
 
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(path, saved_filename)
61
- return f"βœ… Uploaded to Google Drive [File ID: {drive_file_id}]"
62
  except Exception as e:
63
- return f"⚠️ Failed to upload to Google Drive: {e}"
64
 
65
- def generate_leaderboard():
66
  try:
67
  df = fetch_leaderboard()
68
- if df.empty:
69
- return html.Div("No submissions yet.")
70
- df_sorted = df.sort_values(by="score", ascending=False)
71
- return dash_table.DataTable(
72
- data=df_sorted.to_dict("records"),
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
- return html.Div(f"Could not load leaderboard: {e}")
 
 
 
80
 
81
- @app.callback(
82
- Output('upload-status', 'children'),
83
- Output('leaderboard-table', 'children'),
84
- Input('submit-btn', 'n_clicks'),
85
- State('upload-data', 'contents'),
86
- State('upload-data', 'filename'),
87
- prevent_initial_call=True
 
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
- if __name__ == '__main__':
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()