-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Copy widget scripts to extension folder #11082
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,22 @@ | |
| 'use strict'; | ||
| import type * as jupyterlabService from '@jupyterlab/services'; | ||
| import type * as serialize from '@jupyterlab/services/lib/kernel/serialize'; | ||
| import { sha256 } from 'hash.js'; | ||
| import { inject, injectable } from 'inversify'; | ||
| import { IDisposable } from 'monaco-editor'; | ||
| import * as path from 'path'; | ||
| import { Event, EventEmitter, Uri } from 'vscode'; | ||
| import type { Data as WebSocketData } from 'ws'; | ||
| import { IApplicationShell, IWorkspaceService } from '../../common/application/types'; | ||
| import { traceError } from '../../common/logger'; | ||
| import { traceError, traceInfo } from '../../common/logger'; | ||
| import { IFileSystem } from '../../common/platform/types'; | ||
| import { IConfigurationService, IDisposableRegistry, IHttpClient, IPersistentStateFactory } from '../../common/types'; | ||
| import { | ||
| IConfigurationService, | ||
| IDisposableRegistry, | ||
| IExtensionContext, | ||
| IHttpClient, | ||
| IPersistentStateFactory | ||
| } from '../../common/types'; | ||
| import { createDeferred, Deferred } from '../../common/utils/async'; | ||
| import { IInterpreterService, PythonInterpreter } from '../../interpreter/contracts'; | ||
| import { sendTelemetryEvent } from '../../telemetry'; | ||
|
|
@@ -30,6 +38,8 @@ import { | |
| } from '../types'; | ||
| import { IPyWidgetScriptSourceProvider } from './ipyWidgetScriptSourceProvider'; | ||
| import { WidgetScriptSource } from './types'; | ||
| // tslint:disable: no-var-requires no-require-imports | ||
| const sanitize = require('sanitize-filename'); | ||
|
|
||
| @injectable() | ||
| export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocalResourceUriConverter { | ||
|
|
@@ -41,6 +51,14 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal | |
| public get postInternalMessage(): Event<{ message: string; payload: any }> { | ||
| return this.postInternalMessageEmitter.event; | ||
| } | ||
| private get deserialize(): typeof serialize.deserialize { | ||
| if (!this.jupyterSerialize) { | ||
| // tslint:disable-next-line: no-require-imports | ||
| this.jupyterSerialize = require('@jupyterlab/services/lib/kernel/serialize') as typeof serialize; | ||
| } | ||
| return this.jupyterSerialize.deserialize; | ||
| } | ||
| private readonly resourcesMappedToExtensionFolder = new Map<string, Promise<Uri>>(); | ||
| private notebookIdentity?: Uri; | ||
| private postEmitter = new EventEmitter<{ | ||
| message: string; | ||
|
|
@@ -64,14 +82,9 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal | |
| */ | ||
| private pendingModuleRequests = new Map<string, string>(); | ||
| private jupyterSerialize?: typeof serialize; | ||
| private get deserialize(): typeof serialize.deserialize { | ||
| if (!this.jupyterSerialize) { | ||
| // tslint:disable-next-line: no-require-imports | ||
| this.jupyterSerialize = require('@jupyterlab/services/lib/kernel/serialize') as typeof serialize; | ||
| } | ||
| return this.jupyterSerialize.deserialize; | ||
| } | ||
| private readonly uriConversionPromises = new Map<string, Deferred<Uri>>(); | ||
| private readonly targetWidgetScriptsFolder: string; | ||
| private readonly createTargetWidgetScriptsFolder: Promise<string>; | ||
| constructor( | ||
| @inject(IDisposableRegistry) disposables: IDisposableRegistry, | ||
| @inject(INotebookProvider) private readonly notebookProvider: INotebookProvider, | ||
|
|
@@ -81,8 +94,18 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal | |
| @inject(IHttpClient) private readonly httpClient: IHttpClient, | ||
| @inject(IApplicationShell) private readonly appShell: IApplicationShell, | ||
| @inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, | ||
| @inject(IPersistentStateFactory) private readonly stateFactory: IPersistentStateFactory | ||
| @inject(IPersistentStateFactory) private readonly stateFactory: IPersistentStateFactory, | ||
| @inject(IExtensionContext) extensionContext: IExtensionContext | ||
| ) { | ||
| this.targetWidgetScriptsFolder = path.join(extensionContext.extensionPath, 'tmp', 'nbextensions'); | ||
| this.createTargetWidgetScriptsFolder = this.fs | ||
| .directoryExists(this.targetWidgetScriptsFolder) | ||
| .then(async (exists) => { | ||
| if (!exists) { | ||
| await this.fs.createDirectory(this.targetWidgetScriptsFolder); | ||
| } | ||
| return this.targetWidgetScriptsFolder; | ||
| }); | ||
| disposables.push(this); | ||
| this.notebookProvider.onNotebookCreated( | ||
| (e) => { | ||
|
|
@@ -94,7 +117,39 @@ export class IPyWidgetScriptSource implements IInteractiveWindowListener, ILocal | |
| this.disposables | ||
| ); | ||
| } | ||
| public asWebviewUri(localResource: Uri): Promise<Uri> { | ||
| /** | ||
| * This method is called to convert a Uri to a format such that it can be used in a webview. | ||
| * WebViews only allow files that are part of extension and the same directory where notebook lives. | ||
| * To ensure widgets can find the js files, we copy the script file to a into the extensionr folder `tmp/nbextensions`. | ||
| * (storing files in `tmp/nbextensions` is relatively safe as this folder gets deleted when ever a user updates to a new version of VSC). | ||
DonJayamanne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Hence we need to copy for every version of the extension. | ||
| * Copying into global workspace folder would also work, but over time this folder size could grow (in an unmanaged way). | ||
| */ | ||
| public async asWebviewUri(localResource: Uri): Promise<Uri> { | ||
| if (this.notebookIdentity && !this.resourcesMappedToExtensionFolder.has(localResource.fsPath)) { | ||
| const deferred = createDeferred<Uri>(); | ||
| this.resourcesMappedToExtensionFolder.set(localResource.fsPath, deferred.promise); | ||
| try { | ||
| // Create a file name such that it will be unique and consistent across VSC reloads. | ||
| // Only if original file has been modified should we create a new copy of the sam file. | ||
| const fileHash: string = await this.fs.getFileHash(localResource.fsPath); | ||
| const uniqueFileName = sanitize(sha256().update(`${localResource.fsPath}${fileHash}`).digest('hex')); | ||
| const targetFolder = await this.createTargetWidgetScriptsFolder; | ||
|
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. Seems like the name here should have 'promise' in it somewhere. looks like you forgot to call a function |
||
| const mappedResource = Uri.file( | ||
| path.join(targetFolder, `${uniqueFileName}${path.basename(localResource.fsPath)}`) | ||
| ); | ||
| if (!(await this.fs.fileExists(mappedResource.fsPath))) { | ||
| await this.fs.copyFile(localResource.fsPath, mappedResource.fsPath); | ||
| } | ||
| traceInfo(`Widget Script file ${localResource.fsPath} mapped to ${mappedResource.fsPath}`); | ||
| deferred.resolve(mappedResource); | ||
| } catch (ex) { | ||
| traceError(`Failed to map widget Script file ${localResource.fsPath}`); | ||
| deferred.reject(ex); | ||
| } | ||
| } | ||
| localResource = await this.resourcesMappedToExtensionFolder.get(localResource.fsPath)!; | ||
|
|
||
| const key = localResource.toString(); | ||
| if (!this.uriConversionPromises.has(key)) { | ||
| this.uriConversionPromises.set(key, createDeferred<Uri>()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,12 +17,7 @@ export const WIDGET_MIMETYPE = 'application/vnd.jupyter.widget-view+json'; | |
| // Source borrowed from https://github.com/jupyter-widgets/ipywidgets/blob/master/examples/web3/src/manager.ts | ||
|
|
||
| // These widgets can always be loaded from requirejs (as it is bundled). | ||
| const widgetsRegisteredInRequireJs = [ | ||
| '@jupyter-widgets/controls', | ||
| '@jupyter-widgets/base', | ||
| '@jupyter-widgets/output', | ||
| 'azureml_widgets' | ||
|
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. Does this work now? Meaning they put the source up on unpkg? |
||
| ]; | ||
| const widgetsRegisteredInRequireJs = ['@jupyter-widgets/controls', '@jupyter-widgets/base', '@jupyter-widgets/output']; | ||
|
|
||
| export class WidgetManager extends jupyterlab.WidgetManager { | ||
| public kernel: Kernel.IKernelConnection; | ||
|
|
||
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.
Hey do you want to fix the 'workspace' root bug while you're changing this?