def test_dataframe_index_name(dataframe): # Setting a custom index name dataframe.index.name = "custom_index" grid = DataGrid(dataframe) # Making sure no value is set for index_name assert grid._index_name is None # Checking index name matches DataFrame retrieved from grid data = grid.get_visible_data() assert data.index.name == "custom_index"
def test_get_row_index_of_primary_key( invalid_prim_key: bool, datagrid: DataGrid ) -> None: if invalid_prim_key: assert datagrid._get_row_index_of_primary_key("Nay") == [] else: assert datagrid._get_row_index_of_primary_key("Two") == [1]
def test_get_cell_value_by_numerical_index( invalid_coords: bool, data_object: dict ) -> None: if invalid_coords: assert ( DataGrid._get_cell_value_by_numerical_index(data_object, 2, 2) is None ) else: assert ( DataGrid._get_cell_value_by_numerical_index(data_object, 1, 0) == 4 )
def test_default_dataframe_index(dataframe): # Setting a custom index name grid = DataGrid(dataframe) # Making sure index_name is set assert grid._index_name is None # Generate primary key schema object index_key = grid.get_dataframe_index(dataframe) data_obj = grid.generate_data_object(dataframe, "ipydguuid", index_key) # Default and unused keys should not be in schema assert "key" in data_obj["schema"]["primaryKey"]
def test_data_object_generation(dataframe: pd.DataFrame) -> None: data_object = DataGrid.generate_data_object(dataframe, "ipydguuid", "key") expected_output = { "data": [ {"key": "One", "A": 1, "B": 4, "ipydguuid": 0}, {"key": "Two", "A": 2, "B": 5, "ipydguuid": 1}, {"key": "Three", "A": 3, "B": 6, "ipydguuid": 2}, ], "schema": { "fields": [ {"name": "key", "type": "string"}, {"name": "A", "type": "integer"}, {"name": "B", "type": "integer"}, {"name": "ipydguuid", "type": "integer"}, ], "primaryKey": ["key", "ipydguuid"], "pandas_version": "0.20.0", "primaryKeyUuid": "ipydguuid", }, "fields": [ {"key": None}, {"A": None}, {"B": None}, {"ipydguuid": None}, ], } assert data_object == expected_output
def test_user_defined_index_name(dataframe): # Setting a custom index name dataframe.index.name = "unused_index" grid = DataGrid(dataframe, index_name="custom_index") # Making sure index_name is set assert grid._index_name is not None # Checking index name matches DataFrame retrieved from grid index_key = grid.get_dataframe_index(dataframe) data_obj = grid.generate_data_object(dataframe, "ipydguuid", index_key) # Default and unused keys should not be in schema assert "key" not in data_obj["schema"]["primaryKey"] assert "unused_index" not in data_obj["schema"]["primaryKey"] # User defined key should be in primary key schema assert "custom_index" in data_obj["schema"]["primaryKey"]
def test_selections(clear: bool, dataframe: pd.DataFrame) -> None: grid = DataGrid(dataframe, selection_mode="cell", editable=True) grid.select(1, 0, 2, 1) # Select 1A to 2B if clear: grid.clear_selection() assert grid.selected_cells == [] else: assert grid.selected_cells == [ { "c": 0, "r": 1 }, { "c": 1, "r": 1 }, { "c": 0, "r": 2 }, { "c": 1, "r": 2 }, ]
def plot_tabular(df: pd.DataFrame, opts: ComputeOpts) -> DataGrid: renderer = TextRenderer( format=',') if not opts.normalize else TextRenderer(format=',.4f') grid = DataGrid( df, base_row_size=30, selection_mode="cell", editable=False, base_column_size=80, # layout={'height': '200px'} renderers={ c: renderer for c in df.columns if c not in TEMPORAL_GROUP_BY }, ) grid.transform([{"type": "sort", "columnIndex": 0, "desc": False}]) grid.auto_fit_params = {"area": "all", "padding": 40, "numCols": 1} grid.auto_fit_columns = True return grid
def display_grid(data: Union[dict, pd.DataFrame], **opts) -> DataGrid: column_formats: dict = opts.get('column_formats', {}) renderers = { c: TextRenderer(format=column_formats[c]) if c in column_formats else TextRenderer() for c in data.columns } grid = DataGrid( data, base_row_size=30, selection_mode="cell", editable=False, base_column_size=80, # layout={'height': '200px'} renderers=renderers, ) grid.transform([{"type": "sort", "columnIndex": 0, "desc": False}]) grid.auto_fit_params = {"area": "all", "padding": 40, "numCols": 1} grid.auto_fit_columns = True return grid
def test_column_index_to_name(invalid_index: bool, datagrid: DataGrid) -> None: if invalid_index: assert datagrid._column_index_to_name(4) is None else: assert datagrid._column_index_to_name(1) == "B"
def test_column_name_to_index(invalid_index: bool, datagrid: DataGrid) -> None: if invalid_index: assert datagrid._column_name_to_index("Z") is None else: assert datagrid._column_name_to_index("A") == 0
def get_cell_value_by_index(datagrid: DataGrid) -> None: cell = datagrid.get_cell_value_by_index("B", 1) assert cell == [5]
def test_get_cell_value(datagrid: DataGrid) -> None: cell = datagrid.get_cell_value("B", "Three") assert cell == [6]
def test_data_setter(dataframe) -> None: grid = DataGrid(dataframe) new_df = pd.DataFrame(data={"A": [0, 0, 0], "B": [4, 5, 6]}) grid.data = new_df assert grid.data.equals(new_df)
def test_data_getter(dataframe) -> None: grid = DataGrid(dataframe) assert grid.data.equals(dataframe)
def data_object(dataframe) -> None: return DataGrid.generate_data_object(dataframe, "ipydguuid", "key")
def datagrid(dataframe) -> None: return DataGrid(dataframe)
def test_get_cell_value_by_numerical_index(invalid_coords: bool, datagrid: DataGrid) -> None: if invalid_coords: assert datagrid._get_cell_value_by_numerical_index(2, 2) is None else: assert datagrid._get_cell_value_by_numerical_index(1, 0) == 4
def test_get_col_headers(data_object) -> None: assert DataGrid._get_col_headers(data_object) == ["A", "B"]
def test_column_index_to_name(invalid_index: bool, data_object: dict) -> None: if invalid_index: assert DataGrid._column_index_to_name(data_object, 4) is None else: assert DataGrid._column_index_to_name(data_object, 1) == "B"