Search in sources :

Example 1 with Paragraph

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

the class JupyterUtil method getNbformat.

public String getNbformat(String note) {
    Note noteFormat = getGson(new GsonBuilder()).fromJson(note, Note.class);
    JsonObject nbformat = new JsonObject();
    JsonArray cells = new JsonArray();
    Pattern mdPattern = Pattern.compile("%md\\s.*", Pattern.DOTALL);
    Pattern sqlPattern = Pattern.compile("%sql\\s", Pattern.DOTALL);
    Pattern unknownMagicPattern = Pattern.compile("%\\w+\\s", Pattern.DOTALL);
    Pattern htmlPattern = Pattern.compile("%html\\s", Pattern.DOTALL);
    Pattern sparkPattern = Pattern.compile("%spark\\s", Pattern.DOTALL);
    int index = 0;
    for (Paragraph paragraph : noteFormat.getParagraphs()) {
        String code = StringUtils.stripStart(paragraph.getText(), " ");
        JsonObject codeJson = new JsonObject();
        if (code == null || code.trim().isEmpty())
            continue;
        if (mdPattern.matcher(code).matches()) {
            codeJson.addProperty("cell_type", "markdown");
            codeJson.add("metadata", new JsonObject());
            codeJson.addProperty("source", StringUtils.stripStart(StringUtils.stripStart(code, "%md"), // remove '%md'
            "\n"));
        } else if (sqlPattern.matcher(code).matches() || htmlPattern.matcher(code).matches()) {
            codeJson.addProperty("cell_type", "code");
            codeJson.addProperty("execution_count", index);
            codeJson.add("metadata", new JsonObject());
            codeJson.add("outputs", new JsonArray());
            // add % to convert to cell magic
            codeJson.addProperty("source", "%" + code);
        } else if (sparkPattern.matcher(code).matches()) {
            codeJson.addProperty("cell_type", "code");
            codeJson.addProperty("execution_count", index);
            JsonObject metadataJson = new JsonObject();
            metadataJson.addProperty("autoscroll", "auto");
            codeJson.add("metadata", metadataJson);
            codeJson.add("outputs", new JsonArray());
            codeJson.addProperty("source", code);
        } else if (unknownMagicPattern.matcher(code).matches()) {
            // use raw cells for unknown magic
            codeJson.addProperty("cell_type", "raw");
            JsonObject metadataJson = new JsonObject();
            metadataJson.addProperty("format", "text/plain");
            codeJson.add("metadata", metadataJson);
            codeJson.addProperty("source", code);
        } else {
            codeJson.addProperty("cell_type", "code");
            codeJson.addProperty("execution_count", index);
            JsonObject metadataJson = new JsonObject();
            metadataJson.addProperty("autoscroll", "auto");
            codeJson.add("metadata", metadataJson);
            codeJson.add("outputs", new JsonArray());
            codeJson.addProperty("source", code);
        }
        cells.add(codeJson);
        index++;
    }
    JsonObject metadataJson = new JsonObject();
    JsonObject kernelspecJson = new JsonObject();
    kernelspecJson.addProperty("language", "scala");
    kernelspecJson.addProperty("name", "spark2-scala");
    JsonObject languageInfoJson = new JsonObject();
    languageInfoJson.addProperty("codemirror_mode", "text/x-scala");
    languageInfoJson.addProperty("file_extension", ".scala");
    languageInfoJson.addProperty("mimetype", "text/x-scala");
    languageInfoJson.addProperty("name", "scala");
    languageInfoJson.addProperty("pygments_lexer", "scala");
    metadataJson.addProperty("name", noteFormat.getName());
    metadataJson.add("kernelspec", kernelspecJson);
    metadataJson.add("language_info", languageInfoJson);
    nbformat.add("metadata", metadataJson);
    nbformat.addProperty("nbformat", 4);
    nbformat.addProperty("nbformat_minor", 2);
    nbformat.add("cells", cells);
    return PRETTY_GSON.toJson(nbformat);
}
Also used : JsonArray(com.google.gson.JsonArray) Pattern(java.util.regex.Pattern) GsonBuilder(com.google.gson.GsonBuilder) Note(org.apache.zeppelin.jupyter.zformat.Note) JsonObject(com.google.gson.JsonObject) Paragraph(org.apache.zeppelin.jupyter.zformat.Paragraph)

Example 2 with Paragraph

use of org.apache.zeppelin.jupyter.zformat.Paragraph 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 3 with Paragraph

use of org.apache.zeppelin.jupyter.zformat.Paragraph 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

Note (org.apache.zeppelin.jupyter.zformat.Note)3 Paragraph (org.apache.zeppelin.jupyter.zformat.Paragraph)3 JsonObject (com.google.gson.JsonObject)2 TypeData (org.apache.zeppelin.jupyter.zformat.TypeData)2 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)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