How to use the @jupyterlab/outputarea.OutputArea.execute function in @jupyterlab/outputarea

To help you get started, we’ve selected a few @jupyterlab/outputarea 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 jupyterlab / jupyterlab / packages / cells / src / widget.ts View on Github external
metadata = {
      ...model.metadata.toJSON(),
      ...metadata,
      ...cellId
    };
    model.executionCount = null;
    cell.outputHidden = false;
    cell.setPrompt('*');
    model.trusted = true;

    let future: Kernel.IFuture<
      KernelMessage.IExecuteRequestMsg,
      KernelMessage.IExecuteReplyMsg
    >;
    try {
      const msgPromise = OutputArea.execute(
        code,
        cell.outputArea,
        session,
        metadata
      );
      // Save this execution's future so we can compare in the catch below.
      future = cell.outputArea.future;
      const msg = await msgPromise;
      model.executionCount = msg.content.execution_count;
      return msg;
    } catch (e) {
      // If this is still the current execution, clear the prompt.
      if (e.message === 'Canceled' && cell.outputArea.future === future) {
        cell.setPrompt('');
      }
      throw e;
github jupyterlab / jupyterlab / tests / test-outputarea / src / widget.spec.ts View on Github external
it('should clear existing outputs', async () => {
        widget.model.fromJSON(NBTestUtils.DEFAULT_OUTPUTS);
        const reply = await OutputArea.execute(CODE, widget, session);
        expect(reply.content.execution_count).to.be.ok;
        expect(model.length).to.equal(1);
      });
github jupyterlab / jupyterlab-data-explorer / tests / test-outputarea / src / widget.spec.ts View on Github external
it('should allow an error given "raises-exception" metadata tag', async () => {
        let ipySession: ClientSession;
        ipySession = await createClientSession({
          kernelPreference: { name: 'ipython' }
        });
        await ipySession.initialize();
        await ipySession.kernel.ready;
        const widget1 = new LogOutputArea({ rendermime, model });
        const metadata = { tags: ['raises-exception'] };
        const future1 = OutputArea.execute(
          'a++1',
          widget,
          ipySession,
          metadata
        );
        const future2 = OutputArea.execute('a=1', widget1, ipySession);
        const reply = await future1;
        const reply2 = await future2;
        expect(reply.content.status).to.equal('error');
        expect(reply2.content.status).to.equal('ok');
        widget1.dispose();
        await ipySession.shutdown();
      });
    });
github jupyterlab / jupyterlab / tests / test-outputarea / src / widget.spec.ts View on Github external
'display_with_id(1, "here")',
          'display("below")'
        ].join('\n');
        const code2 = [
          'display_with_id(2, "here")',
          'display_with_id(3, "there")',
          'display_with_id(4, "here")'
        ].join('\n');

        let ipySession: ClientSession;
        ipySession = await createClientSession({
          kernelPreference: { name: 'ipython' }
        });
        await ipySession.initialize();
        await ipySession.kernel.ready;
        const promise0 = OutputArea.execute(code0, widget0, ipySession);
        const promise1 = OutputArea.execute(code1, widget1, ipySession);
        await Promise.all([promise0, promise1]);
        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '1' });
        await OutputArea.execute(code2, widget2, ipySession);

        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '4' });
        expect(model2.length).to.equal(3);
        const outputs = model2.toJSON();
        expect(outputs[0].data).to.deep.equal({ 'text/plain': '4' });
        expect(outputs[1].data).to.deep.equal({ 'text/plain': '3' });
        expect(outputs[2].data).to.deep.equal({ 'text/plain': '4' });
        await ipySession.shutdown();
      });
github jupyterlab / jupyterlab-data-explorer / tests / test-outputarea / src / widget.spec.ts View on Github external
'display("below")'
        ].join('\n');
        const code2 = [
          'display_with_id(2, "here")',
          'display_with_id(3, "there")',
          'display_with_id(4, "here")'
        ].join('\n');

        let ipySession: ClientSession;
        ipySession = await createClientSession({
          kernelPreference: { name: 'ipython' }
        });
        await ipySession.initialize();
        await ipySession.kernel.ready;
        const promise0 = OutputArea.execute(code0, widget0, ipySession);
        const promise1 = OutputArea.execute(code1, widget1, ipySession);
        await Promise.all([promise0, promise1]);
        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '1' });
        await OutputArea.execute(code2, widget2, ipySession);

        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '4' });
        expect(model2.length).to.equal(3);
        const outputs = model2.toJSON();
        expect(outputs[0].data).to.deep.equal({ 'text/plain': '4' });
        expect(outputs[1].data).to.deep.equal({ 'text/plain': '3' });
        expect(outputs[2].data).to.deep.equal({ 'text/plain': '4' });
        await ipySession.shutdown();
      });
github jupyterlab / jupyterlab / tests / test-outputarea / src / widget.spec.ts View on Github external
'display_with_id(3, "there")',
          'display_with_id(4, "here")'
        ].join('\n');

        let ipySession: ClientSession;
        ipySession = await createClientSession({
          kernelPreference: { name: 'ipython' }
        });
        await ipySession.initialize();
        await ipySession.kernel.ready;
        const promise0 = OutputArea.execute(code0, widget0, ipySession);
        const promise1 = OutputArea.execute(code1, widget1, ipySession);
        await Promise.all([promise0, promise1]);
        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '1' });
        await OutputArea.execute(code2, widget2, ipySession);

        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '4' });
        expect(model2.length).to.equal(3);
        const outputs = model2.toJSON();
        expect(outputs[0].data).to.deep.equal({ 'text/plain': '4' });
        expect(outputs[1].data).to.deep.equal({ 'text/plain': '3' });
        expect(outputs[2].data).to.deep.equal({ 'text/plain': '4' });
        await ipySession.shutdown();
      });
github jupyterlab / jupyterlab / tests / test-outputarea / src / widget.spec.ts View on Github external
'display("below")'
        ].join('\n');
        const code2 = [
          'display_with_id(2, "here")',
          'display_with_id(3, "there")',
          'display_with_id(4, "here")'
        ].join('\n');

        let ipySession: ClientSession;
        ipySession = await createClientSession({
          kernelPreference: { name: 'ipython' }
        });
        await ipySession.initialize();
        await ipySession.kernel.ready;
        const promise0 = OutputArea.execute(code0, widget0, ipySession);
        const promise1 = OutputArea.execute(code1, widget1, ipySession);
        await Promise.all([promise0, promise1]);
        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '1' });
        await OutputArea.execute(code2, widget2, ipySession);

        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '4' });
        expect(model2.length).to.equal(3);
        const outputs = model2.toJSON();
        expect(outputs[0].data).to.deep.equal({ 'text/plain': '4' });
        expect(outputs[1].data).to.deep.equal({ 'text/plain': '3' });
        expect(outputs[2].data).to.deep.equal({ 'text/plain': '4' });
        await ipySession.shutdown();
      });
github jupyterlab / jupyterlab-data-explorer / tests / test-outputarea / src / widget.spec.ts View on Github external
'display_with_id(3, "there")',
          'display_with_id(4, "here")'
        ].join('\n');

        let ipySession: ClientSession;
        ipySession = await createClientSession({
          kernelPreference: { name: 'ipython' }
        });
        await ipySession.initialize();
        await ipySession.kernel.ready;
        const promise0 = OutputArea.execute(code0, widget0, ipySession);
        const promise1 = OutputArea.execute(code1, widget1, ipySession);
        await Promise.all([promise0, promise1]);
        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '1' });
        await OutputArea.execute(code2, widget2, ipySession);

        expect(model1.length).to.equal(3);
        expect(model1.toJSON()[1].data).to.deep.equal({ 'text/plain': '4' });
        expect(model2.length).to.equal(3);
        const outputs = model2.toJSON();
        expect(outputs[0].data).to.deep.equal({ 'text/plain': '4' });
        expect(outputs[1].data).to.deep.equal({ 'text/plain': '3' });
        expect(outputs[2].data).to.deep.equal({ 'text/plain': '4' });
        await ipySession.shutdown();
      });
github jupyterlab / jupyterlab-data-explorer / jupyterlab / packages / cells / src / widget.ts View on Github external
let model = cell.model;
    let code = model.value.text;
    if (!code.trim() || !session.kernel) {
      model.executionCount = null;
      model.outputs.clear();
      return Promise.resolve(void 0);
    }

    let cellId = { cellId: model.id };
    metadata = { ...metadata, ...cellId };
    model.executionCount = null;
    cell.outputHidden = false;
    cell.setPrompt('*');
    model.trusted = true;

    return OutputArea.execute(code, cell.outputArea, session, metadata)
      .then(msg => {
        model.executionCount = msg.content.execution_count;
        return msg;
      })
      .catch(e => {
        if (e.message === 'Canceled') {
          cell.setPrompt('');
        }
        throw e;
      });
  }
}
github yuvipanda / simplest-notebook / packages / cells / src / widget.ts View on Github external
function execute(cell: CodeCell, session: IClientSession): Promise {
    let model = cell.model;
    let code = model.value.text;
    if (!code.trim() || !session.kernel) {
      model.executionCount = null;
      model.outputs.clear();
      return Promise.resolve(void 0);
    }

    model.executionCount = null;
    cell.outputHidden = false;
    cell.setPrompt('*');
    model.trusted = true;

    return OutputArea.execute(code, cell.outputArea, session).then(msg => {
      model.executionCount = msg.content.execution_count;
      return msg;
    });
  }
}