schillerCFK commited on
Commit
340f0c7
·
verified ·
1 Parent(s): 8742830

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -27
app.py CHANGED
@@ -89,7 +89,7 @@ def draw_coaxiality_plot(distance, r):
89
  return fig
90
 
91
  def calc_angle_from_offset(distance, dist_unit, offset, off_type, off_unit):
92
- if distance <= 0: return "❌ 距離必須大於 0", None
93
 
94
  # 統一將測試距離轉換為 mm 計算
95
  if dist_unit == "公分 (cm)":
@@ -114,14 +114,15 @@ def calc_angle_from_offset(distance, dist_unit, offset, off_type, off_unit):
114
  f"- 輸入條件: 距離 **{distance} {dist_unit}** ({distance_mm:.2f} mm), 偏移 **{offset} {off_unit}** ({off_type})\n"
115
  f"- 實際半徑偏移 (r): **{r:.4f} mm**\n"
116
  f"- 偏移角度: **{deg:.4f}°**\n"
117
- f"- 同軸度: **{mrad:.4f} mRad**"
 
118
  )
119
 
120
  fig = draw_coaxiality_plot(distance_mm, r)
121
- return text_result, fig
122
 
123
  def calc_offset_from_angle(distance, dist_unit, angle, ang_type):
124
- if distance <= 0: return "❌ 距離必須大於 0", None
125
 
126
  # 統一將測試距離轉換為 mm 計算
127
  if dist_unit == "公分 (cm)":
@@ -151,17 +152,18 @@ def calc_offset_from_angle(distance, dist_unit, angle, ang_type):
151
  f"- 輸入條件: 距離 **{distance} {dist_unit}** ({distance_mm:.2f} mm), 角度 **{angle}** ({ang_type})\n"
152
  f"- **半徑長度** (半徑偏移): **{r:.4f} mm** / **{r_um:.2f} µm**\n"
153
  f"- **直徑長度** (直徑偏移): **{diameter:.4f} mm** / **{diameter_um:.2f} µm**\n"
154
- f"- 等效 mRad: **{mrad:.4f} mRad**"
 
155
  )
156
 
157
  fig = draw_coaxiality_plot(distance_mm, r)
158
- return text_result, fig
159
 
160
  # --- 光束發散 (Beam Divergence) 邏輯 ---
161
 
162
  def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
163
- if z1 >= z2: return "❌ 錯誤:第二個位置 (z2) 必須大於第一個位置 (z1)", None
164
- if d1 < 0 or d2 < 0: return "❌ 錯誤:光斑尺寸不能為負數", None
165
 
166
  delta_L = z2 - z1
167
 
@@ -188,7 +190,8 @@ def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
188
  f"- 半徑尺寸差 (${{\\Delta r}}$): **{delta_r:.4f} mm**\n\n"
189
  f"#### 📐 角度結果\n"
190
  f"- **半角發散 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°** / **{half_mrad:.4f} mRad**\n"
191
- f"- **全角發散 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°** / **{full_mrad:.4f} mRad**"
 
192
  )
193
 
194
  # 繪製圖表
@@ -225,13 +228,13 @@ def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
225
  ax.axis('off')
226
 
227
  plt.tight_layout()
228
- return text_result, fig
229
 
230
  # --- 光束焦距收斂 (Focus Beam Divergence) 邏輯 ---
231
 
232
  def calculate_and_plot_focus_divergence(focal_length, dist_unit, d1, d2, size_type):
233
- if focal_length <= 0: return "❌ 錯誤:焦距必須大於 0", None
234
- if d1 < 0 or d2 < 0: return "❌ 錯誤:光斑尺寸不能為負數", None
235
 
236
  # 單位轉換至 mm
237
  focal_length_mm = focal_length * 10.0 if dist_unit == "公分 (cm)" else (focal_length * 1000.0 if dist_unit == "公尺 (m)" else focal_length)
@@ -261,7 +264,8 @@ def calculate_and_plot_focus_divergence(focal_length, dist_unit, d1, d2, size_ty
261
  f"- 半徑收斂差 ($r_1 - r_2$): **{delta_r:.4f} mm**\n\n"
262
  f"#### 📐 收斂角度結果\n"
263
  f"- **半收斂角 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°** / **{half_mrad:.4f} mRad**\n"
264
- f"- **全收斂角 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°** / **{full_mrad:.4f} mRad**"
 
265
  )
266
 
267
  # 繪製圖表
@@ -298,14 +302,14 @@ def calculate_and_plot_focus_divergence(focal_length, dist_unit, d1, d2, size_ty
298
  ax.axis('off')
299
 
300
  plt.tight_layout()
301
- return text_result, fig
302
 
303
 
304
  # --- 線光斑長度與角度 (Line Laser Angle) 邏輯 ---
305
 
306
  def calculate_and_plot_line_angle(distance, dist_unit, line_length, length_unit):
307
- if distance <= 0: return "❌ 錯誤:距離必須大於 0", None
308
- if line_length < 0: return "❌ 錯誤:線長不能為負數", None
309
 
310
  # 單位轉換至 mm
311
  dist_mm = distance * 10.0 if dist_unit == "公分 (cm)" else (distance * 1000.0 if dist_unit == "公尺 (m)" else distance)
@@ -328,7 +332,8 @@ def calculate_and_plot_line_angle(distance, dist_unit, line_length, length_unit)
328
  f"- 半線長 ($l/2$): **{half_l:.2f} mm**\n\n"
329
  f"#### 📐 發散角度結果\n"
330
  f"- **半角 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°**\n"
331
- f"- **發散全角 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°**"
 
332
  )
333
 
334
  # 繪製圖表
@@ -358,7 +363,8 @@ def calculate_and_plot_line_angle(distance, dist_unit, line_length, length_unit)
358
  ax.axis('off')
359
 
360
  plt.tight_layout()
361
- return text_result, fig
 
362
 
363
 
364
  # --- 更新輸入框標籤函數 ---
@@ -710,24 +716,24 @@ with gr.Blocks(title=COMPANY_NAME) as demo:
710
  add_w_btn.click(lambda n, c: (gr.update(choices=c+[n], value=n), c+[n], f"已新增: {n}"), [new_w_input, ws_state], [w_select, ws_state, order_status])
711
  download_btn.click(get_backup_files, outputs=file_output)
712
 
713
- # 同軸度計算
714
  dist_unit_1.change(update_dist_label, inputs=dist_unit_1, outputs=dist_input_1)
715
  dist_unit_2.change(update_dist_label, inputs=dist_unit_2, outputs=dist_input_2)
716
  off_unit.change(update_offset_label, inputs=off_unit, outputs=off_input)
717
- calc_btn_1.click(calc_angle_from_offset, [dist_input_1, dist_unit_1, off_input, off_type, off_unit], [calc_output_text_1, calc_output_plot_1])
718
- calc_btn_2.click(calc_offset_from_angle, [dist_input_2, dist_unit_2, angle_input, ang_type], [calc_output_text_2, calc_output_plot_2])
719
 
720
- # 光束發散計算
721
- div_calc_btn.click(calculate_and_plot_divergence, [z1_in, d1_in, z2_in, d2_in, size_type_div], [div_out_text, div_out_plot])
722
 
723
- # 焦距收斂計算
724
  focal_unit.change(update_focal_label, inputs=focal_unit, outputs=focal_input)
725
- focus_calc_btn.click(calculate_and_plot_focus_divergence, [focal_input, focal_unit, d1_focus_in, d2_focus_in, size_type_focus], [focus_out_text, focus_out_plot])
726
 
727
- # 線斑角度計算
728
  dist_unit_line.change(update_dist_label, inputs=dist_unit_line, outputs=dist_input_line)
729
  length_unit_line.change(update_line_length_label, inputs=length_unit_line, outputs=length_input_line)
730
- calc_btn_line.click(calculate_and_plot_line_angle, [dist_input_line, dist_unit_line, length_input_line, length_unit_line], [calc_output_text_line, calc_output_plot_line])
731
 
732
 
733
  # 部署在 Hugging Face Spaces 時不需加 share=True
 
89
  return fig
90
 
91
  def calc_angle_from_offset(distance, dist_unit, offset, off_type, off_unit):
92
+ if distance <= 0: return "❌ 距離必須大於 0", None, gr.update()
93
 
94
  # 統一將測試距離轉換為 mm 計算
95
  if dist_unit == "公分 (cm)":
 
114
  f"- 輸入條件: 距離 **{distance} {dist_unit}** ({distance_mm:.2f} mm), 偏移 **{offset} {off_unit}** ({off_type})\n"
115
  f"- 實際半徑偏移 (r): **{r:.4f} mm**\n"
116
  f"- 偏移角度: **{deg:.4f}°**\n"
117
+ f"- 同軸度: **{mrad:.4f} mRad**\n\n"
118
+ f"*(✨ 同軸度參數已自動同步至「產品訂購」表單!)*"
119
  )
120
 
121
  fig = draw_coaxiality_plot(distance_mm, r)
122
+ return text_result, fig, round(mrad, 4)
123
 
124
  def calc_offset_from_angle(distance, dist_unit, angle, ang_type):
125
+ if distance <= 0: return "❌ 距離必須大於 0", None, gr.update()
126
 
127
  # 統一將測試距離轉換為 mm 計算
128
  if dist_unit == "公分 (cm)":
 
152
  f"- 輸入條件: 距離 **{distance} {dist_unit}** ({distance_mm:.2f} mm), 角度 **{angle}** ({ang_type})\n"
153
  f"- **半徑長度** (半徑偏移): **{r:.4f} mm** / **{r_um:.2f} µm**\n"
154
  f"- **直徑長度** (直徑偏移): **{diameter:.4f} mm** / **{diameter_um:.2f} µm**\n"
155
+ f"- 等效 mRad: **{mrad:.4f} mRad**\n\n"
156
+ f"*(✨ 同軸度參數已自動同步至「產品訂購」表單!)*"
157
  )
158
 
159
  fig = draw_coaxiality_plot(distance_mm, r)
160
+ return text_result, fig, round(mrad, 4)
161
 
162
  # --- 光束發散 (Beam Divergence) 邏輯 ---
163
 
164
  def calculate_and_plot_divergence(z1, d1, z2, d2, size_type):
165
+ if z1 >= z2: return "❌ 錯誤:第二個位置 (z2) 必須大於第一個位置 (z1)", None, gr.update()
166
+ if d1 < 0 or d2 < 0: return "❌ 錯誤:光斑尺寸不能為負數", None, gr.update()
167
 
168
  delta_L = z2 - z1
169
 
 
190
  f"- 半徑尺寸差 (${{\\Delta r}}$): **{delta_r:.4f} mm**\n\n"
191
  f"#### 📐 角度結果\n"
192
  f"- **半角發散 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°** / **{half_mrad:.4f} mRad**\n"
193
+ f"- **全角發散 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°** / **{full_mrad:.4f} mRad**\n\n"
194
+ f"*(✨ 光點大小已自動同步至「產品訂購」表單!)*"
195
  )
196
 
197
  # 繪製圖表
 
228
  ax.axis('off')
229
 
230
  plt.tight_layout()
231
+ return text_result, fig, str(d2)
232
 
233
  # --- 光束焦距收斂 (Focus Beam Divergence) 邏輯 ---
234
 
235
  def calculate_and_plot_focus_divergence(focal_length, dist_unit, d1, d2, size_type):
236
+ if focal_length <= 0: return "❌ 錯誤:焦距必須大於 0", None, gr.update(), gr.update()
237
+ if d1 < 0 or d2 < 0: return "❌ 錯誤:光斑尺寸不能為負數", None, gr.update(), gr.update()
238
 
239
  # 單位轉換至 mm
240
  focal_length_mm = focal_length * 10.0 if dist_unit == "公分 (cm)" else (focal_length * 1000.0 if dist_unit == "公尺 (m)" else focal_length)
 
264
  f"- 半徑收斂差 ($r_1 - r_2$): **{delta_r:.4f} mm**\n\n"
265
  f"#### 📐 收斂角度結果\n"
266
  f"- **半收斂角 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°** / **{half_mrad:.4f} mRad**\n"
267
+ f"- **全收斂角 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°** / **{full_mrad:.4f} mRad**\n\n"
268
+ f"*(✨ 焦距與光點大小已自動同步至「產品訂購」表單!)*"
269
  )
270
 
271
  # 繪製圖表
 
302
  ax.axis('off')
303
 
304
  plt.tight_layout()
305
+ return text_result, fig, round(focal_length_mm, 2), str(d2)
306
 
307
 
308
  # --- 線光斑長度與角度 (Line Laser Angle) 邏輯 ---
309
 
310
  def calculate_and_plot_line_angle(distance, dist_unit, line_length, length_unit):
311
+ if distance <= 0: return "❌ 錯誤:距離必須大於 0", None, gr.update(), gr.update()
312
+ if line_length < 0: return "❌ 錯誤:線長不能為負數", None, gr.update(), gr.update()
313
 
314
  # 單位轉換至 mm
315
  dist_mm = distance * 10.0 if dist_unit == "公分 (cm)" else (distance * 1000.0 if dist_unit == "公尺 (m)" else distance)
 
332
  f"- 半線長 ($l/2$): **{half_l:.2f} mm**\n\n"
333
  f"#### 📐 發散角度結果\n"
334
  f"- **半角 (Half-Angle, ${{\\theta/2}}$)**: **{half_deg:.4f}°**\n"
335
+ f"- **發散全角 (Full-Angle, ${{\\theta}}$)**: **{full_deg:.4f}°**\n\n"
336
+ f"*(✨ 光斑樣式與尺寸已自動同步至「產品訂購」表單!)*"
337
  )
338
 
339
  # 繪製圖表
 
363
  ax.axis('off')
364
 
365
  plt.tight_layout()
366
+ # 回傳光斑樣式「線狀」與光點大小「長度 mm」以供連動
367
+ return text_result, fig, "線狀", str(len_mm)
368
 
369
 
370
  # --- 更新輸入框標籤函數 ---
 
716
  add_w_btn.click(lambda n, c: (gr.update(choices=c+[n], value=n), c+[n], f"已新增: {n}"), [new_w_input, ws_state], [w_select, ws_state, order_status])
717
  download_btn.click(get_backup_files, outputs=file_output)
718
 
719
+ # 同軸度計算 (連動同軸度參數至表單)
720
  dist_unit_1.change(update_dist_label, inputs=dist_unit_1, outputs=dist_input_1)
721
  dist_unit_2.change(update_dist_label, inputs=dist_unit_2, outputs=dist_input_2)
722
  off_unit.change(update_offset_label, inputs=off_unit, outputs=off_input)
723
+ calc_btn_1.click(calc_angle_from_offset, [dist_input_1, dist_unit_1, off_input, off_type, off_unit], [calc_output_text_1, calc_output_plot_1, coaxial_in])
724
+ calc_btn_2.click(calc_offset_from_angle, [dist_input_2, dist_unit_2, angle_input, ang_type], [calc_output_text_2, calc_output_plot_2, coaxial_in])
725
 
726
+ # 光束發散計算 (連動光點大小至表單)
727
+ div_calc_btn.click(calculate_and_plot_divergence, [z1_in, d1_in, z2_in, d2_in, size_type_div], [div_out_text, div_out_plot, spot_in])
728
 
729
+ # 焦距收斂計算 (連動焦距與光點大小至表單)
730
  focal_unit.change(update_focal_label, inputs=focal_unit, outputs=focal_input)
731
+ focus_calc_btn.click(calculate_and_plot_focus_divergence, [focal_input, focal_unit, d1_focus_in, d2_focus_in, size_type_focus], [focus_out_text, focus_out_plot, focus_in, spot_in])
732
 
733
+ # 線斑角度計算 (連動光斑樣式與長度尺寸至表單)
734
  dist_unit_line.change(update_dist_label, inputs=dist_unit_line, outputs=dist_input_line)
735
  length_unit_line.change(update_line_length_label, inputs=length_unit_line, outputs=length_input_line)
736
+ calc_btn_line.click(calculate_and_plot_line_angle, [dist_input_line, dist_unit_line, length_input_line, length_unit_line], [calc_output_text_line, calc_output_plot_line, s_radio, spot_in])
737
 
738
 
739
  # 部署在 Hugging Face Spaces 時不需加 share=True