Skip to content

Commit

Permalink
Add support for layered charts in add_tiles (#34)
Browse files Browse the repository at this point in the history
* Add support for layered charts in add_tiles

* Fix mypy issues which are due to typed Altair release

* Fix test
  • Loading branch information
binste authored Dec 8, 2023
1 parent 49d89d0 commit 66bd631
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
15 changes: 4 additions & 11 deletions altair_tiles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ def add_tiles(
If chart does not have a geoshape mark or a Mercator projection
or no projection.
"""
if not isinstance(chart, alt.Chart):
raise TypeError(
"Only altair.Chart instances are supported. If you want to add"
+ " tiles to a layer chart, use create_tiles_chart to create the tiles"
+ " and then add them as a normal layer to the existing layer chart."
)

tiles = create_tiles_chart(
provider=provider,
zoom=zoom,
Expand All @@ -68,9 +61,9 @@ def add_tiles(
standalone=False,
)

final_chart = tiles + chart
final_chart = tiles + chart # type: ignore # noqa: PGH003
if attribution:
final_chart = add_attribution(
final_chart = add_attribution( # type: ignore[assignment]
chart=final_chart, provider=provider, attribution=attribution
)
return final_chart
Expand All @@ -80,7 +73,7 @@ def create_tiles_chart(
provider: Union[str, TileProvider] = "OpenStreetMap.Mapnik",
zoom: Optional[int] = None,
attribution: Union[str, bool] = True,
standalone: bool = True,
standalone: Union[bool, alt.Projection] = True,
) -> Union[alt.LayerChart, alt.Chart]:
"""Creates an Altair chart with tiles.
Expand Down Expand Up @@ -383,7 +376,7 @@ def _transform_filter_url_x_y_bounds(
y_max: Union[str, int],
expr_url_x: str,
expr_url_y: str,
) -> str:
) -> alt.Chart:
chart = chart.transform_filter(
# Lower bounds
expr_url_x
Expand Down
6 changes: 0 additions & 6 deletions tests/test_altair_tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,6 @@ def test_no_attribution(self):


class TestAddTiles:
def test_raise_if_not_chart(self):
with pytest.raises(
TypeError, match="Only altair.Chart instances are supported"
):
til.add_tiles(alt.LayerChart)

def test_add_tiles(self):
chart = til.add_tiles(alt.Chart().mark_geoshape().project(type="mercator"))

Expand Down

0 comments on commit 66bd631

Please sign in to comment.