jianghuyihei commited on
Commit
bfd73ac
1 Parent(s): a8a4f77
Files changed (1) hide show
  1. app.py +26 -2
app.py CHANGED
@@ -320,9 +320,14 @@ html_template = """
320
  userId = data.user_id;
321
  sessionStorage.setItem("user_id", userId);
322
  }
 
 
 
 
323
  console.log("set User ID:", userId, "state:", state);
324
  }
325
  window.onload = getUserId;
 
326
  </script>
327
  </body>
328
  </html>
@@ -384,6 +389,16 @@ def form_post(request: Request,response: Response,topic: str = Form(...)):
384
  response.set_cookie(key="user_id", value=user_id)
385
  response.set_cookie(key="state", value=state)
386
 
 
 
 
 
 
 
 
 
 
 
387
  print(f"current user_id={user_id}, state={state}")
388
  loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
389
  if state == "generate":
@@ -396,7 +411,7 @@ def form_post(request: Request,response: Response,topic: str = Form(...)):
396
  new_state = "continue"
397
  new_button_text = "Continue"
398
  response.set_cookie(key="state", value=new_state)
399
- return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text=new_button_text,loading_text=f"Generating content, Usually takes {(queue_len+1)*3}-{(queue_len+1)*4} minutes, please wait...")
400
 
401
  queue.put([user_id,topic])
402
  new_state = "generate"
@@ -404,6 +419,15 @@ def form_post(request: Request,response: Response,topic: str = Form(...)):
404
  response.set_cookie(key="state", value=new_state)
405
  queue_len = queue.qsize()
406
 
 
 
 
 
 
 
 
 
 
407
  # 判断当前是否轮到该用户,如果没轮到则一直等待到轮到为止
408
  while queue.queue[0] != [user_id,topic]:
409
  time.sleep(10)
@@ -439,4 +463,4 @@ def form_post(request: Request,response: Response,topic: str = Form(...)):
439
  error_message = str(e)
440
  # 从队列中移除当前用户
441
  queue.get()
442
- return Template(html_template).render(idea=idea, error=error_message, reply_count=reply_count, time_taken=time_taken,button_text=new_button_text,loading_text=loading_text)
 
320
  userId = data.user_id;
321
  sessionStorage.setItem("user_id", userId);
322
  }
323
+ if (!state) {
324
+ state = "generate";
325
+ sessionStorage.setItem("state", state);
326
+ }
327
  console.log("set User ID:", userId, "state:", state);
328
  }
329
  window.onload = getUserId;
330
+ {{ script}}
331
  </script>
332
  </body>
333
  </html>
 
389
  response.set_cookie(key="user_id", value=user_id)
390
  response.set_cookie(key="state", value=state)
391
 
392
+ script_template = f"""
393
+ <script>
394
+ // 在页面加载时更新 sessionStorage
395
+ window.onload = function() {{
396
+ sessionStorage.setItem("user_id", "{user_id}");
397
+ sessionStorage.setItem("state", "{new_state}");
398
+ }};
399
+ </script>
400
+ """
401
+
402
  print(f"current user_id={user_id}, state={state}")
403
  loading_text = "Generating content, Usually takes 3-4 minutes, please wait..."
404
  if state == "generate":
 
411
  new_state = "continue"
412
  new_button_text = "Continue"
413
  response.set_cookie(key="state", value=new_state)
414
+ return Template(html_template).render(idea="", error=error_message, reply_count=reply_count, button_text=new_button_text,loading_text=f"Generating content, Usually takes {(queue_len+1)*3}-{(queue_len+1)*4} minutes, please wait...",script=script_template)
415
 
416
  queue.put([user_id,topic])
417
  new_state = "generate"
 
419
  response.set_cookie(key="state", value=new_state)
420
  queue_len = queue.qsize()
421
 
422
+ script_template = f"""
423
+ <script>
424
+ // 在页面加载时更新 sessionStorage
425
+ window.onload = function() {{
426
+ sessionStorage.setItem("user_id", "{user_id}");
427
+ sessionStorage.setItem("state", "{new_state}");
428
+ }};
429
+ </script>
430
+ """
431
  # 判断当前是否轮到该用户,如果没轮到则一直等待到轮到为止
432
  while queue.queue[0] != [user_id,topic]:
433
  time.sleep(10)
 
463
  error_message = str(e)
464
  # 从队列中移除当前用户
465
  queue.get()
466
+ return Template(html_template).render(idea=idea, error=error_message, reply_count=reply_count, time_taken=time_taken,button_text=new_button_text,loading_text=loading_text,script=script_template)