Ipywidgets events
In [ ]:
Copied!
import ipywidgets as widgets
import ipywidgets as widgets
In [ ]:
Copied!
print(widgets.Button.on_click.__doc__)
print(widgets.Button.on_click.__doc__)
In [ ]:
Copied!
button = widgets.Button(description="Click Me!", button_style="primary")
output = widgets.Output()
def on_button_clicked(b):
with output:
print("Button clicked.")
button.on_click(on_button_clicked)
widgets.VBox([button, output])
button = widgets.Button(description="Click Me!", button_style="primary")
output = widgets.Output()
def on_button_clicked(b):
with output:
print("Button clicked.")
button.on_click(on_button_clicked)
widgets.VBox([button, output])
In [ ]:
Copied!
print(widgets.Widget.observe.__doc__)
print(widgets.Widget.observe.__doc__)
First example¶
In [ ]:
Copied!
buttons = widgets.ToggleButtons(
value=None,
options=["Show", "Hide", "Close"],
button_style="primary",
)
buttons.style.button_width = "80px"
html = widgets.HTML(
value='<img src="https://earthengine.google.com/static/images/earth-engine-logo.png" width="100" height="100">'
)
vbox = widgets.VBox([buttons, html])
vbox
buttons = widgets.ToggleButtons(
value=None,
options=["Show", "Hide", "Close"],
button_style="primary",
)
buttons.style.button_width = "80px"
html = widgets.HTML(
value='
'
)
vbox = widgets.VBox([buttons, html])
vbox
'
)
vbox = widgets.VBox([buttons, html])
vboxIn [ ]:
Copied!
def handle_btn_click(change):
if change['new'] == 'Show':
vbox.children = [buttons, html]
elif change['new'] == 'Hide':
vbox.children = [buttons]
elif change['new'] == 'Close':
buttons.close()
html.close()
vbox.close()
buttons.observe(handle_btn_click, "value")
def handle_btn_click(change):
if change['new'] == 'Show':
vbox.children = [buttons, html]
elif change['new'] == 'Hide':
vbox.children = [buttons]
elif change['new'] == 'Close':
buttons.close()
html.close()
vbox.close()
buttons.observe(handle_btn_click, "value")
Second example¶
In [ ]:
Copied!
dropdown = widgets.Dropdown(
options=["Landsat", "Sentinel", "MODIS"],
value=None,
description="Satellite:",
style={"description_width": "initial"},
layout=widgets.Layout(width="250px")
)
btns = widgets.ToggleButtons(
value=None,
options=["Apply", "Reset", "Close"],
button_style="primary",
)
btns.style.button_width = "80px"
output = widgets.Output()
box = widgets.VBox([dropdown, btns, output])
box
dropdown = widgets.Dropdown(
options=["Landsat", "Sentinel", "MODIS"],
value=None,
description="Satellite:",
style={"description_width": "initial"},
layout=widgets.Layout(width="250px")
)
btns = widgets.ToggleButtons(
value=None,
options=["Apply", "Reset", "Close"],
button_style="primary",
)
btns.style.button_width = "80px"
output = widgets.Output()
box = widgets.VBox([dropdown, btns, output])
box
In [ ]:
Copied!
def dropdown_change(change):
if change['new']:
with output:
output.clear_output()
print(change['new'])
dropdown.observe(dropdown_change, "value")
def dropdown_change(change):
if change['new']:
with output:
output.clear_output()
print(change['new'])
dropdown.observe(dropdown_change, "value")
In [ ]:
Copied!
def button_click(change):
with output:
output.clear_output()
if change['new'] == "Apply":
if dropdown.value is None:
print("Please select a satellite from the dropdown list.")
else:
print(f"You selected {dropdown.value}")
elif change['new'] == 'Reset':
dropdown.value = None
else:
box.close()
btns.observe(button_click, "value")
def button_click(change):
with output:
output.clear_output()
if change['new'] == "Apply":
if dropdown.value is None:
print("Please select a satellite from the dropdown list.")
else:
print(f"You selected {dropdown.value}")
elif change['new'] == 'Reset':
dropdown.value = None
else:
box.close()
btns.observe(button_click, "value")
