MichaelRKessler commited on
Commit
b48633f
·
1 Parent(s): 0663aa4

Fix opacity sliders to use client-side Plotly.restyle preserving camera and scroll

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -696,7 +696,7 @@ def build_demo() -> gr.Blocks:
696
  value=1.0,
697
  step=0.05,
698
  )
699
- toolpath_plot = gr.Plot(label="Tool Path")
700
  toolpath_status = gr.Markdown("")
701
  parsed_state = gr.State({})
702
 
@@ -712,16 +712,36 @@ def build_demo() -> gr.Blocks:
712
  outputs=[toolpath_plot, toolpath_status, parsed_state],
713
  )
714
  travel_opacity_slider.release(
715
- fn=update_toolpath_opacity,
716
- inputs=[parsed_state, travel_opacity_slider, print_opacity_slider],
717
- outputs=[toolpath_plot],
718
- queue=False,
 
 
 
 
 
 
 
 
 
 
719
  )
720
  print_opacity_slider.release(
721
- fn=update_toolpath_opacity,
722
- inputs=[parsed_state, travel_opacity_slider, print_opacity_slider],
723
- outputs=[toolpath_plot],
724
- queue=False,
 
 
 
 
 
 
 
 
 
 
725
  )
726
 
727
  return demo
 
696
  value=1.0,
697
  step=0.05,
698
  )
699
+ toolpath_plot = gr.Plot(label="Tool Path", elem_id="toolpath_plot")
700
  toolpath_status = gr.Markdown("")
701
  parsed_state = gr.State({})
702
 
 
712
  outputs=[toolpath_plot, toolpath_status, parsed_state],
713
  )
714
  travel_opacity_slider.release(
715
+ fn=None,
716
+ inputs=[travel_opacity_slider],
717
+ outputs=[],
718
+ js="""(opacity_val) => {
719
+ const container = document.getElementById("toolpath_plot");
720
+ if (!container) return [];
721
+ const plotDiv = container.querySelector(".js-plotly-plot");
722
+ if (!plotDiv || !plotDiv.data) return [];
723
+ const indices = plotDiv.data
724
+ .map((t, i) => t.name === "Travel (G0)" ? i : -1)
725
+ .filter(i => i >= 0);
726
+ if (indices.length > 0) Plotly.restyle(plotDiv, {opacity: opacity_val}, indices);
727
+ return [];
728
+ }"""
729
  )
730
  print_opacity_slider.release(
731
+ fn=None,
732
+ inputs=[print_opacity_slider],
733
+ outputs=[],
734
+ js="""(opacity_val) => {
735
+ const container = document.getElementById("toolpath_plot");
736
+ if (!container) return [];
737
+ const plotDiv = container.querySelector(".js-plotly-plot");
738
+ if (!plotDiv || !plotDiv.data) return [];
739
+ const indices = plotDiv.data
740
+ .map((t, i) => t.name === "Print (G1)" ? i : -1)
741
+ .filter(i => i >= 0);
742
+ if (indices.length > 0) Plotly.restyle(plotDiv, {opacity: opacity_val}, indices);
743
+ return [];
744
+ }"""
745
  )
746
 
747
  return demo