Skip to content

Commit

Permalink
Fix logic for no geoviews explorer (#1451)
Browse files Browse the repository at this point in the history
Co-authored-by: maximlt <mliquet@anaconda.com>
  • Loading branch information
ahuang11 and maximlt authored Dec 13, 2024
1 parent c2f9d2b commit dc8b578
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
16 changes: 5 additions & 11 deletions hvplot/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
process_derived_datetime_pandas,
_convert_col_names_to_str,
import_datashader,
import_geoviews,
is_mpl_cmap,
)
from .utilities import hvplot_extension
Expand Down Expand Up @@ -647,6 +648,10 @@ def __init__(

self.dynamic = dynamic
self.geo = any([geo, crs, global_extent, projection, project, coastline, features])
# Try importing geoviews if geo-features requested
if self.geo or self.datatype == 'geopandas':
import_geoviews()

self.crs = self._process_crs(data, crs) if self.geo else None
self.output_projection = self.crs
self.project = project
Expand All @@ -656,17 +661,6 @@ def __init__(
self.tiles_opts = tiles_opts or {}
self.sort_date = sort_date

# Import geoviews if geo-features requested
if self.geo or self.datatype == 'geopandas':
try:
import geoviews # noqa
except ImportError:
raise ImportError(
'In order to use geo-related features '
'the geoviews library must be available. '
'It can be installed with:\n conda '
'install geoviews'
)
if self.geo:
if self.kind not in self._geo_types:
param.main.param.warning(
Expand Down
7 changes: 7 additions & 0 deletions hvplot/tests/testui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from textwrap import dedent
from unittest.mock import patch

import numpy as np
import holoviews as hv
Expand Down Expand Up @@ -415,3 +416,9 @@ def test_max_rows_sample():
ui = hvplot.explorer(df, x='x', y='y', by=['#'], kind='scatter')
assert len(ui._data) == MAX_ROWS
assert not ui._data.equals(df.head(MAX_ROWS))


def test_explorer_geo_no_import_error_when_false():
da = ds_air_temperature['air'].isel(time=0)
with patch('hvplot.util.import_geoviews', return_value=None):
assert hvplot.explorer(da, x='lon', y='lat', geo=False)
6 changes: 6 additions & 0 deletions hvplot/tests/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from hvplot.util import (
check_crs,
import_geoviews,
is_list_like,
process_crs,
process_xarray,
Expand Down Expand Up @@ -383,3 +384,8 @@ def test_is_geodataframe_spatialpandas_dask():
def test_is_geodataframe_classic_dataframe():
df = pd.DataFrame({'geometry': [None, None], 'name': ['A', 'B']})
assert not is_geodataframe(df)


@pytest.mark.geo
def test_geoviews_is_available():
assert import_geoviews()
15 changes: 4 additions & 11 deletions hvplot/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .converter import HoloViewsConverter as _hvConverter
from .plotting import hvPlot as _hvPlot
from .util import is_geodataframe, is_xarray, instantiate_crs_str
from .util import is_geodataframe, is_xarray, instantiate_crs_str, import_geoviews

# Defaults
KINDS = {
Expand Down Expand Up @@ -361,17 +361,10 @@ class Geographic(Controls):
_widgets_kwargs = {'geo': {'type': pn.widgets.Toggle}}

def __init__(self, data, **params):
gv_available = False
try:
import geoviews # noqa

gv_available = True
except ImportError:
pass

geo_params = GEO_KEYS + ['geo']
if not gv_available and any(p in params for p in geo_params):
raise ImportError('GeoViews must be installed to enable the geographic options.')
gv_available = None
if any(params.get(p) for p in geo_params):
gv_available = import_geoviews()
super().__init__(data, **params)
if not gv_available:
for p in geo_params:
Expand Down
12 changes: 12 additions & 0 deletions hvplot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ def import_datashader():
return datashader


def import_geoviews():
geoviews = None
try:
import geoviews
except ModuleNotFoundError:
raise ModuleNotFoundError(
'The `geoviews` package must be installed in order to use '
'geographic features. Install it with pip or conda.'
) from None
return geoviews


def relabel(hv_obj, **kwargs):
"""Conditionally relabel a HoloViews object"""
if kwargs:
Expand Down

0 comments on commit dc8b578

Please sign in to comment.