Skip to content

Commit 5f8afe8

Browse files
IanMatthewHuffrchiodo
authored andcommitted
Fix multiline and shell magic commands (microsoft#4143)
* tests and hygiene * new entry * remove comment * strip comments before sending code to jupyter * add news
1 parent 83bd144 commit 5f8afe8

5 files changed

Lines changed: 40 additions & 4 deletions

File tree

news/1 Enhancements/4064.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Strip comments before sending so shell command and multiline jupyter magics work correctly.

src/client/datascience/common.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ export function concatMultilineString(str : nbformat.MultilineString) : string {
2020
return str.toString().trim();
2121
}
2222

23+
// Strip out comment lines from code
24+
export function stripComments(str : nbformat.MultilineString): nbformat.MultilineString {
25+
if (Array.isArray(str)) {
26+
return str.filter((value: string) => {
27+
if (value.trim().startsWith('#')) {
28+
return false;
29+
}
30+
31+
return true;
32+
});
33+
} else {
34+
return str;
35+
}
36+
}
37+
2338
export function formatStreamText(str: string) : string {
2439
// Go through the string, looking for \r's that are not followed by \n. This is
2540
// a special case that means replace the string before. This is necessary to

src/client/datascience/jupyter/jupyterServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { createDeferred, Deferred, sleep } from '../../common/utils/async';
2020
import * as localize from '../../common/utils/localize';
2121
import { noop } from '../../common/utils/misc';
2222
import { generateCells } from '../cellFactory';
23-
import { concatMultilineString } from '../common';
23+
import { concatMultilineString, stripComments } from '../common';
2424
import {
2525
CellState,
2626
ICell,
@@ -480,7 +480,7 @@ export class JupyterServer implements INotebookServer, IAsyncDisposable {
480480
// Generate a new request if we still can
481481
if (subscriber.isValid(this.sessionStartTime)) {
482482

483-
const request = this.generateRequest(concatMultilineString(subscriber.cell.data.source), false);
483+
const request = this.generateRequest(concatMultilineString(stripComments(subscriber.cell.data.source)), false);
484484

485485
// tslint:disable-next-line:no-require-imports
486486
const jupyterLab = require('@jupyterlab/services') as typeof import('@jupyterlab/services');

src/test/datascience/mockJupyterManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ExecutionResult, IProcessServiceFactory, IPythonExecutionFactory, Outpu
1616
import { IAsyncDisposableRegistry, IConfigurationService } from '../../client/common/types';
1717
import { EXTENSION_ROOT_DIR } from '../../client/constants';
1818
import { generateCells } from '../../client/datascience/cellFactory';
19-
import { concatMultilineString } from '../../client/datascience/common';
19+
import { concatMultilineString, stripComments } from '../../client/datascience/common';
2020
import {
2121
ICell,
2222
IConnection,
@@ -183,7 +183,7 @@ export class MockJupyterManager implements IJupyterSessionManager {
183183
public addCell(code: string, result?: undefined | string | number | nbformat.IUnrecognizedOutput | nbformat.IExecuteResult | nbformat.IDisplayData | nbformat.IStream | nbformat.IError, mimeType?: string) {
184184
const cells = generateCells(code, 'foo.py', 1, true);
185185
cells.forEach(c => {
186-
const key = concatMultilineString(c.data.source).replace(LineFeedRegEx, '');
186+
const key = concatMultilineString(stripComments(c.data.source)).replace(LineFeedRegEx, '');
187187
if (c.data.cell_type === 'code') {
188188
const massagedResult = this.massageCellResult(result, mimeType);
189189
const data: nbformat.ICodeCell = c.data as nbformat.ICodeCell;

src/test/datascience/notebook.functional.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,26 @@ df.head()`,
654654
result: `<td>A table</td>`,
655655
verifyValue: (d) => assert.ok(d.toString().includes('</td>'), 'Table not found')
656656
},
657+
{
658+
// Important to test as multiline cell magics only work if they are the first item in the cell
659+
code:
660+
`#%% Cell Comment
661+
%%bash
662+
echo 'hello'`,
663+
mimeType: 'text/plain',
664+
cellType: 'code',
665+
result: 'hello',
666+
verifyValue: (d) => assert.equal(d, 'hello', 'Multiline cell magic incorrect')
667+
},
668+
{
669+
// Test shell command should work on PC / Mac / Linux
670+
code:
671+
`!echo world`,
672+
mimeType: 'text/plain',
673+
cellType: 'code',
674+
result: 'world',
675+
verifyValue: (d) => assert.equal(d, 'world', 'Cell command incorrect')
676+
},
657677
{
658678
// Plotly
659679
code:

0 commit comments

Comments
 (0)