forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Remove unwanted elements from cells when saving. #11164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix problem with opening a notebook in jupyter after saving in VS code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the MIT License. | ||
'use strict'; | ||
|
||
import { nbformat } from '@jupyterlab/coreutils'; | ||
import { assert } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
import { anything, instance, mock, verify, when } from 'ts-mockito'; | ||
|
@@ -13,12 +14,13 @@ import { PythonSettings } from '../../client/common/configSettings'; | |
import { ConfigurationService } from '../../client/common/configuration/service'; | ||
import { IConfigurationService, IPythonSettings } from '../../client/common/types'; | ||
import { CommandRegistry } from '../../client/datascience/commands/commandRegistry'; | ||
import { pruneCell } from '../../client/datascience/common'; | ||
import { DataScience } from '../../client/datascience/datascience'; | ||
import { DataScienceCodeLensProvider } from '../../client/datascience/editor-integration/codelensprovider'; | ||
import { IDataScienceCodeLensProvider } from '../../client/datascience/types'; | ||
|
||
// tslint:disable: max-func-body-length | ||
suite('Data Science Tests', () => { | ||
suite('DataScience Tests', () => { | ||
let dataScience: DataScience; | ||
let cmdManager: CommandManager; | ||
let codeLensProvider: IDataScienceCodeLensProvider; | ||
|
@@ -75,4 +77,169 @@ suite('Data Science Tests', () => { | |
assert.ok(onDidChangeActiveTextEditor.calledOnce); | ||
}); | ||
}); | ||
|
||
suite('Cell pruning', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now this is clearly a good function for unit test. Nice. |
||
test('Remove output and execution count from non code', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'markdown', | ||
outputs: [], | ||
execution_count: '23', | ||
source: 'My markdown', | ||
metadata: {} | ||
}; | ||
const result = pruneCell(cell); | ||
assert.equal(Object.keys(result).indexOf('outputs'), -1, 'Outputs inside markdown'); | ||
assert.equal(Object.keys(result).indexOf('execution_count'), -1, 'Execution count inside markdown'); | ||
}); | ||
test('Outputs dont contain extra data', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'code', | ||
outputs: [ | ||
{ | ||
output_type: 'display_data', | ||
extra: {} | ||
} | ||
], | ||
execution_count: '23', | ||
source: 'My source', | ||
metadata: {} | ||
}; | ||
const result = pruneCell(cell); | ||
// tslint:disable-next-line: no-any | ||
assert.equal((result.outputs as any).length, 1, 'Outputs were removed'); | ||
assert.equal(result.execution_count, '23', 'Output execution count removed'); | ||
const output = (result.outputs as nbformat.IOutput[])[0]; | ||
assert.equal(Object.keys(output).indexOf('extra'), -1, 'Output still has extra data'); | ||
assert.notEqual(Object.keys(output).indexOf('output_type'), -1, 'Output is missing output_type'); | ||
}); | ||
test('Display outputs still have their data', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'code', | ||
execution_count: 2, | ||
metadata: {}, | ||
outputs: [ | ||
{ | ||
output_type: 'display_data', | ||
data: { | ||
'text/plain': "Box(children=(Label(value='My label'),))", | ||
'application/vnd.jupyter.widget-view+json': { | ||
version_major: 2, | ||
version_minor: 0, | ||
model_id: '90c99248d7bb490ca132427de6d1e235' | ||
} | ||
}, | ||
metadata: { bob: 'youruncle' } | ||
} | ||
], | ||
source: ["line = widgets.Label('My label')\n", 'box = widgets.Box([line])\n', 'box'] | ||
}; | ||
|
||
const result = pruneCell(cell); | ||
// tslint:disable-next-line: no-any | ||
assert.equal((result.outputs as any).length, 1, 'Outputs were removed'); | ||
assert.equal(result.execution_count, 2, 'Output execution count removed'); | ||
assert.deepEqual(result.outputs, cell.outputs, 'Outputs were modified'); | ||
}); | ||
test('Stream outputs still have their data', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'code', | ||
execution_count: 2, | ||
metadata: {}, | ||
outputs: [ | ||
{ | ||
output_type: 'stream', | ||
name: 'stdout', | ||
text: 'foobar' | ||
} | ||
], | ||
source: ["line = widgets.Label('My label')\n", 'box = widgets.Box([line])\n', 'box'] | ||
}; | ||
|
||
const result = pruneCell(cell); | ||
// tslint:disable-next-line: no-any | ||
assert.equal((result.outputs as any).length, 1, 'Outputs were removed'); | ||
assert.equal(result.execution_count, 2, 'Output execution count removed'); | ||
assert.deepEqual(result.outputs, cell.outputs, 'Outputs were modified'); | ||
}); | ||
test('Errors outputs still have their data', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'code', | ||
execution_count: 2, | ||
metadata: {}, | ||
outputs: [ | ||
{ | ||
output_type: 'error', | ||
ename: 'stdout', | ||
evalue: 'stdout is a value', | ||
traceback: ['more'] | ||
} | ||
], | ||
source: ["line = widgets.Label('My label')\n", 'box = widgets.Box([line])\n', 'box'] | ||
}; | ||
|
||
const result = pruneCell(cell); | ||
// tslint:disable-next-line: no-any | ||
assert.equal((result.outputs as any).length, 1, 'Outputs were removed'); | ||
assert.equal(result.execution_count, 2, 'Output execution count removed'); | ||
assert.deepEqual(result.outputs, cell.outputs, 'Outputs were modified'); | ||
}); | ||
test('Execute result outputs still have their data', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'code', | ||
execution_count: 2, | ||
metadata: {}, | ||
outputs: [ | ||
{ | ||
output_type: 'execute_result', | ||
execution_count: '4', | ||
data: { | ||
'text/plain': "Box(children=(Label(value='My label'),))", | ||
'application/vnd.jupyter.widget-view+json': { | ||
version_major: 2, | ||
version_minor: 0, | ||
model_id: '90c99248d7bb490ca132427de6d1e235' | ||
} | ||
}, | ||
metadata: { foo: 'bar' } | ||
} | ||
], | ||
source: ["line = widgets.Label('My label')\n", 'box = widgets.Box([line])\n', 'box'] | ||
}; | ||
|
||
const result = pruneCell(cell); | ||
// tslint:disable-next-line: no-any | ||
assert.equal((result.outputs as any).length, 1, 'Outputs were removed'); | ||
assert.equal(result.execution_count, 2, 'Output execution count removed'); | ||
assert.deepEqual(result.outputs, cell.outputs, 'Outputs were modified'); | ||
}); | ||
test('Unrecognized outputs still have their data', () => { | ||
const cell: nbformat.ICell = { | ||
cell_type: 'code', | ||
execution_count: 2, | ||
metadata: {}, | ||
outputs: [ | ||
{ | ||
output_type: 'unrecognized', | ||
execution_count: '4', | ||
data: { | ||
'text/plain': "Box(children=(Label(value='My label'),))", | ||
'application/vnd.jupyter.widget-view+json': { | ||
version_major: 2, | ||
version_minor: 0, | ||
model_id: '90c99248d7bb490ca132427de6d1e235' | ||
} | ||
}, | ||
metadata: {} | ||
} | ||
], | ||
source: ["line = widgets.Label('My label')\n", 'box = widgets.Box([line])\n', 'box'] | ||
}; | ||
|
||
const result = pruneCell(cell); | ||
// tslint:disable-next-line: no-any | ||
assert.equal((result.outputs as any).length, 1, 'Outputs were removed'); | ||
assert.equal(result.execution_count, 2, 'Output execution count removed'); | ||
assert.deepEqual(result.outputs, cell.outputs, 'Outputs were modified'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only support notebook format 4, right? This check works, but seems like it could be broken by an older format or a new format change. I think ok for now though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it would be okay because the output is either