Toolbar
In [1]:
Copied!
import geosdemo
import ipywidgets as widgets
from ipyleaflet import WidgetControl
import geosdemo
import ipywidgets as widgets
from ipyleaflet import WidgetControl
Creating a toolbar button¶
In [2]:
Copied!
widget_width = "250px"
padding = "0px 0px 0px 5px" # upper, right, bottom, left
toolbar_button = widgets.ToggleButton(
value=False,
tooltip="Toolbar",
icon="wrench",
layout=widgets.Layout(width="28px", height="28px", padding=padding),
)
close_button = widgets.ToggleButton(
value=False,
tooltip="Close the tool",
icon="times",
button_style="primary",
layout=widgets.Layout(height="28px", width="28px", padding=padding),
)
widget_width = "250px"
padding = "0px 0px 0px 5px" # upper, right, bottom, left
toolbar_button = widgets.ToggleButton(
value=False,
tooltip="Toolbar",
icon="wrench",
layout=widgets.Layout(width="28px", height="28px", padding=padding),
)
close_button = widgets.ToggleButton(
value=False,
tooltip="Close the tool",
icon="times",
button_style="primary",
layout=widgets.Layout(height="28px", width="28px", padding=padding),
)
In [3]:
Copied!
toolbar = widgets.HBox([toolbar_button])
toolbar
toolbar = widgets.HBox([toolbar_button])
toolbar
Out[3]:
Adding toolbar event¶
In [4]:
Copied!
def toolbar_click(change):
if change["new"]:
toolbar.children = [toolbar_button, close_button]
else:
toolbar.children = [toolbar_button]
toolbar_button.observe(toolbar_click, "value")
def toolbar_click(change):
if change["new"]:
toolbar.children = [toolbar_button, close_button]
else:
toolbar.children = [toolbar_button]
toolbar_button.observe(toolbar_click, "value")
In [5]:
Copied!
def close_click(change):
if change["new"]:
toolbar_button.close()
close_button.close()
toolbar.close()
close_button.observe(close_click, "value")
toolbar
def close_click(change):
if change["new"]:
toolbar_button.close()
close_button.close()
toolbar.close()
close_button.observe(close_click, "value")
toolbar
Out[5]:
Adding toolbar grid¶
In [6]:
Copied!
rows = 2
cols = 2
grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="65px"))
rows = 2
cols = 2
grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="65px"))
In [7]:
Copied!
icons = ["folder-open", "map", "info", "question"]
for i in range(rows):
for j in range(cols):
grid[i, j] = widgets.Button(description="", button_style="primary", icon=icons[i*rows+j],
layout=widgets.Layout(width="28px", padding="0px"))
grid
icons = ["folder-open", "map", "info", "question"]
for i in range(rows):
for j in range(cols):
grid[i, j] = widgets.Button(description="", button_style="primary", icon=icons[i*rows+j],
layout=widgets.Layout(width="28px", padding="0px"))
grid
Out[7]:
In [8]:
Copied!
toolbar = widgets.VBox([toolbar_button])
toolbar = widgets.VBox([toolbar_button])
In [9]:
Copied!
def toolbar_click(change):
if change["new"]:
toolbar.children = [widgets.HBox([close_button, toolbar_button]), grid]
else:
toolbar.children = [toolbar_button]
toolbar_button.observe(toolbar_click, "value")
toolbar
def toolbar_click(change):
if change["new"]:
toolbar.children = [widgets.HBox([close_button, toolbar_button]), grid]
else:
toolbar.children = [toolbar_button]
toolbar_button.observe(toolbar_click, "value")
toolbar
Out[9]:
Adding toolbar to ipyleaflet¶
In [10]:
Copied!
toolbar_ctrl = WidgetControl(widget=toolbar, position="topright")
toolbar_ctrl = WidgetControl(widget=toolbar, position="topright")
In [11]:
Copied!
m = geosdemo.Map()
m.add_control(toolbar_ctrl)
m
m = geosdemo.Map()
m.add_control(toolbar_ctrl)
m
Out[11]:
In [12]:
Copied!
output = widgets.Output()
output_ctrl = WidgetControl(widget=output, position="bottomright")
m.add_control(output_ctrl)
output = widgets.Output()
output_ctrl = WidgetControl(widget=output, position="bottomright")
m.add_control(output_ctrl)
In [13]:
Copied!
def tool_click(b):
with output:
output.clear_output()
print(f"You clicked the {b.icon} button")
def tool_click(b):
with output:
output.clear_output()
print(f"You clicked the {b.icon} button")
In [14]:
Copied!
for i in range(rows):
for j in range(cols):
tool = grid[i, j]
tool.on_click(tool_click)
for i in range(rows):
for j in range(cols):
tool = grid[i, j]
tool.on_click(tool_click)
Last update:
2023-05-10