Rathapoom commited on
Commit
bc7d842
·
verified ·
1 Parent(s): c2f8555

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -15
app.py CHANGED
@@ -2460,6 +2460,64 @@ def show_main_interface():
2460
  # Save options
2461
  show_save_options()
2462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2463
  def show_completion_options():
2464
  """Display options and summary when story is completed"""
2465
  try:
@@ -2487,9 +2545,9 @@ def show_completion_options():
2487
 
2488
  # Show story statistics
2489
  st.markdown("### 📊 สรุปเรื่องราว")
2490
- col1, col2 = st.columns(2)
2491
 
2492
- with col1:
 
2493
  st.metric(
2494
  "จำนวนประโยคทั้งหมด",
2495
  len(st.session_state.story),
@@ -2501,7 +2559,7 @@ def show_completion_options():
2501
  help="จำนวนคำศัพท์ที่ไม่ซ้ำกัน"
2502
  )
2503
 
2504
- with col2:
2505
  st.metric(
2506
  "คะแนนรวม",
2507
  st.session_state.points['total'],
@@ -2513,21 +2571,22 @@ def show_completion_options():
2513
  help="อัตราการเขียนถูกต้อง"
2514
  )
2515
 
2516
- # Show action buttons with unique keys
2517
  st.markdown("### 🎯 ต้องการทำอะไรต่อ?")
2518
- col1, col2 = st.columns(2)
2519
 
2520
- with col1:
2521
- if st.button("💾 บันทึกเรื่องราว",
2522
- key="save_story_completion",
2523
- use_container_width=True):
2524
- show_save_dialog()
2525
 
2526
- with col2:
2527
- if st.button("🔄 เริ่มเรื่องใหม่",
2528
- key="new_story_completion",
2529
- use_container_width=True):
2530
- reset_story_with_confirmation()
 
 
 
2531
 
2532
  except Exception as e:
2533
  logging.error(f"Error showing completion options: {str(e)}")
 
2460
  # Save options
2461
  show_save_options()
2462
 
2463
+ def save_completed_story():
2464
+ """Save completed story with all available formats"""
2465
+ try:
2466
+ st.markdown("### 💾 บันทึกเรื่องราวที่เสร็จสมบูรณ์")
2467
+
2468
+ # PDF Option
2469
+ if st.button("📑 บันทึกเป็น PDF",
2470
+ key="save_completed_pdf",
2471
+ use_container_width=True):
2472
+ try:
2473
+ pdf = create_story_pdf()
2474
+ st.download_button(
2475
+ "📥 ดาวน์โหลด PDF",
2476
+ data=pdf,
2477
+ file_name=f"completed_story_{datetime.now().strftime('%Y%m%d')}.pdf",
2478
+ mime="application/pdf",
2479
+ key="download_completed_pdf"
2480
+ )
2481
+ st.success("สร้าง PDF เรียบร้อยแล้ว!")
2482
+ except Exception as e:
2483
+ logging.error(f"Error creating PDF: {str(e)}")
2484
+ st.error("เกิดข้อผิดพลาดในการสร้างไฟล์ PDF")
2485
+
2486
+ # JSON Progress Save
2487
+ if st.button("💾 บันทึกความก้าวหน้า",
2488
+ key="save_completed_progress",
2489
+ use_container_width=True):
2490
+ try:
2491
+ story_data = {
2492
+ 'timestamp': datetime.now().isoformat(),
2493
+ 'level': st.session_state.level,
2494
+ 'story': st.session_state.story,
2495
+ 'achievements': st.session_state.achievements,
2496
+ 'stats': {
2497
+ key: list(value) if isinstance(value, set) else value
2498
+ for key, value in st.session_state.stats.items()
2499
+ },
2500
+ 'points': st.session_state.points,
2501
+ 'ending_type': st.session_state.ending_type,
2502
+ 'is_completed': True
2503
+ }
2504
+
2505
+ st.download_button(
2506
+ "📥 ดาวน์โหลดไฟล์บันทึก",
2507
+ data=json.dumps(story_data, ensure_ascii=False, indent=2),
2508
+ file_name=f"completed_story_{datetime.now().strftime('%Y%m%d')}.json",
2509
+ mime="application/json",
2510
+ key="download_completed_json"
2511
+ )
2512
+ st.success("สร้างไฟล์บันทึกเรียบร้อยแล้ว!")
2513
+ except Exception as e:
2514
+ logging.error(f"Error saving progress: {str(e)}")
2515
+ st.error("เกิดข้อผิดพลาดในการบันทึกความก้าวหน้า")
2516
+
2517
+ except Exception as e:
2518
+ logging.error(f"Error saving completed story: {str(e)}")
2519
+ st.error("เกิดข้อผิดพลาดในการบันทึกเรื่องราว")
2520
+
2521
  def show_completion_options():
2522
  """Display options and summary when story is completed"""
2523
  try:
 
2545
 
2546
  # Show story statistics
2547
  st.markdown("### 📊 สรุปเรื่องราว")
 
2548
 
2549
+ metrics_col1, metrics_col2 = st.columns(2)
2550
+ with metrics_col1:
2551
  st.metric(
2552
  "จำนวนประโยคทั้งหมด",
2553
  len(st.session_state.story),
 
2559
  help="จำนวนคำศัพท์ที่ไม่ซ้ำกัน"
2560
  )
2561
 
2562
+ with metrics_col2:
2563
  st.metric(
2564
  "คะแนนรวม",
2565
  st.session_state.points['total'],
 
2571
  help="อัตราการเขียนถูกต้อง"
2572
  )
2573
 
2574
+ # Show action buttons (without nested columns)
2575
  st.markdown("### 🎯 ต้องการทำอะไรต่อ?")
 
2576
 
2577
+ if st.button("💾 บันทึกเรื่องราว",
2578
+ key="save_final_story",
2579
+ use_container_width=True):
2580
+ save_completed_story()
 
2581
 
2582
+ if st.button("🔄 เริ่มเรื่องใหม่",
2583
+ key="start_new_story",
2584
+ use_container_width=True):
2585
+ if st.checkbox("✅ ยืนยันการเริ่มใหม่",
2586
+ key="confirm_new_story"):
2587
+ reset_story()
2588
+ st.success("เริ่มต้นใหม่เรียบร้อยแล้ว!")
2589
+ st.rerun()
2590
 
2591
  except Exception as e:
2592
  logging.error(f"Error showing completion options: {str(e)}")