Toolbar
In [1]:
import geodemo
import ipywidgets as widgets
from ipyleaflet import WidgetControl
Creating a toolbar button¶
In [2]:
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]:
toolbar = widgets.HBox([toolbar_button])
toolbar
Adding toolbar event¶
In [4]:
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]:
def close_click(change):
if change["new"]:
toolbar_button.close()
close_button.close()
toolbar.close()
close_button.observe(close_click, "value")
toolbar
Adding toolbar grid¶
In [6]:
rows = 2
cols = 2
grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="62px"))
In [7]:
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
In [8]:
toolbar = widgets.VBox([toolbar_button])
In [9]:
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
Adding toolbar to ipyleaflet¶
In [10]:
toolbar_ctrl = WidgetControl(widget=toolbar, position="topright")
In [11]:
m = geodemo.Map()
m.add_control(toolbar_ctrl)
m
In [12]:
output = widgets.Output()
output_ctrl = WidgetControl(widget=output, position="bottomright")
m.add_control(output_ctrl)
In [13]:
def tool_click(b):
with output:
output.clear_output()
print(f"You clicked the {b.icon} button")
In [14]:
for i in range(rows):
for j in range(cols):
tool = grid[i, j]
tool.on_click(tool_click)
Last update: 2021-05-07