From 738d0062f622359490a53951a64cc8e004a3a573 Mon Sep 17 00:00:00 2001 From: jaroslawmalekcodete Date: Mon, 19 Feb 2018 18:25:34 +0100 Subject: [PATCH] #6730: tableDisplay update value (#6816) * #6730: tableDisplay update value * #6730: remove update cell method and add example for updating values by sendModel * #6730: update cell by column name --- doc/groovy/TableAPI.ipynb | 43 +++++++++++++++++++ .../twosigma/beakerx/table/TableDisplay.java | 22 ++++++++-- .../beakerx/table/TableDisplayTest.java | 27 +++++++++++- 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/doc/groovy/TableAPI.ipynb b/doc/groovy/TableAPI.ipynb index 7db1caa39a..51415f4e84 100644 --- a/doc/groovy/TableAPI.ipynb +++ b/doc/groovy/TableAPI.ipynb @@ -458,6 +458,49 @@ "displayHtml = new TableDisplay([[col1: \"This & that\", col2: \"This / that\", col3: \"This > that\"]]);\n", "displayHtml" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Update cell" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def mapListToUpdate = [\n", + " [a:1, b:2, c:3],\n", + " [a:4, b:5, c:6],\n", + " [a:7, b:8, c:9]\n", + "]\n", + "tableToUpdate = new TableDisplay(mapListToUpdate)\n", + "\n", + "tableToUpdate" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tableToUpdate.values[0][0] = 99\n", + "tableToUpdate.sendModel()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tableToUpdate.updateCell(2,\"c\",121)\n", + "tableToUpdate.sendModel()" + ] } ], "metadata": { diff --git a/kernel/base/src/main/java/com/twosigma/beakerx/table/TableDisplay.java b/kernel/base/src/main/java/com/twosigma/beakerx/table/TableDisplay.java index 8ea3097639..a3364fb49c 100644 --- a/kernel/base/src/main/java/com/twosigma/beakerx/table/TableDisplay.java +++ b/kernel/base/src/main/java/com/twosigma/beakerx/table/TableDisplay.java @@ -145,7 +145,7 @@ public TableDisplay(Collection> v, BeakerObjectConverter ser subtype = LIST_OF_MAPS_SUBTYPE; // create columns - if(v.size() > 0) { + if (v.size() > 0) { // Every column gets inspected at least once, so put every column in // a list with null for the initial type ArrayList columnOrder = new ArrayList(); @@ -158,14 +158,14 @@ public TableDisplay(Collection> v, BeakerObjectConverter ser columnsToCheck.add(columnName); typeTracker.put(columnName, null); } - + // Visit each row and track the row's type. If some value is found to // contain a string, the column is marked as string based and is no // longer typechecked List columnsToRemove = new ArrayList(); for (Map row : v) { // Remove any columns requested from prior iteration - for (String columnToRemove: columnsToRemove) { + for (String columnToRemove : columnsToRemove) { columnsToCheck.remove(columnToRemove); } columnsToRemove = new ArrayList(); @@ -189,7 +189,7 @@ public TableDisplay(Collection> v, BeakerObjectConverter ser } } } - + // Put results of type checking into `columns` and `classes` for (String columnName : columnOrder) { String columnType = typeTracker.get(columnName); @@ -758,4 +758,18 @@ public interface Element { String get(int columnIndex, int rowIndex); } + @SuppressWarnings("unchecked") + public void updateCell(int row, String columnName, Object value) { + int index = getColumnIndex(columnName); + List rowList = (List) values.get(row); + rowList.set(index, value); + } + + private int getColumnIndex(String columnName) { + int index = columns.indexOf(columnName); + if (index < 0) { + throw new RuntimeException("There is no given column name: " + columnName); + } + return index; + } } diff --git a/kernel/base/src/test/java/com/twosigma/beakerx/table/TableDisplayTest.java b/kernel/base/src/test/java/com/twosigma/beakerx/table/TableDisplayTest.java index af01f39fac..f18bd9968e 100644 --- a/kernel/base/src/test/java/com/twosigma/beakerx/table/TableDisplayTest.java +++ b/kernel/base/src/test/java/com/twosigma/beakerx/table/TableDisplayTest.java @@ -87,10 +87,12 @@ import static com.twosigma.beakerx.widget.TestWidgetUtils.verifyOpenCommMsgWitoutLayout; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class TableDisplayTest { public static final String COL_1 = "str1"; + public static final String COL_3 = "str3"; protected KernelTest kernel; @@ -848,7 +850,7 @@ public static List> getListOfMapsWithEmptyTypes() { } private static List getStringList() { - return Arrays.asList(COL_1, "str2", "str3"); + return Arrays.asList(COL_1, "str2", COL_3); } private static List getRowData() { @@ -877,4 +879,27 @@ private List getValueAsList(Map model, String field) { return (List) model.get(field); } + @Test + public void shouldUpdateCellByColumnName() throws Exception { + //given + //when + tableDisplay.updateCell(0, COL_3, 121); + //then + int indexOfCol3 = tableDisplay.getColumnNames().indexOf(COL_3); + assertThat(tableDisplay.getValues().get(0).get(indexOfCol3)).isEqualTo(121); + } + + @Test + public void shouldThrowExceptionWhenUpdateCellByNotExistingColumnName() throws Exception { + //given + //when + try { + tableDisplay.updateCell(0, "UnknownColumnName", 121); + fail("Should not update cell for unknown column name"); + } catch (Exception e) { + //then + assertThat(e.getMessage()).contains("UnknownColumnName"); + } + } + }