Skip to content

Refactor transform logic to make it column name based #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/DataGrid.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@
" {\n",
" \"type\": \"filter\",\n",
" \"operator\": \"=\",\n",
" \"columnIndex\": 7,\n",
" \"column\": \"Origin\",\n",
" \"value\": \"Europe\",\n",
" },\n",
" {\"type\": \"sort\", \"columnIndex\": 3, \"desc\": True},\n",
" {\"type\": \"sort\", \"column\": \"Horsepower\", \"desc\": True},\n",
" ]\n",
")"
]
Expand All @@ -181,9 +181,9 @@
"source": [
"datagrid.transform(\n",
" [\n",
" {\"type\": \"filter\", \"operator\": \"=\", \"columnIndex\": 7, \"value\": \"USA\"},\n",
" {\"type\": \"filter\", \"operator\": \"<\", \"columnIndex\": 1, \"value\": 13},\n",
" {\"type\": \"sort\", \"columnIndex\": 1},\n",
" {\"type\": \"filter\", \"operator\": \"=\", \"column\": \"Origin\", \"value\": \"USA\"},\n",
" {\"type\": \"filter\", \"operator\": \"<\", \"column\": \"Horsepower\", \"value\": 130},\n",
" {\"type\": \"sort\", \"column\": \"Acceleration\"},\n",
" ]\n",
")"
]
Expand Down Expand Up @@ -372,7 +372,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
12 changes: 10 additions & 2 deletions examples/Nested Hierarchies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@
"\n",
"nested_grid"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23f007f4-bcce-4ecd-b68c-aa9b96c82f82",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -82,7 +90,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions examples/Pandas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"}\n",
"\n",
"grid = DataGrid(df, base_row_size=30, base_column_size=300, renderers=renderers)\n",
"grid.transform([{\"type\": \"sort\", \"columnIndex\": 2, \"desc\": True}])\n",
"grid.transform([{\"type\": \"sort\", \"column\": \"Dates\", \"desc\": True}])\n",
"grid"
]
},
Expand All @@ -69,7 +69,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -83,7 +83,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
17 changes: 5 additions & 12 deletions examples/Trees_paris.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -130,28 +130,21 @@
"source": [
"datagrid.transform(\n",
" [\n",
" {\"type\": \"sort\", \"columnIndex\": 5},\n",
" {\"type\": \"sort\", \"column\": \"Height_m\"},\n",
" {\n",
" \"type\": \"filter\",\n",
" \"operator\": \"=\",\n",
" \"columnIndex\": 4,\n",
" \"value\": \"Platane\",\n",
" \"column\": \"Kind\",\n",
" \"value\": \"Platanus\",\n",
" },\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -165,7 +158,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
19 changes: 17 additions & 2 deletions ipydatagrid/datagrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import datetime
import decimal
import warnings
from collections.abc import Iterator
from copy import deepcopy
from math import floor
Expand Down Expand Up @@ -825,8 +826,22 @@ def _validate_transforms(self, proposal):
transforms = proposal["value"]
field_len = len(self._data["schema"]["fields"])
for transform in transforms:
if transform["columnIndex"] > field_len:
raise ValueError("Column index is out of bounds.")
if "columnIndex" in transform:
warnings.warn(
"Applying transforms on columnIndex is deprecated, "
"please provide the column name instead",
DeprecationWarning,
stacklevel=4,
)

if "column" not in transform:
transform["column"] = self._data["schema"]["fields"][
transform["columnIndex"]
]["name"]

if transform["columnIndex"] > field_len:
raise ValueError("Column index is out of bounds.")

return transforms

@validate("_data")
Expand Down
Loading
Loading