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);
}
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;
}
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());
}
Aggregations