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