Skip to content

Commit

Permalink
Merge pull request #119 from maxfordham/order-fix
Browse files Browse the repository at this point in the history
Order fix
  • Loading branch information
jgunstone authored Feb 15, 2023
2 parents 6be24cc + 88e384d commit 6af4927
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
23 changes: 15 additions & 8 deletions src/ipyautoui/custom/autogrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.0
# jupytext_version: 1.14.4
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down Expand Up @@ -72,7 +72,8 @@ def get_default_row_data_from_schema_properties(


def get_column_widths_from_schema(schema, column_properties, map_name_index, **kwargs):
"""Set the column widths of the data grid based on column_width given in the schema."""
"""Set the column widths of the data grid based on column_width given in the schema.
"""

# start with settings in properties
column_widths = {
Expand Down Expand Up @@ -384,7 +385,10 @@ def coerce_data(

if not isinstance(data.index, pd.RangeIndex):
# raise ValueError("Data must have a RangeIndex")
logging.warning("Data must have a RangeIndex")
logging.warning(
"ipyautoui.custom.autogrid.AutoGrid (and EditGrid) data must have a"
" RangeIndex"
)

def is_bykeys(col_names):
if set(col_names) <= set(self.map_name_index.keys()):
Expand All @@ -393,7 +397,8 @@ def is_bykeys(col_names):
return False
else:
raise ValueError(
"Columns must be a subset of the schema property keys or outward facing index names"
"Columns must be a subset of the schema property keys or outward"
" facing index names"
)

def filter_input_data(data, order, bykeys):
Expand Down Expand Up @@ -429,7 +434,7 @@ def filter_input_data(data, order, bykeys):
)

# ensure columns are in correct order
data.columns = self.get_index(order)
data = data.loc[:, self.get_index(order)]
data.index = pd.RangeIndex(len(data))

# transpose if necessary
Expand Down Expand Up @@ -600,13 +605,14 @@ def _valid_schema(self, proposal):
def _observe_order(self, change):
if not set(self.order) <= set(self.gridschema.properties.keys()):
raise ValueError(
"set(self.order) <= set(self.gridschema.properties.keys()) must be true."
" (i.e. on valid scheam properties allowed)"
"set(self.order) <= set(self.gridschema.properties.keys()) must be"
" true. (i.e. on valid scheam properties allowed)"
)
if self.transposed:
data = self.data.T
else:
data = self.data
data.index = pd.RangeIndex(0, len(data))
self.data = self._init_data(data)

@tr.observe("transposed")
Expand Down Expand Up @@ -1029,7 +1035,8 @@ def selected_col_indexes(self):

@property
def selected_dict(self):
"""Return the dictionary of selected rows where index is row index. still works if transform applied."""
"""Return the dictionary of selected rows where index is row index. still works if transform applied.
"""
if self.transposed:
return self.data.T.loc[self.selected_col_indexes].to_dict("index")
else:
Expand Down
10 changes: 1 addition & 9 deletions src/ipyautoui/custom/editgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.0
# jupytext_version: 1.14.4
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
Expand Down Expand Up @@ -39,16 +39,8 @@

MAP_TRANSPOSED_SELECTION_MODE = frozenmap({True: "column", False: "row"})
# TODO: rename "add" to "fn_add" so not ambiguous...

# +
import warnings

warnings.filterwarnings("ignore")
# REVIEW: Using for now as "No such comm" warning and "UserWarning: Index name of 'index' is not round-trippable" keep popping up.
# Should be resolved at a later date but will ignore for now as not crucial to fix
# -


class DataHandler(BaseModel):
"""CRUD operations for a for EditGrid.
Can be used to connect to a database or other data source.
Expand Down
28 changes: 18 additions & 10 deletions tests/test_autogrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ class TestGridSchema(BaseModel):

assert gr._data["data"] == [
{
("index", ""): 0,
"index": 0,
("a", "String"): "test2",
("b", "Floater"): 2.2,
("b", "Inty"): 1,
("ipydguuid", ""): 0,
"ipydguuid": 0,
}
]

Expand All @@ -344,15 +344,15 @@ class TestGridSchema(BaseModel):
("a", "String"): "test2",
("b", "Floater"): 2.2,
("b", "Inty"): 1,
("ipydguuid", ""): 0,
("index", ""): 0,
"ipydguuid": 0,
"index": 0,
},
{
("a", "String"): "test2",
("b", "Floater"): 2.2,
("b", "Inty"): 1,
("ipydguuid", ""): 1,
("index", ""): 1,
"ipydguuid": 1,
"index": 1,
},
]

Expand Down Expand Up @@ -382,26 +382,30 @@ class DataFrameSchema(BaseModel):
schema=DataFrameSchema, transposed=transposed, order=order
)
# Test with data passed
data = pd.DataFrame([Cols(string="test", floater=2.5).dict()])
grid_with_data = AutoGrid(
schema=DataFrameSchema,
data=pd.DataFrame([Cols(string="test", floater=2.5).dict()]),
data=data,
transposed=transposed,
order=order,
)
di_grid_with_data_before_order = grid_with_data.data.to_dict()
grid_with_data.order = order
if transposed:
assert tuple(grid_without_data.data.index) == tuple(
[grid_without_data.map_name_index.get(name) for name in order]
)
assert tuple(grid_with_data.data.index) == tuple(
[grid_with_data.map_name_index.get(name) for name in order]
)
assert di_grid_with_data_before_order == grid_with_data.data.to_dict()
else:
assert tuple(grid_without_data.data.columns) == tuple(
[grid_without_data.map_name_index.get(name) for name in order]
)
assert tuple(grid_with_data.data.columns) == tuple(
[grid_with_data.map_name_index.get(name) for name in order]
)
assert di_grid_with_data_before_order == grid_with_data.data.to_dict()

@pytest.mark.parametrize("transposed", [True, False])
def test_order_multi_index(self, transposed: bool):
Expand All @@ -426,21 +430,25 @@ class DataFrameSchema(BaseModel):
# Test without data passed
grid_without_data = AutoGrid(schema=DataFrameSchema, order=order)
# Test with data passed
data = pd.DataFrame([Cols(string="test", floater=2.5).dict()])
grid_with_data = AutoGrid(
schema=DataFrameSchema,
data=pd.DataFrame([Cols(string="test", floater=2.5).dict()]),
data=data,
transposed=transposed,
order=order,
)
di_grid_with_data_before_order = grid_with_data.data.to_dict()
grid_with_data.order = order
if transposed is True:
assert tuple(grid_without_data.data.index) == ()
assert tuple(grid_with_data.data.index) == tuple(
[grid_with_data.map_name_index.get(name) for name in order]
)
assert di_grid_with_data_before_order == grid_with_data.data.to_dict()
else:
assert tuple(grid_without_data.data.columns) == tuple(
[grid_without_data.map_name_index.get(name) for name in order]
)
assert tuple(grid_with_data.data.columns) == tuple(
[grid_with_data.map_name_index.get(name) for name in order]
)
assert di_grid_with_data_before_order == grid_with_data.data.to_dict()

0 comments on commit 6af4927

Please sign in to comment.