Lashtw commited on
Commit
a042bc2
·
verified ·
1 Parent(s): d0d2bc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -118
app.py CHANGED
@@ -22,132 +22,29 @@ image_data = [
22
  # 初始化 session state 用於追蹤當前頁面
23
  if 'page' not in st.session_state:
24
  st.session_state.page = 0
25
- if 'fullscreen' not in st.session_state:
26
- st.session_state.fullscreen = False
27
 
28
- # 自訂全屏和換頁功能
29
- st.markdown("""
30
- <style>
31
- .fullscreen-container {
32
- display: none;
33
- position: fixed;
34
- top: 0;
35
- left: 0;
36
- width: 100%;
37
- height: 100%;
38
- background: #000;
39
- z-index: 1000;
40
- justify-content: center;
41
- align-items: center;
42
- }
43
- .fullscreen-container.active {
44
- display: flex;
45
- }
46
- .fullscreen-image {
47
- max-width: 100%;
48
- max-height: 100%;
49
- object-fit: contain;
50
- }
51
- .caption {
52
- position: absolute;
53
- bottom: 10px;
54
- left: 50%;
55
- transform: translateX(-50%);
56
- color: white;
57
- font-size: 20px;
58
- }
59
- .nav-buttons {
60
- position: absolute;
61
- top: 50%;
62
- width: 100%;
63
- display: flex;
64
- justify-content: space-between;
65
- padding: 0 20px;
66
- transform: translateY(-50%);
67
- }
68
- .nav-buttons button {
69
- background-color: rgba(255, 255, 255, 0.7);
70
- border: none;
71
- padding: 10px 20px;
72
- font-size: 16px;
73
- cursor: pointer;
74
- }
75
- .nav-buttons button:hover {
76
- background-color: rgba(255, 255, 255, 1);
77
- }
78
- .exit-fullscreen {
79
- position: absolute;
80
- top: 10px;
81
- right: 10px;
82
- background-color: rgba(255, 255, 255, 0.7);
83
- border: none;
84
- padding: 5px 10px;
85
- cursor: pointer;
86
- }
87
- </style>
88
- <div class="fullscreen-container" id="fullscreen-container">
89
- <img class="fullscreen-image" id="fullscreen-image" src="">
90
- <div class="caption" id="fullscreen-caption"></div>
91
- <div class="nav-buttons">
92
- <button onclick="changePage(-1)">上一頁</button>
93
- <button onclick="changePage(1)">下一頁</button>
94
- </div>
95
- <button class="exit-fullscreen" onclick="exitFullscreen()">退出全屏</button>
96
- </div>
97
- <script>
98
- function toggleFullscreen() {
99
- const container = document.getElementById('fullscreen-container');
100
- const image = document.getElementById('fullscreen-image');
101
- const caption = document.getElementById('fullscreen-caption');
102
- const page = parseInt(window.parent.document.getElementById('page').value);
103
- image.src = window.parent.streamlitDocStore.get('image_data')[page]['path'];
104
- caption.textContent = window.parent.streamlitDocStore.get('image_data')[page]['caption'];
105
- container.classList.toggle('active');
106
- window.parent.document.getElementById('fullscreen').value = container.classList.contains('active');
107
- window.parent.streamlitDocStore.setRerun();
108
- }
109
-
110
- function changePage(direction) {
111
- let page = parseInt(window.parent.document.getElementById('page').value);
112
- const maxPage = window.parent.streamlitDocStore.get('image_data').length - 1;
113
- page = Math.max(0, Math.min(maxPage, page + direction));
114
- window.parent.document.getElementById('page').value = page;
115
- toggleFullscreen(); // 切換後重新載入圖片
116
- }
117
-
118
- function exitFullscreen() {
119
- const container = document.getElementById('fullscreen-container');
120
- container.classList.remove('active');
121
- window.parent.document.getElementById('fullscreen').value = false;
122
- window.parent.streamlitDocStore.setRerun();
123
- }
124
-
125
- // 監聽鍵盤事件(可選)
126
- document.addEventListener('keydown', (event) => {
127
- if (document.getElementById('fullscreen-container').classList.contains('active')) {
128
- if (event.key === 'ArrowLeft') changePage(-1);
129
- if (event.key === 'ArrowRight') changePage(1);
130
- if (event.key === 'Escape') exitFullscreen();
131
- }
132
- });
133
- </script>
134
- <input type="hidden" id="page" value="0">
135
- <input type="hidden" id="fullscreen" value="false">
136
- <button onclick="toggleFullscreen()">點擊進入全屏</button>
137
- """, unsafe_allow_html=True)
138
-
139
- # 根據 session state 控制全屏狀態
140
- if st.session_state.fullscreen:
141
- st.markdown(f'<script>toggleFullscreen()</script>', unsafe_allow_html=True)
142
-
143
- # 顯示當前頁面的圖片(非全屏模式)
144
  st.image(image_data[st.session_state.page]["path"],
145
  caption=image_data[st.session_state.page]["caption"],
146
  use_container_width=True)
147
 
 
 
 
 
 
 
 
 
 
 
 
148
  # 顯示當前頁數
149
  st.write(f"頁面: {st.session_state.page + 1} / {len(image_data)}")
150
 
 
 
 
151
  # 分隔線
152
  st.markdown("---")
153
 
 
22
  # 初始化 session state 用於追蹤當前頁面
23
  if 'page' not in st.session_state:
24
  st.session_state.page = 0
 
 
25
 
26
+ # 顯示當前頁面的圖片,使用 Streamlit 原生全屏
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  st.image(image_data[st.session_state.page]["path"],
28
  caption=image_data[st.session_state.page]["caption"],
29
  use_container_width=True)
30
 
31
+ # 換頁按鈕
32
+ col1, col2 = st.columns(2)
33
+ with col1:
34
+ if st.button("上一頁", disabled=st.session_state.page == 0):
35
+ st.session_state.page -= 1
36
+ st.experimental_rerun()
37
+ with col2:
38
+ if st.button("下一頁", disabled=st.session_state.page == len(image_data) - 1):
39
+ st.session_state.page += 1
40
+ st.experimental_rerun()
41
+
42
  # 顯示當前頁數
43
  st.write(f"頁面: {st.session_state.page + 1} / {len(image_data)}")
44
 
45
+ # 提示使用鍵盤換頁(全屏模式下)
46
+ st.write("**全屏模式提示**:進入全屏後,可使用鍵盤的 ←(上一頁)或 →(下一頁)切換。")
47
+
48
  # 分隔線
49
  st.markdown("---")
50