Skip to content

Commit

Permalink
#6730: tableDisplay update value (#6816)
Browse files Browse the repository at this point in the history
* #6730: tableDisplay update value

* #6730: remove update cell method and add example for updating values by sendModel

* #6730: update cell by column name
  • Loading branch information
jaroslawmalekcodete authored and scottdraves committed Feb 19, 2018
1 parent 9843340 commit 738d006
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
43 changes: 43 additions & 0 deletions doc/groovy/TableAPI.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public TableDisplay(Collection<Map<String, Object>> 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<String> columnOrder = new ArrayList<String>();
Expand All @@ -158,14 +158,14 @@ public TableDisplay(Collection<Map<String, Object>> 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<String> columnsToRemove = new ArrayList<String>();
for (Map<String, Object> row : v) {
// Remove any columns requested from prior iteration
for (String columnToRemove: columnsToRemove) {
for (String columnToRemove : columnsToRemove) {
columnsToCheck.remove(columnToRemove);
}
columnsToRemove = new ArrayList<String>();
Expand All @@ -189,7 +189,7 @@ public TableDisplay(Collection<Map<String, Object>> v, BeakerObjectConverter ser
}
}
}

// Put results of type checking into `columns` and `classes`
for (String columnName : columnOrder) {
String columnType = typeTracker.get(columnName);
Expand Down Expand Up @@ -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<Object> rowList = (List<Object>) 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -848,7 +850,7 @@ public static List<Map<String, Object>> getListOfMapsWithEmptyTypes() {
}

private static List<String> getStringList() {
return Arrays.asList(COL_1, "str2", "str3");
return Arrays.asList(COL_1, "str2", COL_3);
}

private static List<?> getRowData() {
Expand Down Expand Up @@ -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");
}
}

}

0 comments on commit 738d006

Please sign in to comment.