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_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 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_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 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_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 datagrid(dataframe) -> None: return DataGrid(dataframe)