ipyfilechooser intro
In [1]:
Copied!
# !pip install ipyfilechooser
# !pip install ipyfilechooser
In [2]:
Copied!
import os
import geogee
import ipywidgets as widgets
from ipyleaflet import WidgetControl
from ipyfilechooser import FileChooser
import os
import geogee
import ipywidgets as widgets
from ipyleaflet import WidgetControl
from ipyfilechooser import FileChooser
C:\Users\Sarath_Kin\miniconda3\envs\demo310\lib\site-packages\google\api_core\_python_version_support.py:266: FutureWarning: You are using a Python version (3.10.19) which Google will stop supporting in new releases of google.api_core once it reaches its end of life (2026-10-04). Please upgrade to the latest Python version, or at least Python 3.11, to continue receiving updates for google.api_core past that date. warnings.warn(message, FutureWarning)
Getting started with ipyfilechooser¶
In [3]:
Copied!
data_dir = os.path.abspath('./data')
fc = FileChooser(data_dir)
fc.use_dir_icons = True
fc.filter_pattern = ['*.shp', '*.geojson']
display(fc)
data_dir = os.path.abspath('./data')
fc = FileChooser(data_dir)
fc.use_dir_icons = True
fc.filter_pattern = ['*.shp', '*.geojson']
display(fc)
--------------------------------------------------------------------------- InvalidPathError Traceback (most recent call last) Cell In[3], line 3 1 data_dir = os.path.abspath('./data') ----> 3 fc = FileChooser(data_dir) 4 fc.use_dir_icons = True 5 fc.filter_pattern = ['*.shp', '*.geojson'] File ~\miniconda3\envs\demo310\lib\site-packages\ipyfilechooser\filechooser.py:42, in FileChooser.__init__(self, path, filename, title, select_desc, change_desc, show_hidden, select_default, dir_icon, dir_icon_append, show_only_dirs, filter_pattern, sandbox_path, layout, **kwargs) 39 if not is_valid_filename(filename): 40 raise InvalidFileNameError(filename) ---> 42 self._default_path = normalize_path(path) 43 self._default_filename = filename 44 self._selected_path: Optional[str] = None File ~\miniconda3\envs\demo310\lib\site-packages\ipyfilechooser\utils.py:137, in normalize_path(path) 134 normalized_path = os.path.realpath(path) 136 if not os.path.isdir(normalized_path): --> 137 raise InvalidPathError(path) 139 return normalized_path InvalidPathError: L:\Coding\Github\geogee\docs\notebooks\data does not exist
In [4]:
Copied!
print(fc.selected_path)
print(fc.selected_path)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[4], line 1 ----> 1 print(fc.selected_path) NameError: name 'fc' is not defined
In [5]:
Copied!
print(fc.selected_filename)
print(fc.selected_filename)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[5], line 1 ----> 1 print(fc.selected_filename) NameError: name 'fc' is not defined
In [6]:
Copied!
print(fc.selected)
print(fc.selected)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[6], line 1 ----> 1 print(fc.selected) NameError: name 'fc' is not defined
In [7]:
Copied!
fc.reset()
fc.reset()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[7], line 1 ----> 1 fc.reset() NameError: name 'fc' is not defined
Creating a toolbar button¶
In [8]:
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 [9]:
Copied!
toolbar = widgets.HBox([toolbar_button])
toolbar
toolbar = widgets.HBox([toolbar_button])
toolbar
Out[9]:
Adding toolbar event¶
In [10]:
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 [11]:
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[11]:
Adding toolbar grid¶
In [12]:
Copied!
rows = 2
cols = 2
grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="62px"))
rows = 2
cols = 2
grid = widgets.GridspecLayout(rows, cols, grid_gap="0px", layout=widgets.Layout(width="62px"))
In [13]:
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[13]:
In [14]:
Copied!
toolbar = widgets.VBox([toolbar_button])
toolbar = widgets.VBox([toolbar_button])
In [15]:
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[15]:
Adding toolbar to ipyleaflet¶
In [16]:
Copied!
toolbar_ctrl = WidgetControl(widget=toolbar, position="topright")
toolbar_ctrl = WidgetControl(widget=toolbar, position="topright")
In [17]:
Copied!
m = geogee.Map()
m.add_control(toolbar_ctrl)
m
m = geogee.Map()
m.add_control(toolbar_ctrl)
m
Out[17]:
In [18]:
Copied!
output = widgets.Output()
output_ctrl = WidgetControl(widget=output, position="topright")
output = widgets.Output()
output_ctrl = WidgetControl(widget=output, position="topright")
In [19]:
Copied!
buttons = widgets.ToggleButtons(
value=None,
options=["Apply", "Reset", "Close"],
tooltips=["Apply", "Reset", "Close"],
button_style="primary",
)
buttons.style.button_width = "80px"
buttons = widgets.ToggleButtons(
value=None,
options=["Apply", "Reset", "Close"],
tooltips=["Apply", "Reset", "Close"],
button_style="primary",
)
buttons.style.button_width = "80px"
In [20]:
Copied!
filechooser_widget = widgets.VBox([fc, buttons])
filechooser_widget = widgets.VBox([fc, buttons])
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[20], line 1 ----> 1 filechooser_widget = widgets.VBox([fc, buttons]) NameError: name 'fc' is not defined
In [21]:
Copied!
def button_click(change):
if change["new"] == "Apply" and fc.selected is not None:
if fc.selected.endswith(".shp"):
m.add_shapefile(fc.selected, layer_name="Shapefile")
elif fc.selected.endswith(".geojson"):
m.add_geojson(fc.selected, layer_name="GeoJSON")
elif change["new"] == "Reset":
fc.reset()
elif change["new"] == "Close":
fc.reset()
m.remove_control(output_ctrl)
buttons.observe(button_click, "value")
def button_click(change):
if change["new"] == "Apply" and fc.selected is not None:
if fc.selected.endswith(".shp"):
m.add_shapefile(fc.selected, layer_name="Shapefile")
elif fc.selected.endswith(".geojson"):
m.add_geojson(fc.selected, layer_name="GeoJSON")
elif change["new"] == "Reset":
fc.reset()
elif change["new"] == "Close":
fc.reset()
m.remove_control(output_ctrl)
buttons.observe(button_click, "value")
In [22]:
Copied!
def tool_click(b):
with output:
output.clear_output()
if b.icon == "folder-open":
display(filechooser_widget)
m.add_control(output_ctrl)
def tool_click(b):
with output:
output.clear_output()
if b.icon == "folder-open":
display(filechooser_widget)
m.add_control(output_ctrl)
In [23]:
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)

In [ ]:
Copied!