How to use the @nteract/commutable.appendCellToNotebook 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 / nteract / packages / reducers / __tests__ / document.spec.ts View on Github external
test("cleans up the outputs, pagers, and status", () => {
    const notebook = appendCellToNotebook(emptyNotebook, emptyCodeCell);
    const id = notebook.get("cellOrder").first();

    const initialState = makeDocumentRecord({
      filename: "test.ipynb",
      notebook,
      cellPagers: Immutable.Map({
        // Hokey data, we're just expecting it to be cleared
        id: Immutable.List(["a", "b"])
      }),
      transient: Immutable.Map({
        cellMap: Immutable.Map({
          id: Immutable.Map({
            status: "idle"
          })
        })
      })
github nteract / hydrogen / spec / store / index-spec.js View on Github external
it("should return a fully-fledged notebook when the file isn't empty", () => {
      const source1 = 'print("Hola World! I <3 ZMQ!")';
      const source2 = "2 + 2";
      editor.setText(`# %%\n${source1}\n# %%\n${source2}\n`);
      store.updateEditor(editor);
      const codeCell1 = commutable.emptyCodeCell.set("source", source1);
      const codeCell2 = commutable.emptyCodeCell.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,
        codeCell1
      );
      nb = commutable.appendCellToNotebook(nb, codeCell2);
      expect(store.notebook).toEqual(commutable.toJS(nb));
    });
github nteract / hydrogen / lib / store / index.js View on Github external
const { start, end } = cell;
      let source = codeManager.getTextInRange(editor, start, end);
      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 / packages / reducers / __tests__ / document.spec.ts View on Github external
test("should create and focus a new markdown cell if last cell and last cell is markdown cell", () => {
    const originalState = monocellDocument.set(
      "notebook",
      appendCellToNotebook(fixtureCommutable, emptyMarkdownCell)
    );

    const id = originalState.getIn(["notebook", "cellOrder"]).last();
    const state = reducers(
      originalState,
      actions.focusNextCell({ id, createCellIfUndefined: true })
    );
    const newCellId = state.getIn(["notebook", "cellOrder"]).last();
    const newCellType = state.getIn([
      "notebook",
      "cellMap",
      newCellId,
      "cell_type"
    ]);

    expect(state.cellFocused).not.toBeNull();
github nteract / nteract / packages / core / __tests__ / metadata-spec.js View on Github external
import * as constants from "../src/constants";
import { MetadataRecord } from "../src/records";

import { appendCellToNotebook, emptyCodeCell } from "@nteract/commutable";

import { dummyCommutable } from "../src/dummy";

import { metadata as reducers } from "../src/reducers";

const initialDocument = new Map();
initialDocument.set(
  "notebook",
  appendCellToNotebook(dummyCommutable, emptyCodeCell)
);

describe("changeFilename", () => {
  test("returns the same originalState if filename is undefined", () => {
    const originalState = new MetadataRecord({
      filename: "original.ipynb"
    });

    const action = { type: constants.CHANGE_FILENAME };

    const state = reducers(originalState, action);
    expect(state.filename).toBe("original.ipynb");
  });
  test("sets the filename if given a valid one", () => {
    const originalState = new MetadataRecord({
      filename: "original.ipynb"
github nteract / nteract / packages / reducers / __tests__ / document.spec.ts View on Github external
test("doesn't clear outputs on markdown cells", () => {
    const notebook = appendCellToNotebook(emptyNotebook, emptyMarkdownCell);

    const originalState = makeDocumentRecord({
      notebook,
      filename: "test.ipynb"
    });

    const id = originalState.getIn(["notebook", "cellOrder"]).last();
    const state = reducers(originalState, actions.clearOutputs({ id }));
    const outputs = state.getIn(["notebook", "cellMap", id, "outputs"]);
    expect(outputs).toBeUndefined();
  });
  test("clear prompts on code cells", () => {
github nteract / nteract / packages / epics / __tests__ / ipywidgets.spec.ts View on Github external
import { toArray } from "rxjs/operators";
import Immutable from "immutable";

import { emptyCodeCell, appendCellToNotebook } from "@nteract/commutable";
import { actions, createContentRef, makeDocumentRecord } from "@nteract/core";
import { fixtureCommutable } from "@nteract/fixtures";

import { ipywidgetsModel$ } from "../src/ipywidgets";
import { from } from "rxjs";

const monocellDocument = makeDocumentRecord({
  notebook: appendCellToNotebook(fixtureCommutable, emptyCodeCell)
});

describe("ipywidgetsModel$", () => {
  test("emits AppendOutput if comm_open for LinkModel is sent", done => {
    const contentRef = createContentRef();
    const comm_id = "a_comm_id";
    const messages = [
      {
        header: { msg_type: "comm_open" },
        content: {
          comm_id,
          data: {
            state: {
              _model_name: "LinkModel"
            }
          }
github nteract / nteract / packages / reducers / __tests__ / document.spec.ts View on Github external
test("should create and focus a new code cell if last cell and last cell is code cell", () => {
    const originalState = monocellDocument.set(
      "notebook",
      appendCellToNotebook(fixtureCommutable, emptyCodeCell)
    );

    const id = originalState.getIn(["notebook", "cellOrder"]).last();
    const state = reducers(
      originalState,
      actions.focusNextCell({ id, createCellIfUndefined: true })
    );
    const newCellId = state.getIn(["notebook", "cellOrder"]).last();
    const newCellType = state.getIn([
      "notebook",
      "cellMap",
      newCellId,
      "cell_type"
    ]);

    expect(state.cellFocused).not.toBeNull();
github nteract / nteract / packages / fixtures / src / index.ts View on Github external
function buildFixtureNotebook(config: JSONObject) {
  let notebook = monocellNotebook.setIn(
    ["metadata", "kernelspec", "name"],
    "python2"
  );

  if (config) {
    if (config.codeCellCount) {
      for (let i = 1; i < config.codeCellCount; i++) {
        notebook = appendCellToNotebook(notebook, emptyCodeCell);
      }
    }

    if (config.markdownCellCount) {
      for (let i = 0; i < config.markdownCellCount; i++) {
        notebook = appendCellToNotebook(
          notebook,
          emptyMarkdownCell.set("cell_type", "markdown")
        );
      }
    }

    if (config.hideAll) {
      notebook = hideCells(notebook);
    }
  }