How to use the @nteract/commutable.toJS function in @nteract/commutable

To help you get started, we’ve selected a few @nteract/commutable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nteract / hydrogen / spec / store / index-spec.js View on Github external
it("should export markdown to markdown cells", () => {
      const source1 = 'print("Hola World! I <3 ZMQ!")';
      const source2 = "2 + 2";
      editor.setText(`# %%\n${source1}\n# %% markdown\n${source2}\n`);
      store.updateEditor(editor);
      const codeCell = commutable.emptyCodeCell.set("source", source1);
      const markdownCell = commutable.emptyMarkdownCell.set("source", source2);
      // The outputted notebook will have three cells because currently a cell
      // is always created before the first `# %%`
      let nb = commutable.appendCellToNotebook(
        commutable.emptyNotebook,
        codeCell
      );
      nb = commutable.appendCellToNotebook(nb, markdownCell);
      expect(store.notebook).toEqual(commutable.toJS(nb));
    });
  });
github nteract / hydrogen / spec / store / index-spec.js View on Github external
it("should return an empty notebook for empty file", () => {
      store.updateEditor(editor);
      // Build a notebook with one code cell.
      const nb = commutable.emptyNotebook;
      expect(store.notebook).toEqual(commutable.toJS(nb));
    });
github nteract / hydrogen / lib / store / index.js View on Github external
source = source ? source : "";
      // When the cell marker following a given cell range is on its own line,
      // the newline immediately preceding that cell marker is included in
      // `source`. We remove that here. See #1512 for more details.
      if (source.slice(-1) === "\n") source = source.slice(0, -1);
      const cellType = codeManager.getMetadataForRow(editor, start);
      let newCell;
      if (cellType === "codecell") {
        newCell = commutable.emptyCodeCell.set("source", source);
      } else if (cellType === "markdown") {
        source = codeManager.removeCommentsMarkdownCell(editor, source);
        newCell = commutable.emptyMarkdownCell.set("source", source);
      }
      notebook = commutable.appendCellToNotebook(notebook, newCell);
    });
    return commutable.toJS(notebook);
  }
github nteract / nteract / applications / desktop / src / notebook / epics / loading.ts View on Github external
const timestamp = new Date();
      const filepath =
        (
          action.payload.filepath !== null &&
          path.resolve(action.payload.filepath)
        ) || path.join(action.payload.cwd, "Untitled.ipynb");

      return actions.fetchContentFulfilled({
        filepath,
        model: {
          type: "notebook",
          mimetype: notebookMediaType,
          format: "json",
          // Back to JS, only to immutableify it inside of the reducer
          content: toJS(notebook),
          writable: true,
          name: path.basename(filepath),
          path: filepath,
          created: timestamp.toString(),
          last_modified: timestamp.toString()
        },
        kernelRef: action.payload.kernelRef,
        contentRef: action.payload.contentRef
      });
    })
  );
github nteract / nteract / packages / epics / src / contents.ts View on Github external
state: AppState,
  content:
    | RecordOf
    | RecordOf
    | RecordOf
    | RecordOf
) {
  // This could be object for notebook, or string for files
  let serializedData: Notebook | string;
  let saveModel: Partial> = {};
  if (content.type === "notebook") {
    const appVersion = selectors.appVersion(state);

    // contents API takes notebook as raw JSON whereas downloading takes
    // a string
    serializedData = toJS(
      content.model.notebook.setIn(
        ["metadata", "nteract", "version"],
        appVersion
      )
    );
    saveModel = {
      content: serializedData,
      type: content.type
    };
  } else if (content.type === "file") {
    serializedData = content.model.text;
    saveModel = {
      content: serializedData,
      type: content.type,
      format: "text"
    };
github nteract / nteract / applications / notebook-on-next / pages / edit.js View on Github external
static async getInitialProps(context: Object) {
    const query = context.query;
    const isServer = context.isServer;

    let serverNotebook;
    if (query.gistid) {
      serverNotebook = await fetchFromGist(query.gistid);
    }

    if (!serverNotebook) {
      serverNotebook = toJS(monocellNotebook);
    }

    throw new Error(
      "Notebook on next has not been migrated to the new state tree"
    );

    // TODO: provide notebook using fetchContentFulfilled
    store.dispatch(actions.fetchContentFulfilled({}));
    return { serverNotebook, isServer };
  }
github nteract / nteract / packages / epics / src / hosts.ts View on Github external
| RecordOf
        | undefined = selectors
        .contentByRef(state)
        .get(action.payload.contentRef);

      if (!content || content.type !== "notebook") {
        return of({
          type: "ERROR",
          error: true,
          payload: {
            error: new Error("Only Notebooks can be published to Bookstore")
          }
        }) as any;
      }

      const notebook: NotebookV4 = toJS(content.model.notebook);

      // Save notebook first before sending to Bookstore
      return contents
        .save(serverConfig, content.filepath, {
          content: notebook,
          type: "notebook"
        })
        .pipe(
          tap((xhr: AjaxResponse) => {
            if (xhr.status !== 200) {
              throw new Error(xhr.response);
            }
          }),
          map((nb: AjaxResponse) => {
            return actions.publishToBookstoreAfterSave({
              contentRef: action.payload.contentRef,
github nteract / nteract / applications / desktop / __tests__ / renderer / epics / loading.spec.ts View on Github external
const filepath = process.platform === "win32"
      ? "\\home\\whatever\\Untitled.ipynb"
      : "/home/whatever/Untitled.ipynb";

    expect(responseActions).toEqual([
      {
        type: actions.FETCH_CONTENT_FULFILLED,
        payload: {
          contentRef: "cRef",
          kernelRef: "kRef",
          filepath: filepath,
          model: {
            type: "notebook",
            mimetype: "application/x-ipynb+json",
            format: "json",
            content: toJS(
              monocellNotebook
                .setIn(["metadata", "kernel_info", "name"], "hylang")
                .setIn(["metadata", "language_info", "name"], "hylang")
            ),
            writable: true,
            name: "Untitled.ipynb",
            path: filepath,
            created: expect.any(String),
            last_modified: expect.any(String)
          }
        }
      }
    ]);
  });
});
github nteract / nteract / applications / desktop / src / notebook / epics / saving.ts View on Github external
}
      const model = content.model;

      if (!model || model.type !== "notebook") {
        return of(
          actions.saveFailed({
            contentRef: action.payload.contentRef,
            error: new Error("no notebook loaded to save")
          })
        );
      }

      const filepath = content.filepath;
      const appVersion = selectors.appVersion(state);
      const notebook = stringifyNotebook(
        toJS(
          model.notebook.setIn(["metadata", "nteract", "version"], appVersion)
        )
      );
      return writeFileObservable(filepath, notebook).pipe(
        map(() => {
          if (process.platform !== "darwin") {
            const notificationSystem = selectors.notificationSystem(
              state$.value
            );
            notificationSystem.addNotification({
              autoDismiss: 2,
              level: "success",
              title: "Save successful!"
            });
          }
          return actions.saveFulfilled({