DeepLearning101 commited on
Commit
0fdf933
·
verified ·
1 Parent(s): ec2eb9c

JS 強制介入版

Browse files

CSS:負責「定寬」和「換行」(這部分您之前滿意了)。
JavaScript:負責「抓出那個撐大的外層容器」,直接把它的捲軸殺掉。

Files changed (1) hide show
  1. app.py +49 -39
app.py CHANGED
@@ -131,45 +131,54 @@ def check_login(user, password):
131
  error_msg: "<span style='color: red'>❌ 帳號或密碼錯誤</span>"
132
  }
133
 
134
- # --- 🔥 [結構穿透 CSS]:精準殺死中層捲軸 🔥 ---
135
- custom_css = """
136
- /* 1. 【全域鎖死】
137
- 防止整個頁面因為表格太寬而產生瀏覽器級別的水平捲軸
138
- */
139
- body, .gradio-container {
140
- overflow-x: hidden !important;
141
- max-width: 100vw !important;
142
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
- /* 2. 【ID層鎖死】
145
- 鎖定我們指定的元件外層
146
- */
147
- #booking_table {
148
- overflow: hidden !important;
149
- max-width: 100% !important;
150
- border: none !important;
151
  }
 
152
 
153
- /* 3. 🔥【關鍵修正:殺死中間層】🔥
154
- 選取 #booking_table 底下的「直接子元素 div」。
155
- 這通常就是那個帶有 'wrap' class 且預設 overflow:auto 的傢伙。
156
- 我們強制把它隱藏,這就是您一直在找的「外層捲軸」控制點!
157
- */
158
- #booking_table > div {
159
- overflow: hidden !important;
160
  border: none !important;
161
  }
162
 
163
- /* 4. 【復活內層】
164
- 找到真正包含 table 的那一層 (通常是 .table-wrap),只有這裡可以捲動。
165
- */
166
- #booking_table .table-wrap {
167
- overflow-x: auto !important;
168
- overflow-y: hidden !important;
169
- border: 1px solid #444 !important;
170
- }
171
-
172
- /* 5. 【表格定寬】(您的設定) */
173
  #booking_table table {
174
  table-layout: fixed !important;
175
  width: 1500px !important;
@@ -178,13 +187,11 @@ body, .gradio-container {
178
  margin: 0 !important;
179
  }
180
 
181
- /* 6. 【欄位行為】 */
182
  #booking_table th, #booking_table td {
183
- display: table-cell !important;
184
- white-space: normal !important;
185
- overflow-wrap: break-word !important;
186
  word-break: break-all !important;
187
-
188
  vertical-align: top !important;
189
  box-sizing: border-box !important;
190
  padding: 8px 5px !important;
@@ -193,7 +200,7 @@ body, .gradio-container {
193
  line-height: 1.4 !important;
194
  }
195
 
196
- /* 7. 【欄位寬度】 */
197
  #booking_table th:nth-child(1), #booking_table td:nth-child(1) { width: 60px !important; }
198
  #booking_table th:nth-child(2), #booking_table td:nth-child(2) { width: 170px !important; }
199
  #booking_table th:nth-child(3), #booking_table td:nth-child(3) { width: 80px !important; }
@@ -240,6 +247,9 @@ with gr.Blocks(title="Admin", css=custom_css) as demo:
240
  inputs=[username_input, password_input],
241
  outputs=[login_row, admin_row, error_msg]
242
  )
 
 
 
243
 
244
  if __name__ == "__main__":
245
  demo.launch()
 
131
  error_msg: "<span style='color: red'>❌ 帳號或密碼錯誤</span>"
132
  }
133
 
134
+ # --- 🟢 [JavaScript] 用來強制殺死外層捲軸 ---
135
+ # 這個 JS 會在頁面載入後,每 0.5 秒檢查一次,強制把那些不受控的 wrapper 鎖死
136
+ kill_scroll_js = """
137
+ function() {
138
+ // 定義一個修復函數
139
+ const fixScroll = () => {
140
+ const tableComponent = document.querySelector('#booking_table');
141
+ if (!tableComponent) return;
142
+
143
+ // 1. 找到所有中間層 (wrapper)
144
+ // 這些 div 通常是 Gradio 自動生成的,就是它們在搞鬼
145
+ const wrappers = tableComponent.querySelectorAll('div');
146
+
147
+ wrappers.forEach(div => {
148
+ // 如果這個 div 不是真正包著 table 的那個 (table-wrap)
149
+ // 而且它有水平捲軸,就強制殺掉它
150
+ if (!div.classList.contains('table-wrap') && div.scrollWidth > div.clientWidth) {
151
+ div.style.overflowX = 'hidden';
152
+ div.style.maxWidth = '100vw'; // 強制不准超過螢幕
153
+ }
154
+ });
155
+
156
+ // 2. 確保最內層可以捲動
157
+ const innerScroller = tableComponent.querySelector('.table-wrap');
158
+ if (innerScroller) {
159
+ innerScroller.style.overflowX = 'auto';
160
+ innerScroller.style.maxWidth = '100vw'; // 確保捲動容器本身也不超過螢幕
161
+ }
162
+ };
163
 
164
+ // 立即執行一次
165
+ fixScroll();
166
+
167
+ // 每 500ms 執行一次,以防 Gradio 重新渲染後失效
168
+ setInterval(fixScroll, 500);
 
 
169
  }
170
+ """
171
 
172
+ # --- 🔥 [CSS] 定寬 + 換行設定 ---
173
+ custom_css = """
174
+ /* 1. 元件外框鎖死 */
175
+ #booking_table {
176
+ max-width: 100vw !important;
177
+ overflow: hidden !important;
 
178
  border: none !important;
179
  }
180
 
181
+ /* 2. 表格內容設定 */
 
 
 
 
 
 
 
 
 
182
  #booking_table table {
183
  table-layout: fixed !important;
184
  width: 1500px !important;
 
187
  margin: 0 !important;
188
  }
189
 
190
+ /* 3. 欄位設定:允許換行 */
191
  #booking_table th, #booking_table td {
192
+ white-space: normal !important;
 
 
193
  word-break: break-all !important;
194
+ overflow-wrap: break-word !important;
195
  vertical-align: top !important;
196
  box-sizing: border-box !important;
197
  padding: 8px 5px !important;
 
200
  line-height: 1.4 !important;
201
  }
202
 
203
+ /* 4. 🔥 個別欄位寬度 (您指定的) */
204
  #booking_table th:nth-child(1), #booking_table td:nth-child(1) { width: 60px !important; }
205
  #booking_table th:nth-child(2), #booking_table td:nth-child(2) { width: 170px !important; }
206
  #booking_table th:nth-child(3), #booking_table td:nth-child(3) { width: 80px !important; }
 
247
  inputs=[username_input, password_input],
248
  outputs=[login_row, admin_row, error_msg]
249
  )
250
+
251
+ # 🔥🔥🔥 關鍵:頁面載入時執行 JS 來修復捲軸 🔥🔥🔥
252
+ demo.load(None, js=kill_scroll_js)
253
 
254
  if __name__ == "__main__":
255
  demo.launch()