Skip to content

Commit

Permalink
Merge branch 'main' into update-ruff-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned authored Jun 19, 2024
2 parents 2122dd2 + 76a9ce1 commit 580c9f6
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 148 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
groups:
github-actions:
patterns:
- "*"
commit-message:
prefix: "ci"
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
jsonschema-version: ["3.0", "latest"]
name: py ${{ matrix.python-version }} js ${{ matrix.jsonschema-version }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
name: ruff-mypy
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
Expand Down
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
5 changes: 3 additions & 2 deletions altair/utils/_importers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations
from packaging.version import Version

from importlib.metadata import version as importlib_version
from typing import TYPE_CHECKING

from packaging.version import Version

if TYPE_CHECKING:
from types import ModuleType


def import_vegafusion() -> ModuleType:
min_version = "1.5.0"
try:
Expand Down
54 changes: 42 additions & 12 deletions altair/utils/_vegafusion_data.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
from __future__ import annotations
from toolz import curried
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 +46,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
) -> _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 580c9f6

Please sign in to comment.