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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -26
app.py CHANGED
@@ -22,42 +22,128 @@ image_data = [
22
  # 初始化 session state 用於追蹤當前頁面
23
  if 'page' not in st.session_state:
24
  st.session_state.page = 0
 
 
25
 
26
- # 使用容器來控制圖片和點擊區域的佈局
27
- with st.container():
28
- # 顯示當前頁面的圖片,全屏顯示
29
- st.image(image_data[st.session_state.page]["path"],
30
- caption=image_data[st.session_state.page]["caption"],
31
- use_container_width=True)
32
-
33
- # 自訂 CSS 樣式,將點擊區域設為透明並覆蓋圖片左右兩側
34
- st.markdown("""
35
  <style>
36
- .click-area {
37
- position: absolute;
 
38
  top: 0;
 
 
39
  height: 100%;
40
- width: 30%; /* 左右各占 30% 的點擊區域 */
41
- background-color: rgba(0, 0, 0, 0); /* 完全透明 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  cursor: pointer;
43
  }
44
- .left-click {
45
- left: 0;
46
  }
47
- .right-click {
48
- right: 0;
 
 
 
 
 
 
49
  }
50
  </style>
51
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- # 分成左右兩欄作為點擊區域
54
- col_left, col_center, col_right = st.columns([1, 2, 1]) # 左右各占 1,中間占 2
55
- with col_left:
56
- if st.button("", key="prev_area", help="點擊左側回到上一頁") and st.session_state.page > 0:
57
- st.session_state.page -= 1
58
- with col_right:
59
- if st.button("", key="next_area", help="點擊右側前往下一頁") and st.session_state.page < len(image_data) - 1:
60
- st.session_state.page += 1
61
 
62
  # 顯示當前頁數
63
  st.write(f"頁面: {st.session_state.page + 1} / {len(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)}")