コード例 #1
0
 @NotNull
 public static IpnbFile parseIpnbFile(
     @NotNull final CharSequence fileText, @NotNull final VirtualFile virtualFile)
     throws IOException {
   final String path = virtualFile.getPath();
   IpnbFileRaw rawFile = gson.fromJson(fileText.toString(), IpnbFileRaw.class);
   if (rawFile == null) {
     int nbformat = isIpythonNewFormat(virtualFile) ? 4 : 3;
     return new IpnbFile(Collections.emptyMap(), nbformat, Lists.newArrayList(), path);
   }
   List<IpnbCell> cells = new ArrayList<IpnbCell>();
   final IpnbWorksheet[] worksheets = rawFile.worksheets;
   if (worksheets == null) {
     for (IpnbCellRaw rawCell : rawFile.cells) {
       cells.add(rawCell.createCell());
     }
   } else {
     for (IpnbWorksheet worksheet : worksheets) {
       final List<IpnbCellRaw> rawCells = worksheet.cells;
       for (IpnbCellRaw rawCell : rawCells) {
         cells.add(rawCell.createCell());
       }
     }
   }
   return new IpnbFile(rawFile.metadata, rawFile.nbformat, cells, path);
 }
コード例 #2
0
    public static CellOutputRaw fromOutput(@NotNull final IpnbOutputCell outputCell, int nbformat) {
      final CellOutputRaw raw = new CellOutputRaw();
      raw.metadata = outputCell.getMetadata();
      if (raw.metadata == null
          && !(outputCell instanceof IpnbStreamOutputCell)
          && !(outputCell instanceof IpnbErrorOutputCell)) {
        raw.metadata = Collections.emptyMap();
      }

      if (outputCell instanceof IpnbPngOutputCell) {
        if (nbformat == 4) {
          final OutputDataRaw dataRaw = new OutputDataRaw();
          dataRaw.png = ((IpnbPngOutputCell) outputCell).getBase64String();
          dataRaw.text = outputCell.getText();
          raw.data = dataRaw;
          raw.execution_count = outputCell.getPromptNumber();
          raw.output_type =
              outputCell.getPromptNumber() != null ? "execute_result" : "display_data";
        } else {
          raw.png = ((IpnbPngOutputCell) outputCell).getBase64String();
          raw.text = outputCell.getText();
        }
      } else if (outputCell instanceof IpnbSvgOutputCell) {
        if (nbformat == 4) {
          final OutputDataRaw dataRaw = new OutputDataRaw();
          dataRaw.text = outputCell.getText();
          dataRaw.svg = ((IpnbSvgOutputCell) outputCell).getSvg();
          raw.data = dataRaw;
          raw.execution_count = outputCell.getPromptNumber();
          raw.output_type =
              outputCell.getPromptNumber() != null ? "execute_result" : "display_data";
        } else {
          raw.svg = ((IpnbSvgOutputCell) outputCell).getSvg();
          raw.text = outputCell.getText();
        }
      } else if (outputCell instanceof IpnbJpegOutputCell) {
        if (nbformat == 4) {
          final OutputDataRaw dataRaw = new OutputDataRaw();
          dataRaw.text = outputCell.getText();
          dataRaw.jpeg = Lists.newArrayList(((IpnbJpegOutputCell) outputCell).getBase64String());
          raw.data = dataRaw;
        } else {
          raw.jpeg = ((IpnbJpegOutputCell) outputCell).getBase64String();
          raw.text = outputCell.getText();
        }
      } else if (outputCell instanceof IpnbLatexOutputCell) {
        if (nbformat == 4) {
          final OutputDataRaw dataRaw = new OutputDataRaw();
          dataRaw.text = outputCell.getText();
          dataRaw.latex = ((IpnbLatexOutputCell) outputCell).getLatex();
          raw.data = dataRaw;
          raw.execution_count = outputCell.getPromptNumber();
          raw.output_type = "execute_result";
        } else {
          raw.latex = ((IpnbLatexOutputCell) outputCell).getLatex();
          raw.text = outputCell.getText();
          raw.prompt_number = outputCell.getPromptNumber();
        }
      } else if (outputCell instanceof IpnbStreamOutputCell) {
        if (nbformat == 4) {
          raw.name = ((IpnbStreamOutputCell) outputCell).getStream();
        } else {
          raw.stream = ((IpnbStreamOutputCell) outputCell).getStream();
        }
        raw.text = outputCell.getText();
        raw.output_type = "stream";
      } else if (outputCell instanceof IpnbHtmlOutputCell) {
        if (nbformat == 4) {
          final OutputDataRaw dataRaw = new OutputDataRaw();
          dataRaw.html = ((IpnbHtmlOutputCell) outputCell).getHtmls();
          dataRaw.text = outputCell.getText();
          raw.data = dataRaw;
          raw.execution_count = outputCell.getPromptNumber();
        } else {
          raw.html = ((IpnbHtmlOutputCell) outputCell).getHtmls();
        }
        raw.output_type = nbformat == 4 ? "execute_result" : "pyout";
      } else if (outputCell instanceof IpnbErrorOutputCell) {
        raw.output_type = nbformat == 4 ? "error" : "pyerr";
        raw.evalue = ((IpnbErrorOutputCell) outputCell).getEvalue();
        raw.ename = ((IpnbErrorOutputCell) outputCell).getEname();
        raw.traceback = outputCell.getText();
      } else if (outputCell instanceof IpnbOutOutputCell) {
        if (nbformat == 4) {
          raw.execution_count = outputCell.getPromptNumber();
          raw.output_type = "execute_result";
          final OutputDataRaw dataRaw = new OutputDataRaw();
          dataRaw.text = outputCell.getText();
          raw.data = dataRaw;
        } else {
          raw.output_type = "pyout";
          raw.prompt_number = outputCell.getPromptNumber();
          raw.text = outputCell.getText();
        }
      } else {
        raw.text = outputCell.getText();
      }
      return raw;
    }