Skip to content

Commit

Permalink
jarek/7057: %classpath add results as HTML widget (#7212)
Browse files Browse the repository at this point in the history
* #7057: %classpath add results as HTML widget

* #7057: add bxhtml
  • Loading branch information
jaroslawmalekcodete authored and scottdraves committed Apr 24, 2018
1 parent 7408e60 commit dc76805
Show file tree
Hide file tree
Showing 20 changed files with 363 additions and 130 deletions.
44 changes: 44 additions & 0 deletions js/notebook/src/BxHTML.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017 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.
*/

const widgets = require('./widgets');

class BxHTMLModel extends widgets.HTMLModel {
defaults() {
return {
...super.defaults(),
_view_name: "BxHTMLView",
_model_name: "BxHTMLModel",
_model_module: 'beakerx',
_view_module: 'beakerx',
_model_module_version: BEAKERX_MODULE_VERSION,
_view_module_version: BEAKERX_MODULE_VERSION
};
}
}

class BxHTMLView extends widgets.HTMLView {
render() {
super.render();
this.content.style.lineHeight = "20px";
this.content.style.fontSize = "14px";
}
}

export default {
BxHTMLModel,
BxHTMLView
};
3 changes: 2 additions & 1 deletion js/notebook/src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var loadedModules = [
require("./TabView"),
require("./GridView"),
require("./CyclingDisplayBox"),
require("./HTMLPre").default
require("./HTMLPre").default,
require("./BxHTML").default
];

for (var i in loadedModules) {
Expand Down
3 changes: 2 additions & 1 deletion js/notebook/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ var loadedModules = [
require("./TabView"),
require("./GridView"),
require("./CyclingDisplayBox"),
require("./HTMLPre").default
require("./HTMLPre").default,
require("./BxHTML").default
];

for (var i in loadedModules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package com.twosigma.beakerx.kernel;

import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.kernel.msg.MessageCreator;
import com.twosigma.beakerx.message.Message;

import static com.twosigma.beakerx.util.Preconditions.checkNotNull;
import static com.twosigma.beakerx.kernel.handler.MagicCommandExecutor.sendRepliesWithStatus;
import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
import static org.apache.commons.lang3.builder.HashCodeBuilder.reflectionHashCode;
import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;
Expand Down Expand Up @@ -81,7 +81,10 @@ public Message getMessage() {

public void execute(KernelFunctionality kernel, int executionCount) {
if (hasErrors()) {
sendRepliesWithStatus(getErrors(), kernel, getMessage(), executionCount);
errors.forEach(item -> {
item.sendMagicCommandOutcome(kernel, message, executionCount);
kernel.send(MessageCreator.buildReplyWithErrorStatus(message, executionCount));
});
} else {
takeCodeFramesWithoutLast().forEach(frame -> frame.executeFrame(this, kernel, message, executionCount));
takeLastCodeFrame().executeLastFrame(this, kernel, message, executionCount);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

import java.util.Optional;

import static com.twosigma.beakerx.kernel.handler.MagicCommandExecutor.sendMagicCommandOutcome;
import static com.twosigma.beakerx.kernel.handler.MagicCommandExecutor.sendRepliesWithStatus;
import static java.util.Collections.singletonList;

public class MagicCommand extends CodeFrame {

private MagicCommandFunctionality magicCommandFunctionality;
Expand Down Expand Up @@ -62,7 +58,7 @@ public String getCommandCodeBlock() {
@Override
public void executeFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
MagicCommandOutcomeItem execute = execute(code, executionCount, false);
sendMagicCommandOutcome(execute, kernel, message, executionCount);
execute.sendMagicCommandOutcome(kernel, message, executionCount);
TryResult result = execute.getResult();
SimpleEvaluationObject seo = execute.getSimpleEvaluationObject();
handleResult(seo, result);
Expand All @@ -71,7 +67,7 @@ public void executeFrame(Code code, KernelFunctionality kernel, Message message,
@Override
public void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
MagicCommandOutcomeItem execute = execute(code, executionCount, true);
sendRepliesWithStatus(singletonList(execute), kernel, message, executionCount);
execute.sendRepliesWithStatus(kernel, message, executionCount);
TryResult result = execute.getResult();
SimpleEvaluationObject seo = execute.getSimpleEvaluationObject();
handleResult(seo, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.twosigma.beakerx.kernel.magic.command.MagicCommandFunctionality;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput;
import com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutputHTML;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -56,14 +57,14 @@ public Collection<String> addJars(String path) {
}
}

public MagicCommandOutput handleAddedJars(String path) {
public MagicCommandOutcomeItem handleAddedJars(String path) {
Collection<String> newAddedJars = addJars(path);
if (newAddedJars.isEmpty()) {
return new MagicCommandOutput(MagicCommandOutput.Status.OK);
}
String textMessage = "Added jar" + (newAddedJars.size() > 1 ? "s: " : ": ") + newAddedJars;
MagicCommandOutput.Status status = MagicCommandOutcomeItem.Status.OK;
return new MagicCommandOutput(status, textMessage);
return new MagicCommandOutputHTML(status, textMessage);
}

private Collection<String> handlePath(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
package com.twosigma.beakerx.kernel.magic.command.functionality;

import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.widget.HTML;
import com.twosigma.beakerx.widget.StringWidget;
import com.twosigma.beakerx.widget.BxHTML;

import java.math.BigDecimal;
import java.util.Timer;
Expand All @@ -30,15 +29,15 @@
public class MvnLoggerWidget {

static final int PERIOD = 250;
private StringWidget widget;
private BxHTML widget;
private Timer timer;
private volatile int jarNumbers = 0;
private volatile double sizeInKb;
private volatile String speed;
private volatile String currentLine;

public MvnLoggerWidget(Message parentMessage) {
this.widget = new HTML(parentMessage);
this.widget = new BxHTML(parentMessage);
this.timer = new Timer();
this.timer.scheduleAtFixedRate(new TimerTask() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.mimetype.MIMEContainer;

import java.util.Optional;
Expand All @@ -27,20 +29,16 @@ public interface MagicCommandOutcomeItem {

Status getStatus();

Outcome getOutcome();

TryResult getResult();

SimpleEvaluationObject getSimpleEvaluationObject();

void sendRepliesWithStatus(KernelFunctionality kernel, Message message, int executionCount);

void sendMagicCommandOutcome(KernelFunctionality kernel, Message message, int executionCount);

enum Status {
OK,
ERROR
}

enum Outcome {
RESULT,
OUTPUT
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.kernel.msg.MessageCreator;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.mimetype.MIMEContainer;

import java.util.Collections;
import java.util.Optional;

import static com.twosigma.beakerx.util.Preconditions.checkNotNull;
Expand Down Expand Up @@ -70,8 +74,23 @@ public SimpleEvaluationObject getSimpleEvaluationObject() {
}

@Override
public Outcome getOutcome() {
return Outcome.OUTPUT;
public void sendRepliesWithStatus(KernelFunctionality kernel, Message message, int executionCount) {
if (getStatus().equals(MagicCommandOutcomeItem.Status.OK)) {
if (getMIMEContainer().isPresent()) {
kernel.publish(Collections.singletonList(MessageCreator.buildOutputMessage(message, (String) getMIMEContainer().get().getData(), false)));
}
kernel.send(MessageCreator.buildReplyWithOkStatus(message, executionCount));
} else {
kernel.publish(Collections.singletonList(MessageCreator.buildOutputMessage(message, (String) getMIMEContainer().get().getData(), true)));
kernel.send(MessageCreator.buildReplyWithErrorStatus(message, executionCount));
}
}

@Override
public void sendMagicCommandOutcome(KernelFunctionality kernel, Message message, int executionCount) {
if (getMIMEContainer().isPresent()) {
boolean hasError = getStatus().equals(MagicCommandOutcomeItem.Status.ERROR);
kernel.publish(Collections.singletonList(MessageCreator.buildOutputMessage(message, (String) getMIMEContainer().get().getData(), hasError)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.kernel.magic.command.outcome;

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.kernel.msg.MessageCreator;
import com.twosigma.beakerx.message.Message;
import com.twosigma.beakerx.mimetype.MIMEContainer;
import com.twosigma.beakerx.widget.BxHTML;

import java.util.Optional;

import static com.twosigma.beakerx.util.Preconditions.checkNotNull;

public class MagicCommandOutputHTML implements MagicCommandOutcomeItem {

private final MIMEContainer mineContainer;
private MagicCommandOutput.Status status;

public MagicCommandOutputHTML(MagicCommandOutput.Status status, String text) {
this.mineContainer = MIMEContainer.HTML(checkNotNull(text));
this.status = checkNotNull(status);
}

@Override
public Optional<MIMEContainer> getMIMEContainer() {
return Optional.of(this.mineContainer);
}

@Override
public Status getStatus() {
return this.status;
}

@Override
public TryResult getResult() {
return null;
}

@Override
public SimpleEvaluationObject getSimpleEvaluationObject() {
return null;
}

@Override
public void sendRepliesWithStatus(KernelFunctionality kernel, Message message, int executionCount) {
if (getStatus().equals(MagicCommandOutcomeItem.Status.OK)) {
sendHTML(message);
kernel.send(MessageCreator.buildReplyWithOkStatus(message, executionCount));
} else {
sendHTML(message);
kernel.send(MessageCreator.buildReplyWithErrorStatus(message, executionCount));
}
}

private void sendHTML(Message message) {
BxHTML value = new BxHTML(message);
value.setValue(getMIMEContainer().get().getData());
value.display();
}

@Override
public void sendMagicCommandOutcome(KernelFunctionality kernel, Message message, int executionCount) {
sendHTML(message);
}
}
Loading

0 comments on commit dc76805

Please sign in to comment.