Nicha1234 commited on
Commit
27c5cb9
·
verified ·
1 Parent(s): a733e54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import numpy as np
 
3
  import scipy.io
4
  import zipfile
5
  import os
@@ -116,6 +117,20 @@ def get_image_from_plot(fig):
116
  plt.close(fig)
117
  return buf.getvalue() # Return bytes — reliably cacheable by st.cache_data
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  @st.cache_data
120
  def draw_kspace_diagram():
121
  fig, ax = plt.subplots(figsize=(6, 6))
@@ -318,7 +333,7 @@ with col_main:
318
 
319
  _, col_img_center, _ = st.columns([2.5, 3, 2.5])
320
  with col_img_center:
321
- st.image(draw_kspace_diagram(), use_container_width=True)
322
 
323
  # ---------------------------------------------------------
324
  # K-Space Trajectories
@@ -366,9 +381,9 @@ with col_main:
366
  # ถอด st.empty ออกให้หมดเพื่อให้ Streamlit จัดการ Layout ปกติ ภาพจะไม่กระโดดยุบตัว
367
  col_anim1, col_anim2 = st.columns([1, 1])
368
  with col_anim1:
369
- st.image(draw_pulse_sequence(st.session_state.fill_step, total_anim_steps), use_container_width=True)
370
  with col_anim2:
371
- st.image(draw_kspace_filling(st.session_state.fill_step, total_anim_steps), use_container_width=True)
372
 
373
  st.markdown("""
374
  ---
@@ -407,9 +422,9 @@ with col_main:
407
 
408
  _, col_img1, col_img2, _ = st.columns([0.5, 2.5, 2.5, 0.5])
409
  with col_img1:
410
- st.image(draw_kspace_point(kx_val, ky_val, kspace_bg_image), caption="พิกัด K-Space (มีเส้นบอกทิศทาง)", use_container_width=True)
411
  with col_img2:
412
- st.image(draw_wave(kx_val, ky_val), caption="แผ่นลวดลายคลื่น (2D Sinusoidal Wave)", use_container_width=True)
413
 
414
  with st.expander("🔍 สังเกตลักษณะคลื่น (คลิกเพื่อดูคำอธิบายเพิ่มเติม)"):
415
  st.markdown("""
@@ -462,9 +477,9 @@ with col_main:
462
 
463
  _, col_fimg1, col_fimg2, _ = st.columns([0.5, 2.5, 2.5, 0.5])
464
  with col_fimg1:
465
- st.image(draw_filtered_kspace(filtered_k), caption="ภาพ K-Space ที่ถูกเลือก", use_container_width=True)
466
  with col_fimg2:
467
- st.image(draw_mri(mri_result), caption="ภาพ MRI ผลลัพธ์", use_container_width=True)
468
 
469
  # Spacer: keeps page taller than viewport at all zoom levels so the
470
  # scrollbar never disappears and never causes a layout-shift jiggle.
 
1
  import streamlit as st
2
  import numpy as np
3
+ import base64
4
  import scipy.io
5
  import zipfile
6
  import os
 
117
  plt.close(fig)
118
  return buf.getvalue() # Return bytes — reliably cacheable by st.cache_data
119
 
120
+ def st_image(img_bytes, caption=None):
121
+ """Display image as inline base64 data URI.
122
+ Prevents layout jiggle: browser renders it instantly with no HTTP round-trip,
123
+ so no 0-height placeholder → image-pop-in → scrollbar-flicker cycle."""
124
+ b64 = base64.b64encode(img_bytes).decode('utf-8')
125
+ cap_html = (f'<p style="text-align:center;font-size:14px;color:gray;margin-top:4px">'
126
+ + caption + '</p>') if caption else ''
127
+ st.markdown(
128
+ f'<div style="width:100%">'
129
+ f'<img src="data:image/png;base64,{b64}" style="width:100%;height:auto;display:block">'
130
+ f'{cap_html}</div>',
131
+ unsafe_allow_html=True
132
+ )
133
+
134
  @st.cache_data
135
  def draw_kspace_diagram():
136
  fig, ax = plt.subplots(figsize=(6, 6))
 
333
 
334
  _, col_img_center, _ = st.columns([2.5, 3, 2.5])
335
  with col_img_center:
336
+ st_image(draw_kspace_diagram())
337
 
338
  # ---------------------------------------------------------
339
  # K-Space Trajectories
 
381
  # ถอด st.empty ออกให้หมดเพื่อให้ Streamlit จัดการ Layout ปกติ ภาพจะไม่กระโดดยุบตัว
382
  col_anim1, col_anim2 = st.columns([1, 1])
383
  with col_anim1:
384
+ st_image(draw_pulse_sequence(st.session_state.fill_step, total_anim_steps))
385
  with col_anim2:
386
+ st_image(draw_kspace_filling(st.session_state.fill_step, total_anim_steps))
387
 
388
  st.markdown("""
389
  ---
 
422
 
423
  _, col_img1, col_img2, _ = st.columns([0.5, 2.5, 2.5, 0.5])
424
  with col_img1:
425
+ st_image(draw_kspace_point(kx_val, ky_val, kspace_bg_image), caption="พิกัด K-Space (มีเส้นบอกทิศทาง)")
426
  with col_img2:
427
+ st_image(draw_wave(kx_val, ky_val), caption="แผ่นลวดลายคลื่น (2D Sinusoidal Wave)")
428
 
429
  with st.expander("🔍 สังเกตลักษณะคลื่น (คลิกเพื่อดูคำอธิบายเพิ่มเติม)"):
430
  st.markdown("""
 
477
 
478
  _, col_fimg1, col_fimg2, _ = st.columns([0.5, 2.5, 2.5, 0.5])
479
  with col_fimg1:
480
+ st_image(draw_filtered_kspace(filtered_k), caption="ภาพ K-Space ที่ถูกเลือก")
481
  with col_fimg2:
482
+ st_image(draw_mri(mri_result), caption="ภาพ MRI ผลลัพธ์")
483
 
484
  # Spacer: keeps page taller than viewport at all zoom levels so the
485
  # scrollbar never disappears and never causes a layout-shift jiggle.