linh-hk commited on
Commit
046aaba
·
1 Parent(s): 55788de

fix socketIO connection and Log placeholder

Browse files
Files changed (2) hide show
  1. app.py +36 -36
  2. templates/Output.html +5 -1
app.py CHANGED
@@ -28,7 +28,7 @@ app.config["SECRET_KEY"] = "dev-key"
28
  app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
29
 
30
  # # Socket.IO (logs on for debugging)
31
- # socketio = SocketIO(app, async_mode="eventlet", cors_allowed_origins="*")
32
 
33
 
34
  # --- job registry for cancel flags ---
@@ -171,7 +171,7 @@ def run_job(job_id, accessions):
171
  sources = out.get("Sources", "No Links")
172
  time_cost = out.get("Time cost") or f"{dt:.2f}s"
173
 
174
- send_row = {
175
  "idx": i,
176
  "sample_id": sample_id,
177
  "predicted_country": predicted_country,
@@ -182,7 +182,7 @@ def run_job(job_id, accessions):
182
  "time_cost": time_cost,
183
  }
184
 
185
- socketio.emit("row", send_row, room=room)
186
  socketio.sleep(0) # <- correct spelling; yield so the emit flushes
187
  send_log(f"Processed {acc} in {dt:.2f}s")
188
 
@@ -202,39 +202,39 @@ def run_job(job_id, accessions):
202
  CANCEL_FLAGS.pop(job_id, None)
203
  JOBS.pop(job_id, None) # <— tidy queued job
204
 
205
- # # ---- Socket.IO events ----
206
- # @socketio.on("connect")
207
- # def on_connect():
208
- # emit("connected", {"ok": True})
209
-
210
- # @socketio.on("join")
211
- # def on_join(data):
212
- # job_id = data.get("job_id")
213
- # if job_id:
214
- # join_room(job_id)
215
- # emit("joined", {"room": job_id})
216
-
217
- # # Start the job once the client is in the room
218
- # job = JOBS.get(job_id)
219
- # if job and not job["started"]:
220
- # job["started"] = True
221
- # total = len(job["accs"])
222
- # # Send an initial queued/total status so the UI can set progress denominator
223
- # socketio.emit("status", {"state": "queued", "total": total}, room=job_id)
224
- # socketio.start_background_task(run_job, job_id, job["accs"])
225
-
226
- # @socketio.on("leave")
227
- # def on_leave(data):
228
- # job_id = data.get("job_id")
229
- # if job_id:
230
- # leave_room(job_id)
231
-
232
- # @socketio.on("cancel")
233
- # def on_cancel(data):
234
- # job_id = data.get("job_id")
235
- # if job_id in CANCEL_FLAGS:
236
- # CANCEL_FLAGS[job_id] = True # flip the flag
237
- # emit("status", {"state": "cancelling"}, room=job_id)
238
 
239
  @app.route("/about")
240
  def about():
 
28
  app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)
29
 
30
  # # Socket.IO (logs on for debugging)
31
+ socketio = SocketIO(app, async_mode="eventlet", cors_allowed_origins="*")
32
 
33
 
34
  # --- job registry for cancel flags ---
 
171
  sources = out.get("Sources", "No Links")
172
  time_cost = out.get("Time cost") or f"{dt:.2f}s"
173
 
174
+ send = {
175
  "idx": i,
176
  "sample_id": sample_id,
177
  "predicted_country": predicted_country,
 
182
  "time_cost": time_cost,
183
  }
184
 
185
+ socketio.emit("row", send, room=room)
186
  socketio.sleep(0) # <- correct spelling; yield so the emit flushes
187
  send_log(f"Processed {acc} in {dt:.2f}s")
188
 
 
202
  CANCEL_FLAGS.pop(job_id, None)
203
  JOBS.pop(job_id, None) # <— tidy queued job
204
 
205
+ # ---- Socket.IO events ----
206
+ @socketio.on("connect")
207
+ def on_connect():
208
+ emit("connected", {"ok": True})
209
+
210
+ @socketio.on("join")
211
+ def on_join(data):
212
+ job_id = data.get("job_id")
213
+ if job_id:
214
+ join_room(job_id)
215
+ emit("joined", {"room": job_id})
216
+
217
+ # Start the job once the client is in the room
218
+ job = JOBS.get(job_id)
219
+ if job and not job["started"]:
220
+ job["started"] = True
221
+ total = len(job["accs"])
222
+ # Send an initial queued/total status so the UI can set progress denominator
223
+ socketio.emit("status", {"state": "queued", "total": total}, room=job_id)
224
+ socketio.start_background_task(run_job, job_id, job["accs"])
225
+
226
+ @socketio.on("leave")
227
+ def on_leave(data):
228
+ job_id = data.get("job_id")
229
+ if job_id:
230
+ leave_room(job_id)
231
+
232
+ @socketio.on("cancel")
233
+ def on_cancel(data):
234
+ job_id = data.get("job_id")
235
+ if job_id in CANCEL_FLAGS:
236
+ CANCEL_FLAGS[job_id] = True # flip the flag
237
+ emit("status", {"state": "cancelling"}, room=job_id)
238
 
239
  @app.route("/about")
240
  def about():
templates/Output.html CHANGED
@@ -75,7 +75,7 @@
75
 
76
  <h3>Live log</h3>
77
  <pre id="live-log" class="log mono" aria-live="polite" aria-atomic="false">
78
- <span class="logline muted">Waiting for messages…</span>
79
  </pre>
80
  </div>
81
 
@@ -209,6 +209,10 @@
209
  }
210
  function appendLog(line) {
211
  if (!els.log) return;
 
 
 
 
212
  const span = document.createElement('span');
213
  span.className = 'logline';
214
  span.textContent = line;
 
75
 
76
  <h3>Live log</h3>
77
  <pre id="live-log" class="log mono" aria-live="polite" aria-atomic="false">
78
+ <span id="log-placeholder" class="logline muted">Waiting for messages…</span>
79
  </pre>
80
  </div>
81
 
 
209
  }
210
  function appendLog(line) {
211
  if (!els.log) return;
212
+
213
+ const ph = document.getElementById('log-placeholder');
214
+ if (ph) ph.remove(); // drop the placeholder once
215
+
216
  const span = document.createElement('span');
217
  span.className = 'logline';
218
  span.textContent = line;