Search in sources :

Example 1 with JupyterUtil

use of org.apache.zeppelin.jupyter.JupyterUtil in project zeppelin by apache.

the class NotebookServer method convertNote.

protected void convertNote(NotebookSocket conn, Message fromMessage) throws IOException {
    String noteId = fromMessage.get("noteId").toString();
    getNotebook().processNote(noteId, note -> {
        if (note == null) {
            throw new IOException("No such note: " + noteId);
        } else {
            Message resp = new Message(OP.CONVERTED_NOTE_NBFORMAT).put("nbformat", new JupyterUtil().getNbformat(note.toJson())).put("noteName", fromMessage.get("noteName"));
            conn.send(serializeMessage(resp));
            return null;
        }
    });
}
Also used : OnMessage(javax.websocket.OnMessage) ClusterMessage(org.apache.zeppelin.cluster.event.ClusterMessage) Message(org.apache.zeppelin.common.Message) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) IOException(java.io.IOException)

Example 2 with JupyterUtil

use of org.apache.zeppelin.jupyter.JupyterUtil in project zeppelin by apache.

the class JupyterUtilTest method getNbFormat.

@Test
public void getNbFormat() throws Exception {
    InputStream resource = getClass().getResourceAsStream("/basic.ipynb");
    Nbformat nbformat = new JupyterUtil().getNbformat(new InputStreamReader(resource));
    assertTrue(nbformat.getCells().get(0) instanceof CodeCell);
    resource = getClass().getResourceAsStream("/examples.ipynb");
    nbformat = new JupyterUtil().getNbformat(new InputStreamReader(resource));
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) Test(org.junit.Test)

Example 3 with JupyterUtil

use of org.apache.zeppelin.jupyter.JupyterUtil in project zeppelin by apache.

the class JupyterUtilTest method getNote.

@Test
public void getNote() throws Exception {
    InputStream resource = getClass().getResourceAsStream("/examples.ipynb");
    Note n = new JupyterUtil().getNote(new InputStreamReader(resource), "", "%python", "%md");
    assertNotNull(n);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Note(org.apache.zeppelin.jupyter.zformat.Note) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) Test(org.junit.Test)

Example 4 with JupyterUtil

use of org.apache.zeppelin.jupyter.JupyterUtil in project zeppelin by apache.

the class JupyterUtilTest method testgetNbformat.

@Test
public void testgetNbformat() {
    InputStream resource = getClass().getResourceAsStream("/spark_example_notebook.zpln");
    String text = new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
    JupyterUtil util = new JupyterUtil();
    Nbformat nbformat = util.getNbformat(new StringReader(util.getNbformat(text)));
    assertEquals(7, nbformat.getCells().size());
    assertEquals(3, nbformat.getCells().stream().filter(c -> c instanceof MarkdownCell).count());
    assertEquals(4, nbformat.getCells().stream().filter(c -> c instanceof CodeCell).count());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) JupyterUtil(org.apache.zeppelin.jupyter.JupyterUtil) Test(org.junit.Test)

Example 5 with JupyterUtil

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

JupyterUtil (org.apache.zeppelin.jupyter.JupyterUtil)5 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 Test (org.junit.Test)4 Note (org.apache.zeppelin.jupyter.zformat.Note)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 OnMessage (javax.websocket.OnMessage)1 ClusterMessage (org.apache.zeppelin.cluster.event.ClusterMessage)1 Message (org.apache.zeppelin.common.Message)1 Paragraph (org.apache.zeppelin.jupyter.zformat.Paragraph)1 TypeData (org.apache.zeppelin.jupyter.zformat.TypeData)1