Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
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);
});
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();
});
});
'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();
});
'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();
});
'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();
});
'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();
});
'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();
});
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;
});
}
}
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;
});
}
}