forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Prompt to launch TensorBoard when active Python file or ipynb contains a tensorboard import #14884
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
8 commits
Select commit
Hold shift + click to select a range
db37fb6
Show prompt when tensorboard is imported in the active .py or .ipynb …
joyceerhl 2c49399
Linter
joyceerhl 598ec81
Whoops
joyceerhl ce6de9f
Register disposables and remove duplicated code
joyceerhl 6e6e2bc
Fix dupes
joyceerhl 414d699
Nullish coalescing
joyceerhl dc4965f
Merge branch 'main' of https://github.com/microsoft/vscode-python int…
joyceerhl 4d9aee7
Merge branch 'tensorboard-imports' of https://github.com/microsoft/vs…
joyceerhl 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
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { inject, injectable } from 'inversify'; | ||
import { noop } from 'lodash'; | ||
import * as path from 'path'; | ||
import { Event, EventEmitter, TextEditor, window } from 'vscode'; | ||
import { IExtensionSingleActivationService } from '../activation/types'; | ||
import { IDocumentManager } from '../common/application/types'; | ||
import { IDisposableRegistry } from '../common/types'; | ||
import { getDocumentLines } from '../telemetry/importTracker'; | ||
import { ITensorBoardImportTracker } from './types'; | ||
|
||
// While it is uncommon for users to `import tensorboard`, TensorBoard is frequently | ||
// included as a submodule of other packages, e.g. torch.utils.tensorboard. | ||
// This is a modified version of the regex from src/client/telemetry/importTracker.ts | ||
// in order to match on imported submodules as well, since the original regex only | ||
// matches the 'main' module. | ||
const ImportRegEx = /^\s*from (?<fromImport>\w+(?:\.\w+)*) import (?<fromImportTarget>\w+(?:, \w+)*)(?: as \w+)?|import (?<importImport>\w+(?:, \w+)*)(?: as \w+)?$/; | ||
|
||
@injectable() | ||
export class TensorBoardImportTracker implements ITensorBoardImportTracker, IExtensionSingleActivationService { | ||
private pendingChecks = new Map<string, NodeJS.Timer | number>(); | ||
private _onDidImportTensorBoard = new EventEmitter<void>(); | ||
|
||
constructor( | ||
@inject(IDocumentManager) private documentManager: IDocumentManager, | ||
@inject(IDisposableRegistry) private disposables: IDisposableRegistry | ||
) { | ||
this.documentManager.onDidChangeActiveTextEditor( | ||
(e) => this.onChangedActiveTextEditor(e), | ||
this, | ||
this.disposables | ||
); | ||
} | ||
|
||
// Fires when the active text editor contains a tensorboard import. | ||
public get onDidImportTensorBoard(): Event<void> { | ||
return this._onDidImportTensorBoard.event; | ||
} | ||
|
||
public dispose() { | ||
this.pendingChecks.clear(); | ||
} | ||
|
||
public async activate(): Promise<void> { | ||
// Process active text editor with a timeout delay | ||
this.onChangedActiveTextEditor(window.activeTextEditor); | ||
} | ||
|
||
private onChangedActiveTextEditor(editor: TextEditor | undefined) { | ||
if (!editor || !editor.document) { | ||
return; | ||
} | ||
const document = editor.document; | ||
if ( | ||
(path.extname(document.fileName) === '.ipynb' && document.languageId === 'python') || | ||
path.extname(document.fileName) === '.py' | ||
) { | ||
const lines = getDocumentLines(document); | ||
this.lookForImports(lines); | ||
} | ||
} | ||
|
||
private lookForImports(lines: (string | undefined)[]) { | ||
try { | ||
for (const s of lines) { | ||
const matches = s ? ImportRegEx.exec(s) : null; | ||
if (matches === null || matches.groups === undefined) { | ||
continue; | ||
} | ||
let componentsToCheck: string[] = []; | ||
if (matches.groups.fromImport && matches.groups.fromImportTarget) { | ||
// from x.y.z import u, v, w | ||
componentsToCheck = matches.groups.fromImport | ||
.split('.') | ||
.concat(matches.groups.fromImportTarget.split(',')); | ||
} else if (matches.groups.importImport) { | ||
// import package1, package2, ... | ||
componentsToCheck = matches.groups.importImport.split(','); | ||
} | ||
for (const component of componentsToCheck) { | ||
if (component && component.trim() === 'tensorboard') { | ||
this._onDidImportTensorBoard.fire(); | ||
return; | ||
} | ||
} | ||
} | ||
} catch { | ||
// Don't care about failures. | ||
noop(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { Event } from 'vscode'; | ||
|
||
export const ITensorBoardImportTracker = Symbol('ITensorBoardImportTracker'); | ||
export interface ITensorBoardImportTracker { | ||
onDidImportTensorBoard: Event<void>; | ||
} |
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
86 changes: 86 additions & 0 deletions
86
src/test/tensorBoard/tensorBoardImportTracker.unit.test.ts
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,86 @@ | ||
import * as sinon from 'sinon'; | ||
import { TensorBoardImportTracker } from '../../client/tensorBoard/tensorBoardImportTracker'; | ||
import { MockDocumentManager } from '../startPage/mockDocumentManager'; | ||
|
||
suite('TensorBoard import tracker', () => { | ||
let documentManager: MockDocumentManager; | ||
let tensorBoardImportTracker: TensorBoardImportTracker; | ||
let onDidImportTensorBoardListener: sinon.SinonExpectation; | ||
|
||
setup(() => { | ||
documentManager = new MockDocumentManager(); | ||
tensorBoardImportTracker = new TensorBoardImportTracker(documentManager, []); | ||
onDidImportTensorBoardListener = sinon.expectation.create('onDidImportTensorBoardListener'); | ||
tensorBoardImportTracker.onDidImportTensorBoard(onDidImportTensorBoardListener); | ||
}); | ||
|
||
test('Simple tensorboard import in Python file', async () => { | ||
const document = documentManager.addDocument('import tensorboard', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('Simple tensorboard import in Python ipynb', async () => { | ||
const document = documentManager.addDocument('import tensorboard', 'foo.ipynb'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('`from x.y.tensorboard import z` import', async () => { | ||
const document = documentManager.addDocument('from torch.utils.tensorboard import SummaryWriter', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('`from x.y import tensorboard` import', async () => { | ||
const document = documentManager.addDocument('from torch.utils import tensorboard', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('`import x, y` import', async () => { | ||
const document = documentManager.addDocument('import tensorboard, tensorflow', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('`import pkg as _` import', async () => { | ||
const document = documentManager.addDocument('import tensorboard as tb', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('Fire on changed text editor', async () => { | ||
await tensorBoardImportTracker.activate(); | ||
const document = documentManager.addDocument('import tensorboard as tb', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
onDidImportTensorBoardListener.once().verify(); | ||
}); | ||
test('Do not fire event if no tensorboard import', async () => { | ||
const document = documentManager.addDocument('import tensorflow as tf\nfrom torch.utils import foo', 'foo.py'); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.never().verify(); | ||
}); | ||
test('Do not fire event if language is not Python', async () => { | ||
const document = documentManager.addDocument( | ||
'import tensorflow as tf\nfrom torch.utils import foo', | ||
'foo.cpp', | ||
'cpp' | ||
); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.never().verify(); | ||
}); | ||
test('Ignore docstrings', async () => { | ||
const document = documentManager.addDocument( | ||
`""" | ||
import tensorboard | ||
"""`, | ||
'foo.py' | ||
); | ||
await documentManager.showTextDocument(document); | ||
await tensorBoardImportTracker.activate(); | ||
onDidImportTensorBoardListener.never().verify(); | ||
}); | ||
}); |
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.
Per @jmew if the user selects 'yes', we will disable the prompt for the current session only, and continue to show the prompt on the next session where the trigger conditions are met, until the user selects 'doNotAskAgain'.