Skip to content

Commit

Permalink
#6800: add general display method (#6871)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete authored and scottdraves committed Feb 19, 2018
1 parent bf5251c commit 9843340
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public DefaultJVMVariables() {
"com.twosigma.beakerx.table.renderer.TableDisplayCellRenderer",
"com.twosigma.beakerx.table.format.TableDisplayStringFormat",
"com.twosigma.beakerx.table.highlight.TableDisplayCellHighlighter",
"com.twosigma.beakerx.table.highlight.ThreeColorHeatmapHighlighter"
"com.twosigma.beakerx.table.highlight.ThreeColorHeatmapHighlighter",
"static com.twosigma.beakerx.Display.display"

);
}
Expand Down
57 changes: 57 additions & 0 deletions kernel/base/src/main/java/com/twosigma/beakerx/Display.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx;

import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.kernel.comm.Comm;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.mimetype.MIMEContainer;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import static com.twosigma.beakerx.evaluator.InternalVariable.getParentHeader;
import static com.twosigma.beakerx.kernel.comm.Comm.DATA;
import static com.twosigma.beakerx.kernel.msg.JupyterMessages.DISPLAY_DATA;
import static java.util.Collections.singletonList;

public class Display {

public static void display(Object value) {
List<MIMEContainer> result = getMIMEContainers(value);
if (!result.isEmpty()) {
displayMIMEContainers(result);
}
}

private static void displayMIMEContainers(List<MIMEContainer> result) {
HashMap<String, Serializable> content = new HashMap<>();
HashMap<String, Object> data = new HashMap<>();
result.forEach(x -> data.put(x.getMime().asString(), x.getData()));
content.put(DATA, data);
Message message = Comm.messageMessage(DISPLAY_DATA, Comm.Buffer.EMPTY, content, getParentHeader());
KernelManager.get().publish(singletonList(message));
}

private static List<MIMEContainer> getMIMEContainers(Object value) {
List<MIMEContainer> containers = MIMEContainerFactory.createMIMEContainers(value);
return containers.stream()
.filter(x -> !x.getMime().asString().equals(MIMEContainer.MIME.HIDDEN)).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
import static java.util.Collections.singletonList;


public class SerializeToString {
public class MIMEContainerFactory {

private static List<MIMEContainer> HIDDEN_MIME = singletonList(HIDDEN);

public static List<MIMEContainer> doit(final Object input) {
public static List<MIMEContainer> createMIMEContainers(final Object input) {
if (input == null) {
return getMimeContainerForNull();
}
return getMimeContainer(input);
return createMIMEContainersFromObject(input);
}

public static List<MIMEContainer> getMimeContainerForNull() {
Expand All @@ -54,7 +54,7 @@ public static List<MIMEContainer> getMimeContainerForNull() {
return HIDDEN_MIME;
}

private static List<MIMEContainer> getMimeContainer(final Object data) {
private static List<MIMEContainer> createMIMEContainersFromObject(final Object data) {

Object input = DisplayerDataMapper.convert(data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.twosigma.beakerx.jvm.object;

import com.twosigma.beakerx.SerializeToString;
import com.twosigma.beakerx.MIMEContainerFactory;
import com.twosigma.beakerx.mimetype.MIMEContainer;
import com.twosigma.beakerx.widget.Widget;
import com.twosigma.beakerx.widget.HTML;
Expand Down Expand Up @@ -54,7 +54,7 @@ private Optional<Widget> toWidget(Object item) {
if (item == null) {
return handleNull();
}
Widget widget = SerializeToString.getTableDisplay(item);
Widget widget = MIMEContainerFactory.getTableDisplay(item);
if (widget != null) {
return of(widget);
}
Expand All @@ -69,7 +69,7 @@ private Optional<Widget> toWidget(Object item) {
}

private Optional<Widget> handleNull() {
List<MIMEContainer> mimeContainerForNull = SerializeToString.getMimeContainerForNull();
List<MIMEContainer> mimeContainerForNull = MIMEContainerFactory.getMimeContainerForNull();
if (mimeContainerForNull.contains(MIMEContainer.HIDDEN)) {
return empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,15 @@ public Message createMessage(JupyterMessages type, Buffer buffer, Comm.Data data
}

private Message create(JupyterMessages type, Comm.Buffer buffer, Map<String, Serializable> content) {
Message parentMessage = getParentMessage();// can be null
Message parentMessage = getParentMessage();
return messageMessage(type, buffer, content, parentMessage);
}

public static Message messageMessage(JupyterMessages type, Buffer buffer, Map<String, Serializable> content, Message parentMessage) {
Message message = new Message();
message.setHeader(new Header(type, parentMessage != null ? parentMessage.getHeader().getSession() : null));
if (parentMessage != null) {
message.setParentHeader(getParentMessage().getHeader());
message.setParentHeader(parentMessage.getHeader());
}
message.setContent(content);
message.setMetadata(buildMetadata());
Expand Down Expand Up @@ -270,10 +274,9 @@ public void handleMsg(Message parentMessage) {
}
}

private HashMap<String, Serializable> buildMetadata() {
private static HashMap<String, Serializable> buildMetadata() {
HashMap<String, Serializable> metadata = new HashMap<>();
metadata.put(VERSION, "2");

return metadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.twosigma.beakerx.SerializeToString;
import com.twosigma.beakerx.MIMEContainerFactory;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject.EvaluationStatus;
import com.twosigma.beakerx.kernel.SocketEnum;

Expand Down Expand Up @@ -265,7 +265,7 @@ private static String[] clearText(String[] input) {

private static MessageHolder createFinishResult(SimpleEvaluationObject seo, Message message) {
MessageHolder ret = null;
List<MIMEContainer> mimes = SerializeToString.doit(seo.getPayload());
List<MIMEContainer> mimes = MIMEContainerFactory.createMIMEContainers(seo.getPayload());
if (!mimes.contains(MIMEContainer.HIDDEN)) {
ret = new MessageHolder(SocketEnum.IOPUB_SOCKET,
buildMessage(message, mimes, outputdataResult(seo.getOutputdata()), seo.getExecutionCount()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.twosigma.beakerx.widget;

import com.twosigma.beakerx.SerializeToString;
import com.twosigma.beakerx.MIMEContainerFactory;
import com.twosigma.beakerx.evaluator.InternalVariable;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelManager;
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void runCompiledCode(Message message, ExecuteCompiledCode handler,
try {
Object result = handler.executeCode(params);
if (result != null) {
SerializeToString.doit(result);
MIMEContainerFactory.createMIMEContainers(result);
}
} catch (Exception e) {
printError(message, seo, e);
Expand All @@ -80,7 +80,7 @@ public static void runCompiledCodeAndPublish(Message message, ExecuteCompiledCod
try {
Object result = handler.executeCode(params);
if (result != null) {
List<MIMEContainer> resultString = SerializeToString.doit(result);
List<MIMEContainer> resultString = MIMEContainerFactory.createMIMEContainers(result);
KernelManager.get().publish(singletonList(MessageCreator.buildDisplayData(message, resultString)));
}
} catch (Exception e) {
Expand Down
78 changes: 78 additions & 0 deletions kernel/base/src/test/java/com/twosigma/beakerx/DisplayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx;

import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.kernel.msg.JupyterMessages;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.mimetype.MIMEContainer;
import com.twosigma.beakerx.widget.Widget;
import com.twosigma.beakerx.widget.integers.IntSlider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.stream.Collectors;

import static com.twosigma.beakerx.widget.TestWidgetUtils.getData;
import static org.assertj.core.api.Assertions.assertThat;

public class DisplayTest {

private KernelTest kernel;

@Before
public void setUp() throws Exception {
kernel = new KernelTest();
KernelManager.register(kernel);
}

@After
public void tearDown() throws Exception {
KernelManager.register(null);
}

@Test
public void shouldSendMessageForText() {
//given
//when
Display.display("Hello");
//then
verifyText();
}

private void verifyText() {
Message message = kernel.getPublishedMessages().get(0);
assertThat(message.type()).isEqualTo(JupyterMessages.DISPLAY_DATA);
assertThat(getData(message).get(MIMEContainer.MIME.TEXT_PLAIN)).isEqualTo("Hello");
}

@Test
public void shouldSendMessageForWidget() {
//given
//when
Display.display(new IntSlider());
//then
verifyWidget();
}

private void verifyWidget() {
Message message = kernel.getPublishedMessages().stream()
.filter(x -> x.type().equals(JupyterMessages.DISPLAY_DATA))
.collect(Collectors.toList()).get(0);
assertThat(getData(message).get(Widget.APPLICATION_VND_JUPYTER_WIDGET_VIEW_JSON)).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@
import com.twosigma.beakerx.mimetype.MIMEContainer;
import jupyter.Displayer;
import jupyter.Displayers;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.twosigma.beakerx.mimetype.MIMEContainer.MIME.TEXT_HTML;
import static java.util.Arrays.stream;
import static org.assertj.core.api.Assertions.assertThat;

public class SerializeToStringTest {
Expand All @@ -46,7 +42,7 @@ public Map<String, String> display(Integer value) {
}
});
//when
List<MIMEContainer> result = SerializeToString.doit(2);
List<MIMEContainer> result = MIMEContainerFactory.createMIMEContainers(2);
//then
assertThat(result.get(0)).isEqualTo(new MIMEContainer(TEXT_HTML, "<div><h1>" + 2 + "</h1></div>"));
}
Expand All @@ -55,7 +51,7 @@ public Map<String, String> display(Integer value) {
public void OutputCellHIDDENShouldReturnMIMEContainerHidden() throws Exception {
//give
//when
List<MIMEContainer> result = SerializeToString.doit(OutputCell.HIDDEN);
List<MIMEContainer> result = MIMEContainerFactory.createMIMEContainers(OutputCell.HIDDEN);
//then
assertThat(result.get(0)).isEqualTo(MIMEContainer.HIDDEN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.twosigma.beakerx.widget;

import com.twosigma.beakerx.SerializeToString;
import com.twosigma.beakerx.MIMEContainerFactory;
import com.twosigma.beakerx.kernel.comm.Comm;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -38,7 +38,7 @@ public void shouldSend3MessagesForAllClassesWhichImplementInternalWidgetInterfac
//give
Widget internalWidget = clazz.newInstance();
//when
SerializeToString.doit(internalWidget);
MIMEContainerFactory.createMIMEContainers(internalWidget);
//then
assertEquals("Should be 3 messages for " + clazz, groovyKernel.getPublishedMessages().size(), 3);
assertThat(groovyKernel.getPublishedMessages().size()).isEqualTo(3);
Expand Down
58 changes: 58 additions & 0 deletions test/ipynb/groovy/GeneralDisplayMethod.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import com.twosigma.beakerx.widget.integers.IntSlider\n",
"import com.twosigma.beakerx.widget.integers.IntProgress\n",
"\n",
"slider = new IntSlider()\n",
"progress = new IntProgress()\n",
"\n",
"def mapList = [\n",
" [a:1, b:2, c:3],\n",
" [a:4, b:5, c:6],\n",
" [a:7, b:8, c:5]\n",
"]\n",
"table1 = new TableDisplay(mapList)\n",
"OutputCell.HIDDEN"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(\"Hello1\")\n",
"display(slider)\n",
"display(Latex('$\\\\alpha^2 + \\\\eta$'))\n",
"display(progress)\n",
"display(YoutubeVideo(\"gSVvxOchT8Y\"))\n",
"display(table1)\n",
"display(\"Hello3\")\n",
"\"done\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Groovy",
"language": "groovy",
"name": "groovy"
},
"language_info": {
"codemirror_mode": "groovy",
"file_extension": ".groovy",
"mimetype": "",
"name": "Groovy",
"nbconverter_exporter": "",
"version": "2.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 9843340

Please sign in to comment.