Abhishek Thakur commited on
Commit
6aa12a9
·
1 Parent(s): ad58342

selected submissions

Browse files
competitions/app.py CHANGED
@@ -52,6 +52,11 @@ class User(BaseModel):
52
  user_token: str
53
 
54
 
 
 
 
 
 
55
  def run_job_runner():
56
  job_runner = JobRunner(token=HF_TOKEN, competition_info=COMP_INFO, output_path=OUTPUT_PATH)
57
  job_runner.run()
@@ -148,8 +153,8 @@ async def my_submissions(request: Request, user: User):
148
  }
149
  }
150
  subs = pd.concat([success_subs, failed_subs], axis=0)
151
- subs = subs.to_markdown(index=False)
152
- if len(subs.strip()) == 0:
153
  subs = "You have not made any submissions yet."
154
  failed_subs = ""
155
  submission_text = SUBMISSION_TEXT.format(COMP_INFO.submission_limit)
@@ -189,3 +194,24 @@ async def new_submission(
189
  except AuthenticationError:
190
  return {"response": "Invalid token"}
191
  return {"response": "Invalid competition type"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  user_token: str
53
 
54
 
55
+ class UserSubmissionUpdate(BaseModel):
56
+ user_token: str
57
+ submission_ids: str
58
+
59
+
60
  def run_job_runner():
61
  job_runner = JobRunner(token=HF_TOKEN, competition_info=COMP_INFO, output_path=OUTPUT_PATH)
62
  job_runner.run()
 
153
  }
154
  }
155
  subs = pd.concat([success_subs, failed_subs], axis=0)
156
+ subs = subs.to_dict(orient="records")
157
+ if len(subs) == 0:
158
  subs = "You have not made any submissions yet."
159
  failed_subs = ""
160
  submission_text = SUBMISSION_TEXT.format(COMP_INFO.submission_limit)
 
194
  except AuthenticationError:
195
  return {"response": "Invalid token"}
196
  return {"response": "Invalid competition type"}
197
+
198
+
199
+ @app.post("/update_selected_submissions", response_class=JSONResponse)
200
+ def update_selected_submissions(request: Request, user_sub: UserSubmissionUpdate):
201
+ sub = Submissions(
202
+ end_date=COMP_INFO.end_date,
203
+ submission_limit=COMP_INFO.submission_limit,
204
+ competition_id=COMPETITION_ID,
205
+ token=HF_TOKEN,
206
+ competition_type=COMP_INFO.competition_type,
207
+ hardware=COMP_INFO.hardware,
208
+ )
209
+ submission_ids = user_sub.submission_ids.split(",")
210
+ submission_ids = [s.strip() for s in submission_ids]
211
+ if len(submission_ids) > COMP_INFO.selection_limit:
212
+ return {
213
+ "success": False,
214
+ "error": f"Please select at most {COMP_INFO.selection_limit} submissions.",
215
+ }
216
+ sub.update_selected_submissions(user_token=user_sub.user_token, selected_submission_ids=submission_ids)
217
+ return {"success": True, "error": ""}
competitions/templates/index.html CHANGED
@@ -133,7 +133,35 @@
133
  })
134
  .then(data => {
135
  const contentDiv = document.getElementById('content');
136
- contentDiv.innerHTML = marked.parse(data.response.submission_text) + marked.parse(data.response.submissions);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  articleLoadingSpinner.classList.add('hidden');
138
  })
139
  .catch(error => {
@@ -389,7 +417,7 @@
389
  (optional)
390
  </label>
391
  <textarea id="submission_comment" name="submission_comment" rows="5"
392
- class="p-2.5 w-full text-sm text-gray-900" placeholder=""></textarea>
393
  </div>
394
  <div class="form-actions mt-6">
395
  <button data-modal-hide="submission-modal" type="button"
@@ -461,4 +489,53 @@
461
  });
462
  </script>
463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464
  </html>
 
133
  })
134
  .then(data => {
135
  const contentDiv = document.getElementById('content');
136
+ // console.log(data.response.submissions);
137
+ // contentDiv.innerHTML = marked.parse(data.response.submission_text) + data.response.submissions;
138
+ if (data.response.submissions && data.response.submissions.length > 0) {
139
+ // Start building the table HTML
140
+ let tableHTML = '<table border="1"><tr><th>Datetime</th><th>Submission ID</th><th>Public Score</th><th>Submission Comment</th><th>Selected</th><th>Status</th></tr>';
141
+
142
+ // Iterate over each submission and add it to the table
143
+ data.response.submissions.forEach(submission => {
144
+ tableHTML += `<tr>
145
+ <td>${submission.datetime}</td>
146
+ <td>${submission.submission_id}</td>
147
+ <td>${submission.public_score}</td>
148
+ <td>${submission.submission_comment}</td>
149
+ <td><input type="checkbox" name="selectedSubmissions" value="${submission.submission_id}" ${submission.selected ? 'checked' : ''}></td>
150
+ <td>${submission.status}</td>
151
+ </tr>`;
152
+ });
153
+
154
+ // Close the table HTML and set it as the content
155
+ tableHTML += '</table>';
156
+ tableHTML += '<button id="updateSelectedSubmissionsButton" type="button" class="confirm text-white bg-green-600 hover:bg-green-800 focus:ring-4 focus:outline-none focus:ring-green-300 font-medium rounded-lg text-sm inline-flex items-center px-5 py-2.5 text-center me-2">Update Selected Submissions</button>';
157
+ contentDiv.innerHTML = marked.parse(data.response.submission_text) + tableHTML;
158
+ document.getElementById('updateSelectedSubmissionsButton').addEventListener('click', function () {
159
+ updateSelectedSubmissions(userToken);
160
+ });
161
+ } else {
162
+ // Display message if there are no submissions
163
+ contentDiv.innerHTML = marked.parse(data.response.submission_text) + 'You have not made any submissions yet.';
164
+ }
165
  articleLoadingSpinner.classList.add('hidden');
166
  })
167
  .catch(error => {
 
417
  (optional)
418
  </label>
419
  <textarea id="submission_comment" name="submission_comment" rows="5"
420
+ class="p-2.5 w-full text-sm text-gray-900" placeholder=" "></textarea>
421
  </div>
422
  <div class="form-actions mt-6">
423
  <button data-modal-hide="submission-modal" type="button"
 
489
  });
490
  </script>
491
 
492
+ <script>
493
+ function updateSelectedSubmissions(userToken) {
494
+ const selectedSubmissions = document.querySelectorAll('input[name="selectedSubmissions"]:checked');
495
+ const articleLoadingSpinner = document.getElementById('articleLoadingSpinner');
496
+ articleLoadingSpinner.classList.remove('hidden');
497
+ let selectedSubmissionIds = [];
498
+ selectedSubmissions.forEach((submission) => {
499
+ selectedSubmissionIds.push(submission.value);
500
+ });
501
+
502
+ const updateEndpoint = '/update_selected_submissions';
503
+ const requestOptions = {
504
+ method: 'POST',
505
+ headers: {
506
+ 'Content-Type': 'application/json',
507
+ },
508
+ body: JSON.stringify({
509
+ "user_token": userToken,
510
+ "submission_ids": selectedSubmissionIds.join(',')
511
+ })
512
+ };
513
+
514
+ fetch(updateEndpoint, requestOptions)
515
+ .then(response => {
516
+ if (!response.ok) {
517
+ throw new Error('Network response was not ok');
518
+ }
519
+ return response.json();
520
+ })
521
+ .then(data => {
522
+ if (data.success) {
523
+ // Optionally, display a success message or handle accordingly
524
+ console.log('Update successful');
525
+ articleLoadingSpinner.classList.add('hidden');
526
+ } else {
527
+ // Handle failure case
528
+ console.log('Update failed');
529
+ articleLoadingSpinner.classList.add('hidden');
530
+ alert(data.error);
531
+ }
532
+ // Refresh submissions display
533
+ fetchAndDisplaySubmissions();
534
+ })
535
+ .catch(error => {
536
+ console.error('There was a problem with the fetch operation for updating:', error);
537
+ });
538
+ }
539
+ </script>
540
+
541
  </html>