Skip to content

Commit

Permalink
#5147: add jvm-repr
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete committed Jul 31, 2017
1 parent 2c6acaf commit 427225a
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 38 deletions.
94 changes: 94 additions & 0 deletions doc/contents/JVM_repr_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"null"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import jupyter.Displayer;\n",
"import jupyter.Displayers;\n",
"Displayers.register(Integer.class, new Displayer<Integer>() {\n",
" @Override\n",
" public Map<String, String> display(Integer value) {\n",
" return new HashMap<String, String>() {{\n",
" put(MIMEContainer.MIME.TEXT_HTML, \"<div><h1>\" + value + \"</h1></div>\");\n",
" \n",
" }};\n",
" }\n",
" });"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><h1>2</h1></div>"
],
"text/plain": [
"<div><h1>2</h1></div>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1+1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import jupyter.Displayer;\n",
"import jupyter.Displayers;\n",
"Displayers.display(1+1);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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
}
2 changes: 1 addition & 1 deletion kernel/base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.5'
compile group: 'com.opencsv', name: 'opencsv', version: '3.10'

compile group: 'com.github.jupyter', name: 'jvm-repr', version: '0.1.0'

testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,57 @@
import com.twosigma.beakerx.mimetype.MIMEContainer;
import com.twosigma.beakerx.table.TableDisplay;
import com.twosigma.beakerx.widgets.DisplayableWidget;
import jupyter.Displayers;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.twosigma.beakerx.mimetype.MIMEContainer.HIDDEN;
import static com.twosigma.beakerx.mimetype.MIMEContainer.Text;
import static java.util.Collections.singletonList;


public class SerializeToString {
private static List<MIMEContainer> HIDDEN_MIME = singletonList(HIDDEN);

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

public static MIMEContainer getMimeContainerForNull() {
public static List<MIMEContainer> getMimeContainerForNull() {
if (Kernel.showNullExecutionResult) {
return Text("null");
return singletonList(Text("null"));
}
return HIDDEN;

return HIDDEN_MIME;
}

private static MIMEContainer getMimeContainer(final Object input) {
private static List<MIMEContainer> getMimeContainer(final Object input) {
if (input instanceof DisplayableWidget) {
((DisplayableWidget) input).display();
return HIDDEN;
return HIDDEN_MIME;
}
TableDisplay table = getTableDisplay(input);
if (table != null) {
table.display();
return HIDDEN;
return HIDDEN_MIME;
}
if (input instanceof XYGraphics) {
new Plot().add((XYGraphics) input).display();
return HIDDEN;
return HIDDEN_MIME;
}
if (input instanceof MIMEContainer) {
return (MIMEContainer) input;
return singletonList((MIMEContainer) input);
}
return Text(input.toString());

return Displayers.display(input).entrySet().stream().
map(item -> new MIMEContainer(item.getKey(), item.getValue())).
collect(Collectors.toList());
}

public static TableDisplay getTableDisplay(final Object input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ private Optional<Widget> toWidget(Object item) {
}

private Optional<Widget> handleNull() {
MIMEContainer mimeContainerForNull = SerializeToString.getMimeContainerForNull();
if (mimeContainerForNull.getMime().asString().equals(MIMEContainer.MIME.HIDDEN)) {
List<MIMEContainer> mimeContainerForNull = SerializeToString.getMimeContainerForNull();
if (mimeContainerForNull.contains(MIMEContainer.HIDDEN)) {
return empty();
}
return of(createHTML(mimeContainerForNull.getData().toString()));
return of(createHTML(mimeContainerForNull.get(0).getData().toString()));
}

private Widget createHTML(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public Kernel(final String sessionId, final Evaluator evaluator,
this.handlers = new KernelHandlers(this, getCommOpenHandler(this), getKernelInfoHandler(this));
configureSignalHandler();
initKernel(getKernelParameters());
configureJvmRepr();
}

public abstract KernelParameters getKernelParameters();
Expand All @@ -74,6 +75,9 @@ public Kernel(final String sessionId, final Evaluator evaluator,

public abstract KernelHandler<Message> getKernelInfoHandler(Kernel kernel);

protected void configureJvmRepr(){
}

@Override
public void run() {
KernelManager.register(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import static com.twosigma.beakerx.mimetype.MIMEContainer.HTML;
import static com.twosigma.beakerx.mimetype.MIMEContainer.JavaScript;
import static com.twosigma.beakerx.mimetype.MIMEContainer.Text;
import static java.util.Collections.singletonList;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.twosigma.beakerx.kernel.Code;
import com.twosigma.beakerx.kernel.CodeWithoutCommand;
import com.twosigma.beakerx.kernel.ImportPath;
Expand Down Expand Up @@ -293,7 +293,7 @@ private MagicCommandFunctionality javascript() {
MIMEContainer result = JavaScript(code.takeCodeWithoutCommand().get().asString());
return new MagicCommandItemWithResult(
messageCreator
.buildMessage(message, result.getMime().asString(), result.getData().toString(), executionCount),
.buildMessage(message, singletonList(result), executionCount),
messageCreator.buildReplyWithoutStatus(message, executionCount)
);
};
Expand All @@ -305,7 +305,7 @@ private MagicCommandFunctionality html() {
"<html>" + code.takeCodeWithoutCommand().get().asString() + "</html>");
return new MagicCommandItemWithResult(
messageCreator
.buildMessage(message, html.getMime().asString(), html.getData().toString(), executionCount),
.buildMessage(message, singletonList(html), executionCount),
messageCreator.buildReplyWithoutStatus(message, executionCount)
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import com.twosigma.beakerx.jvm.object.ConsoleOutput;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
Expand Down Expand Up @@ -73,22 +74,23 @@ private Message initMessage(JupyterMessages type, Message message) {
return reply;
}

public Message buildMessage(Message message, String mime, Object code, int executionCount) {
public Message buildMessage(Message message, List<MIMEContainer> mimes, int executionCount) {
Message reply = initMessage(EXECUTE_RESULT, message);
reply.setContent(new HashMap<String, Serializable>());
reply.setContent(new HashMap<>());
reply.getContent().put("execution_count", executionCount);
HashMap<String, Object> map3 = new HashMap<>();
map3.put(mime, code);
mimes.forEach(mimeItem -> map3.put(mimeItem.getMime().asString(), mimeItem.getData()));
reply.getContent().put("data", map3);
reply.getContent().put("metadata", new HashMap<>());
return reply;
}

public Message buildMessage(Message message, String mime, Object data, String outputdataResult, int executionCount) {
private Message buildMessage(Message message, List<MIMEContainer> mimes, String outputdataResult, int executionCount) {
if (!outputdataResult.isEmpty()) {
return buildMessage(message, mime, data + outputdataResult, executionCount);
List<MIMEContainer> collect = mimes.stream().map(x -> new MIMEContainer(x.getMime().asString(), x.getData() + outputdataResult)).collect(Collectors.toList());
return buildMessage(message, collect, executionCount);
}
return buildMessage(message, mime, data, executionCount);
return buildMessage(message, mimes, executionCount);
}

public Message buildClearOutput(Message message, boolean wait) {
Expand All @@ -99,12 +101,12 @@ public Message buildClearOutput(Message message, boolean wait) {
return reply;
}

public Message buildDisplayData(Message message, MIMEContainer value) {
public Message buildDisplayData(Message message, List<MIMEContainer> mimes) {
Message reply = initMessage(DISPLAY_DATA, message);
reply.setContent(new HashMap<String, Serializable>());
reply.setContent(new HashMap<>());
reply.getContent().put("metadata", new HashMap<>());
HashMap<String, Serializable> map3 = new HashMap<>();
map3.put(value.getMime().asString(), value.getData().toString());
HashMap<String, Object> map3 = new HashMap<>();
mimes.forEach(mimeItem -> map3.put(mimeItem.getMime().asString(), mimeItem.getData()));
reply.getContent().put("data", map3);
return reply;
}
Expand Down Expand Up @@ -252,12 +254,10 @@ private String[] markRed(String[] input) {

private MessageHolder createFinishResult(SimpleEvaluationObject seo, Message message) {
MessageHolder ret = null;
MIMEContainer resultString = SerializeToString.doit(seo.getPayload());
if (!MIMEContainer.HIDDEN.getMime().equals(resultString.getMime())) {
List<MIMEContainer> mimes = SerializeToString.doit(seo.getPayload());
if (!mimes.contains(MIMEContainer.HIDDEN)) {
ret = new MessageHolder(SocketEnum.IOPUB_SOCKET,
buildMessage(message, resultString.getMime().asString(),
resultString.getData(), outputdataResult(seo.getOutputdata()),
seo.getExecutionCount()));
buildMessage(message, mimes, outputdataResult(seo.getOutputdata()), seo.getExecutionCount()));
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.StringWriter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import static com.twosigma.beakerx.handler.KernelHandlerWrapper.wrapBusyIdle;

Expand Down Expand Up @@ -203,8 +204,7 @@ public void handleCompiledCode(Message message, boolean publishResult, ExecuteCo
try {
Object result = handler.executeCode(params);
if(result != null && message != null){
MIMEContainer resultString = SerializeToString.doit(result);
logger.info("code execution result is = " + resultString.getMime());
List<MIMEContainer> resultString = SerializeToString.doit(result);
if(publishResult){
KernelManager.get().publish(mc.buildDisplayData(message, resultString));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,46 @@

import com.twosigma.beakerx.jvm.object.OutputCell;
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 {

@Test
public void integerAsTextHtml() throws Exception {
//given
Displayers.register(Integer.class, new Displayer<Integer>() {
@Override
public Map<String, String> display(Integer value) {
return new HashMap<String, String>() {{
put(TEXT_HTML, "<div><h1>" + value + "</h1></div>");
}};
}
});
//when
List<MIMEContainer> result = SerializeToString.doit(2);
//then
assertThat(result.get(0)).isEqualTo(new MIMEContainer(TEXT_HTML, "<div><h1>" + 2 + "</h1></div>"));
}

@Test
public void OutputCellHIDDENShouldReturnMIMEContainerHidden() throws Exception {
//give
//when
MIMEContainer result = SerializeToString.doit(OutputCell.HIDDEN);
List<MIMEContainer> result = SerializeToString.doit(OutputCell.HIDDEN);
//then
Assertions.assertThat(result).isEqualTo(MIMEContainer.HIDDEN);
assertThat(result.get(0)).isEqualTo(MIMEContainer.HIDDEN);
}
}
6 changes: 6 additions & 0 deletions kernel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}

allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}

subprojects {
apply plugin: 'jacoco'
apply plugin: 'java'
Expand Down
Loading

0 comments on commit 427225a

Please sign in to comment.