Search in sources :

Example 1 with Result

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

Aggregations

JsonObject (com.google.gson.JsonObject)1 ArrayList (java.util.ArrayList)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 Note (org.apache.zeppelin.jupyter.zformat.Note)1 Paragraph (org.apache.zeppelin.jupyter.zformat.Paragraph)1 Result (org.apache.zeppelin.jupyter.zformat.Result)1 TypeData (org.apache.zeppelin.jupyter.zformat.TypeData)1