Skip to content

Commit

Permalink
Merge pull request #1753 from sufyanAbbasi/dark-mode
Browse files Browse the repository at this point in the history
Support adaptive theming (and dark mode) of map widgets in Colab
  • Loading branch information
naschmitz authored Oct 3, 2023
2 parents 20685d7 + 747c000 commit ad94fb7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
66 changes: 66 additions & 0 deletions geemap/map_widgets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
"""Various ipywidgets that can be added to a map."""

import functools

import IPython
from IPython.core.display import HTML, display

import ee
import ipytree
import ipywidgets

from . import common


def _set_css_in_cell_output():
display(
HTML(
"""
<style>
.geemap-dark {
--jp-widgets-color: white;
--jp-widgets-label-color: white;
--jp-ui-font-color1: white;
--jp-layout-color2: #454545;
background-color: #383838;
}
.geemap-dark .jupyter-button {
--jp-layout-color3: #383838;
}
.geemap-colab {
background-color: var(--colab-primary-surface-color, white);
}
.geemap-colab .jupyter-button {
--jp-layout-color3: var(--colab-primary-surface-color, white);
}
</style>
"""
)
)


try:
IPython.get_ipython().events.register("pre_run_cell", _set_css_in_cell_output)
except AttributeError:
pass


class Theme:
"""Applies dynamic theme in Colab, otherwise light."""
current_theme = "colab" if common.in_colab_shell() else "light"

@staticmethod
def apply(cls):
original_init = cls.__init__

@functools.wraps(cls.__init__)
def wrapper(self, *args, **kwargs):
original_init(self, *args, **kwargs)
self.add_class("geemap-{}".format(Theme.current_theme))

cls.__init__ = wrapper
return cls


@Theme.apply
class Colorbar(ipywidgets.Output):
"""A matplotlib colorbar widget that can be added to the map."""

Expand Down Expand Up @@ -148,6 +207,7 @@ def _get_dimensions(self, orientation, kwargs):
)


@Theme.apply
class Legend(ipywidgets.VBox):
"""A legend widget that can be added to the map."""

Expand Down Expand Up @@ -359,6 +419,7 @@ def __create_layout_property(name, default_value, **kwargs):
return default_value if name not in kwargs else kwargs[name]


@Theme.apply
class Inspector(ipywidgets.VBox):
"""Inspector widget for Earth Engine data."""

Expand Down Expand Up @@ -621,6 +682,7 @@ def _objects_info(self, latlon):
return self._root_node("Objects", nodes)


@Theme.apply
class LayerManager(ipywidgets.VBox):
def __init__(self, host_map):
"""Initializes a layer manager widget.
Expand Down Expand Up @@ -801,6 +863,7 @@ def _on_layer_visibility_changed(self, change, layer):
self._host_map.remove_control(attachment)


@Theme.apply
class Basemap(ipywidgets.HBox):
"""Widget for selecting a basemap."""

Expand Down Expand Up @@ -840,6 +903,7 @@ def _on_close_click(self, _):
self.on_close()


@Theme.apply
class LayerEditor(ipywidgets.VBox):
"""Widget for displaying and editing layer visualization properties."""

Expand Down Expand Up @@ -937,6 +1001,7 @@ def _on_close_click(self, _):
self.on_close()


@Theme.apply
class _RasterLayerEditor(ipywidgets.VBox):
"""Widget for displaying and editing layer visualization properties for raster layers."""

Expand Down Expand Up @@ -1490,6 +1555,7 @@ def _radio2_observer(self, _):
self._colorbar_output.clear_output()


@Theme.apply
class _VectorLayerEditor(ipywidgets.VBox):
"""Widget for displaying and editing layer visualization properties."""

Expand Down
2 changes: 1 addition & 1 deletion geemap/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
from .common import *
from .timelapse import *

# from .geemap import MapDrawControl
from . import map_widgets


@map_widgets.Theme.apply
class Toolbar(widgets.VBox):
"""A toolbar that can be added to the map."""

Expand Down

0 comments on commit ad94fb7

Please sign in to comment.