Search in sources :

Example 1 with MIMEContainer

use of com.twosigma.beakerx.mimetype.MIMEContainer in project beakerx by twosigma.

the class HtmlMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String commandCodeBlock = param.getCommandCodeBlock();
    if (commandCodeBlock == null) {
        return new MagicCommandOutput(MagicCommandOutput.Status.ERROR, String.format(USAGE_ERROR_MSG, HTML));
    }
    MIMEContainer html = HTML("<html>" + commandCodeBlock + "</html>");
    return new MagicCommandResult(MagicCommandOutcomeItem.Status.OK, html);
}
Also used : MagicCommandOutput(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput) MagicCommandResult(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandResult) MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer)

Example 2 with MIMEContainer

use of com.twosigma.beakerx.mimetype.MIMEContainer in project beakerx by twosigma.

the class MIMEContainerFactory method createMIMEContainersFromObject.

private static List<MIMEContainer> createMIMEContainersFromObject(final Object data) {
    Object input = DisplayerDataMapper.convert(data);
    if (input instanceof DisplayableWidget) {
        ((DisplayableWidget) input).display();
        return HIDDEN_MIME;
    }
    TableDisplay table = getTableDisplay(input);
    if (table != null) {
        table.display();
        return HIDDEN_MIME;
    }
    if (input instanceof Collection) {
        return singletonList(MIMEContainer.Text(collectionToString((Collection<?>) input)));
    }
    if (input instanceof XYGraphics) {
        new Plot().add((XYGraphics) input).display();
        return HIDDEN_MIME;
    }
    if (input instanceof MIMEContainer) {
        return singletonList((MIMEContainer) input);
    }
    return Displayers.display(input).entrySet().stream().map(item -> new MIMEContainer(item.getKey(), item.getValue())).collect(Collectors.toList());
}
Also used : Displayers(jupyter.Displayers) Plot(com.twosigma.beakerx.chart.xychart.Plot) Iterator(java.util.Iterator) Text(com.twosigma.beakerx.mimetype.MIMEContainer.Text) TableDisplay(com.twosigma.beakerx.table.TableDisplay) Collection(java.util.Collection) MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer) HIDDEN(com.twosigma.beakerx.mimetype.MIMEContainer.HIDDEN) Collectors(java.util.stream.Collectors) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) XYGraphics(com.twosigma.beakerx.chart.xychart.plotitem.XYGraphics) DisplayableWidget(com.twosigma.beakerx.widget.DisplayableWidget) Map(java.util.Map) Kernel(com.twosigma.beakerx.kernel.Kernel) DisplayableWidget(com.twosigma.beakerx.widget.DisplayableWidget) MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer) TableDisplay(com.twosigma.beakerx.table.TableDisplay) Plot(com.twosigma.beakerx.chart.xychart.Plot) XYGraphics(com.twosigma.beakerx.chart.xychart.plotitem.XYGraphics) Collection(java.util.Collection)

Example 3 with MIMEContainer

use of com.twosigma.beakerx.mimetype.MIMEContainer in project beakerx by twosigma.

the class MagicCommandExecutor method publishOutcome.

private static void publishOutcome(KernelFunctionality kernel, Message message, int executionCount, MagicCommandOutcomeItem item, boolean hasError) {
    if (item.getMIMEContainer().isPresent()) {
        if (item.getOutcome().equals(MagicCommandOutcomeItem.Outcome.OUTPUT)) {
            kernel.publish(Collections.singletonList(MessageCreator.buildOutputMessage(message, (String) item.getMIMEContainer().get().getData(), hasError)));
        } else {
            MIMEContainer mimeContainer = item.getMIMEContainer().get();
            kernel.publish(Collections.singletonList(MessageCreator.buildMessage(message, singletonList(mimeContainer), executionCount)));
        }
    }
}
Also used : MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer)

Example 4 with MIMEContainer

use of com.twosigma.beakerx.mimetype.MIMEContainer in project beakerx by twosigma.

the class JavaScriptMagicCommand method execute.

@Override
public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
    String commandCodeBlock = param.getCommandCodeBlock();
    MIMEContainer result = JavaScript(commandCodeBlock);
    return new MagicCommandResult(MagicCommandOutcomeItem.Status.OK, result);
}
Also used : MagicCommandResult(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandResult) MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer)

Example 5 with MIMEContainer

use of com.twosigma.beakerx.mimetype.MIMEContainer in project beakerx by twosigma.

the class MIMEContainerFactoryTest method integerAsTextHtml.

@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 = MIMEContainerFactory.createMIMEContainers(2);
    // then
    assertThat(result.get(0)).isEqualTo(new MIMEContainer(TEXT_HTML, "<div><h1>" + 2 + "</h1></div>"));
}
Also used : MIMEContainer(com.twosigma.beakerx.mimetype.MIMEContainer) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

MIMEContainer (com.twosigma.beakerx.mimetype.MIMEContainer)7 MagicCommandResult (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandResult)2 Map (java.util.Map)2 Plot (com.twosigma.beakerx.chart.xychart.Plot)1 XYGraphics (com.twosigma.beakerx.chart.xychart.plotitem.XYGraphics)1 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)1 Kernel (com.twosigma.beakerx.kernel.Kernel)1 MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)1 HIDDEN (com.twosigma.beakerx.mimetype.MIMEContainer.HIDDEN)1 Text (com.twosigma.beakerx.mimetype.MIMEContainer.Text)1 TableDisplay (com.twosigma.beakerx.table.TableDisplay)1 DisplayableWidget (com.twosigma.beakerx.widget.DisplayableWidget)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Displayers (jupyter.Displayers)1 Test (org.junit.Test)1