Search in sources :

Example 1 with TypeData

use of org.apache.zeppelin.jupyter.zformat.TypeData in project zeppelin by apache.

the class JupyterUtil method getNote.

public Note getNote(Nbformat nbformat, String id, String codeReplaced, String markdownReplaced) {
    Note note = new Note();
    String name = nbformat.getMetadata().getTitle();
    if (null == name) {
        name = "Note converted from Jupyter_" + id;
    }
    note.setName(name);
    String lineSeparator = System.lineSeparator();
    Paragraph paragraph;
    List<Paragraph> paragraphs = new ArrayList<>();
    String interpreterName;
    List<TypeData> typeDataList;
    for (Cell cell : nbformat.getCells()) {
        String status = Result.SUCCESS;
        paragraph = new Paragraph();
        typeDataList = new ArrayList<>();
        Object cellSource = cell.getSource();
        List<String> sourceRaws = new ArrayList<>();
        if (cellSource instanceof String) {
            sourceRaws.add((String) cellSource);
        } else {
            sourceRaws.addAll((List<String>) cellSource);
        }
        List<String> source = Output.verifyEndOfLine(sourceRaws);
        String codeText = StringUtils.join(source, "");
        if (cell instanceof CodeCell) {
            interpreterName = codeReplaced;
            for (Output output : ((CodeCell) cell).getOutputs()) {
                if (output instanceof Error) {
                    typeDataList.add(output.toZeppelinResult());
                } else {
                    typeDataList.add(output.toZeppelinResult());
                    if (output instanceof Stream) {
                        Stream streamOutput = (Stream) output;
                        if (streamOutput.isError()) {
                            status = Result.ERROR;
                        }
                    }
                }
            }
        } else if (cell instanceof MarkdownCell || cell instanceof HeadingCell) {
            interpreterName = markdownReplaced;
            String markdownContent = markdownParser.render(codeText);
            typeDataList.add(new TypeData(TypeData.HTML, markdownContent));
            paragraph.setUpMarkdownConfig(true);
        } else {
            interpreterName = "";
        }
        paragraph.setText(interpreterName + lineSeparator + codeText);
        paragraph.setResults(new Result(status, typeDataList));
        paragraphs.add(paragraph);
    }
    note.setParagraphs(paragraphs);
    return note;
}
Also used : TypeData(org.apache.zeppelin.jupyter.zformat.TypeData) ArrayList(java.util.ArrayList) Error(org.apache.zeppelin.jupyter.nbformat.Error) Paragraph(org.apache.zeppelin.jupyter.zformat.Paragraph) Result(org.apache.zeppelin.jupyter.zformat.Result) ExecuteResult(org.apache.zeppelin.jupyter.nbformat.ExecuteResult) Note(org.apache.zeppelin.jupyter.zformat.Note) Output(org.apache.zeppelin.jupyter.nbformat.Output) CodeCell(org.apache.zeppelin.jupyter.nbformat.CodeCell) JsonObject(com.google.gson.JsonObject) Stream(org.apache.zeppelin.jupyter.nbformat.Stream) HeadingCell(org.apache.zeppelin.jupyter.nbformat.HeadingCell) MarkdownCell(org.apache.zeppelin.jupyter.nbformat.MarkdownCell) RawCell(org.apache.zeppelin.jupyter.nbformat.RawCell) MarkdownCell(org.apache.zeppelin.jupyter.nbformat.MarkdownCell) CodeCell(org.apache.zeppelin.jupyter.nbformat.CodeCell) HeadingCell(org.apache.zeppelin.jupyter.nbformat.HeadingCell) Cell(org.apache.zeppelin.jupyter.nbformat.Cell)

Example 2 with TypeData

use of org.apache.zeppelin.jupyter.zformat.TypeData in project zeppelin by apache.

the class Output method getZeppelinResult.

protected TypeData getZeppelinResult(Map<String, Object> data, JupyterOutputType type) {
    TypeData result = null;
    Object outputsObject = data.get(type.toString());
    List<String> outputsRaws = new ArrayList<>();
    if (outputsObject instanceof String) {
        outputsRaws.add((String) outputsObject);
    } else {
        outputsRaws.addAll((List<String>) outputsObject);
    }
    List<String> outputs = verifyEndOfLine(outputsRaws);
    String outputData = StringUtils.join(outputs, "");
    if (type == JupyterOutputType.IMAGE_PNG) {
        String base64CodeRaw = outputData;
        String base64Code = base64CodeRaw.replace("\n", "");
        result = new TypeData(type.getZeppelinType().toString(), ZeppelinResultGenerator.toBase64ImageHtmlElement(base64Code));
    } else if (type == JupyterOutputType.LATEX) {
        result = new TypeData(type.getZeppelinType().toString(), ZeppelinResultGenerator.toLatex(outputData));
    } else if (type == JupyterOutputType.APPLICATION_JAVASCRIPT) {
        result = new TypeData(type.getZeppelinType().toString(), ZeppelinResultGenerator.toJavascript(outputData));
    } else {
        result = new TypeData(type.getZeppelinType().toString(), outputData);
    }
    return result;
}
Also used : TypeData(org.apache.zeppelin.jupyter.zformat.TypeData) ArrayList(java.util.ArrayList)

Example 3 with TypeData

use of org.apache.zeppelin.jupyter.zformat.TypeData in project zeppelin by apache.

the class JupyterUtilTest method getNoteAndVerifyData.

@Test
public void getNoteAndVerifyData() throws Exception {
    String noteName = "Note converted from Jupyter";
    InputStream resource = getClass().getResourceAsStream("/basic.ipynb");
    Note n = new JupyterUtil().getNote(new InputStreamReader(resource), "", "%python", "%md");
    assertEquals(8, n.getParagraphs().size());
    assertTrue(n.getName().startsWith(noteName));
    Paragraph firstParagraph = n.getParagraphs().get(0);
    assertEquals("%python\nimport numpy as np", firstParagraph.getText());
    assertEquals("FINISHED", firstParagraph.getStatus());
    Map<String, Object> config = firstParagraph.getConfig();
    assertEquals("ace/mode/python", config.get("editorMode"));
    assertFalse((boolean) config.get("editorHide"));
    Paragraph markdownParagraph = n.getParagraphs().get(6);
    assertEquals("%md\n" + "<div class=\"alert\" style=\"border: 1px solid #aaa; background: radial-gradient(ellipse at center, #ffffff 50%, #eee 100%);\">\n" + "<div class=\"row\">\n" + "    <div class=\"col-sm-1\"><img src=\"https://knowledgeanyhow.org/static/images/favicon_32x32.png\" style=\"margin-top: -6px\"/></div>\n" + "    <div class=\"col-sm-11\">This notebook was created using <a href=\"https://knowledgeanyhow.org\">IBM Knowledge Anyhow Workbench</a>.  To learn more, visit us at <a href=\"https://knowledgeanyhow.org\">https://knowledgeanyhow.org</a>.</div>\n" + "    </div>\n" + "</div>", markdownParagraph.getText());
    assertEquals("FINISHED", markdownParagraph.getStatus());
    Map<String, Object> markdownConfig = markdownParagraph.getConfig();
    assertEquals("ace/mode/markdown", markdownConfig.get("editorMode"));
    assertTrue((boolean) markdownConfig.get("editorHide"));
    assertEquals("SUCCESS", markdownParagraph.getResults().getCode());
    List<TypeData> results = markdownParagraph.getResults().getMsg();
    assertEquals("<div class=\"markdown-body\">\n" + "<div class=\"alert\" style=\"border: 1px solid #aaa; background: radial-gradient(ellipse at center, #ffffff 50%, #eee 100%);\">\n" + "<div class=\"row\">\n" + "    <div class=\"col-sm-1\"><img src=\"https://knowledgeanyhow.org/static/images/favicon_32x32.png\" style=\"margin-top: -6px\"/></div>\n" + "    <div class=\"col-sm-11\">This notebook was created using <a href=\"https://knowledgeanyhow.org\">IBM Knowledge Anyhow Workbench</a>.  To learn more, visit us at <a href=\"https://knowledgeanyhow.org\">https://knowledgeanyhow.org</a>.</div>\n" + "    </div>\n" + "</div>\n" + "</div>", results.get(0).getData());
    assertEquals("HTML", results.get(0).getType());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Note(org.apache.zeppelin.jupyter.zformat.Note) TypeData(org.apache.zeppelin.jupyter.zformat.TypeData) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) Paragraph(org.apache.zeppelin.jupyter.zformat.Paragraph) Test(org.junit.Test)

Aggregations

TypeData (org.apache.zeppelin.jupyter.zformat.TypeData)3 ArrayList (java.util.ArrayList)2 Note (org.apache.zeppelin.jupyter.zformat.Note)2 Paragraph (org.apache.zeppelin.jupyter.zformat.Paragraph)2 JsonObject (com.google.gson.JsonObject)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 JupyterUtil (org.apache.zeppelin.jupyter.JupyterUtil)1 Cell (org.apache.zeppelin.jupyter.nbformat.Cell)1 CodeCell (org.apache.zeppelin.jupyter.nbformat.CodeCell)1 Error (org.apache.zeppelin.jupyter.nbformat.Error)1 ExecuteResult (org.apache.zeppelin.jupyter.nbformat.ExecuteResult)1 HeadingCell (org.apache.zeppelin.jupyter.nbformat.HeadingCell)1 MarkdownCell (org.apache.zeppelin.jupyter.nbformat.MarkdownCell)1 Output (org.apache.zeppelin.jupyter.nbformat.Output)1 RawCell (org.apache.zeppelin.jupyter.nbformat.RawCell)1 Stream (org.apache.zeppelin.jupyter.nbformat.Stream)1 Result (org.apache.zeppelin.jupyter.zformat.Result)1 Test (org.junit.Test)1