Skip to content

Commit

Permalink
refactor: Remove toolz dependency (#3426)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned authored Jun 19, 2024
1 parent 9e2762c commit 76a9ce1
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 148 deletions.
2 changes: 0 additions & 2 deletions altair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@
"concat",
"condition",
"core",
"curry",
"data",
"data_transformers",
"datum",
Expand All @@ -591,7 +590,6 @@
"overload",
"param",
"parse_shorthand",
"pipe",
"renderers",
"repeat",
"sample",
Expand Down
5 changes: 3 additions & 2 deletions altair/_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import IPython
from IPython.core import magic_arguments
import pandas as pd
from toolz import curried

from altair.vegalite import v5 as vegalite_v5

Expand Down Expand Up @@ -41,7 +40,9 @@ def _prepare_data(data, data_transformers):
if data is None or isinstance(data, dict):
return data
elif isinstance(data, pd.DataFrame):
return curried.pipe(data, data_transformers.get())
if func := data_transformers.get():
data = func(data)
return data
elif isinstance(data, str):
return {"url": data}
else:
Expand Down
3 changes: 2 additions & 1 deletion altair/utils/_importers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from importlib.metadata import version as importlib_version
from types import ModuleType

from packaging.version import Version
from importlib.metadata import version as importlib_version


def import_vegafusion() -> ModuleType:
Expand Down
53 changes: 41 additions & 12 deletions altair/utils/_vegafusion_data.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
from toolz import curried
from __future__ import annotations
import uuid
from weakref import WeakValueDictionary

from typing import (
Any,
Optional,
Union,
Dict,
Set,
MutableMapping,
TypedDict,
Final,
TYPE_CHECKING,
overload,
Callable,
)

from altair.utils._importers import import_vegafusion
from altair.utils.core import DataFrameLike
from altair.utils.data import DataType, ToValuesReturnType, MaxRowsError
from altair.utils.data import (
DataType,
ToValuesReturnType,
MaxRowsError,
SupportsGeoInterface,
)
from altair.vegalite.data import default_data_transformer

if TYPE_CHECKING:
import pandas as pd
from vegafusion.runtime import ChartState # type: ignore

# Temporary storage for dataframes that have been extracted
Expand All @@ -36,21 +45,41 @@ class _ToVegaFusionReturnUrlDict(TypedDict):
url: str


@curried.curry
_VegaFusionReturnType = Union[_ToVegaFusionReturnUrlDict, ToValuesReturnType]


@overload
def vegafusion_data_transformer(
data: None = ..., max_rows: int = ...
) -> Callable[..., Any]: ...


@overload
def vegafusion_data_transformer(
data: DataFrameLike, max_rows: int
) -> ToValuesReturnType: ...


@overload
def vegafusion_data_transformer(
data: DataType, max_rows: int = 100000
) -> Union[_ToVegaFusionReturnUrlDict, ToValuesReturnType]:
data: Union[dict, pd.DataFrame, SupportsGeoInterface], max_rows: int
) -> _VegaFusionReturnType: ...


def vegafusion_data_transformer(
data: Optional[DataType] = None, max_rows: int = 100000
) -> Union[Callable[..., Any], _VegaFusionReturnType]:
"""VegaFusion Data Transformer"""
if hasattr(data, "__geo_interface__"):
# Use default transformer for geo interface objects
# # (e.g. a geopandas GeoDataFrame)
return default_data_transformer(data)
elif isinstance(data, DataFrameLike):
if data is None:
return vegafusion_data_transformer
elif isinstance(data, DataFrameLike) and not isinstance(data, SupportsGeoInterface):
table_name = f"table_{uuid.uuid4()}".replace("-", "_")
extracted_inline_tables[table_name] = data
return {"url": VEGAFUSION_PREFIX + table_name}
else:
# Use default transformer if we don't recognize data type
# Use default transformer for geo interface objects
# # (e.g. a geopandas GeoDataFrame)
# Or if we don't recognize data type
return default_data_transformer(data)


Expand Down
Loading

0 comments on commit 76a9ce1

Please sign in to comment.