带有两个滑块的 Jupyter widget notebook,在更改时发出 HTTP POST 请求
这是 带有交互式 IntSlider 发出 HTTP POST 请求的 Jupyter Widget notebook 的扩展版本,具有两个滑块而不是一个。
jupyter_two_sliders_http_post.py
import ipywidgets as widgets
import httpx
from IPython.display import display
# Define the slider widget
delaySlider = widgets.IntSlider(
value=450, # Initial value
min=0, # Minimum value
max=2000, # Maximum value
step=1, # Step size
description='Delay:'
)
lengthSlider = widgets.IntSlider(
value=20*10, # Initial value
min=0, # Minimum value
max=40*10, # Maximum value
step=1, # Step size
description='Length:'
)
# Define a function to handle slider changes
def on_slider_change(change):
# Define the API URL with the slider value
httpx.post("http://10.1.2.3/api/configure", json={"channels":[{
"channel": 0,
"delay": delaySlider.value,
"length": lengthSlider.value,
}]})
# Attach the slider change handler to the slider widget
delaySlider.observe(on_slider_change, names='value')
lengthSlider.observe(on_slider_change, names='value')
# Display the slider widget in the notebook
display(widgets.Box(children=[delaySlider, lengthSlider]))If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow