提交 2c34226f 编写于 作者: R rebornix

Notebook Provider for both ipynb and test.ipynb

上级 6616034c
......@@ -26,6 +26,16 @@
{
"viewType": "jupyter",
"displayName": "Jupyter",
"selector": [
{
"filenamePattern": "*.ipynb",
"excludeFileNamePattern": "*.test.ipynb"
}
]
},
{
"viewType": "jupyterTest",
"displayName": "Jupyter Test",
"selector": [
{
"filenamePattern": "*.test.ipynb"
......
......@@ -9,6 +9,7 @@ import { NotebookProvider } from './notebookProvider';
export function activate(context: vscode.ExtensionContext) {
console.log(context.extensionPath);
context.subscriptions.push(vscode.window.registerNotebookProvider('jupyter', new NotebookProvider(context.extensionPath)));
context.subscriptions.push(vscode.window.registerNotebookProvider('jupyter', new NotebookProvider(context.extensionPath, true)));
context.subscriptions.push(vscode.window.registerNotebookProvider('jupyterTest', new NotebookProvider(context.extensionPath, false)));
}
......@@ -40,6 +40,10 @@ export class Cell implements vscode.ICell {
outputsFullFilled() {
return this._outputs && this.outputs.length === this._outputs.length;
}
clearOutputs() {
this.outputs = [];
}
}
export class JupyterNotebook implements vscode.INotebook {
......@@ -56,17 +60,26 @@ export class NotebookProvider implements vscode.NotebookProvider {
onDidChangeNotebook: vscode.Event<{ resource: vscode.Uri; notebook: vscode.INotebook; }> = this._onDidChangeNotebook.event;
private _notebooks: Map<string, JupyterNotebook> = new Map();
constructor(private _extensionPath: string) {
constructor(private _extensionPath: string, private fillOutputs: boolean) {
}
async resolveNotebook(resource: vscode.Uri): Promise<vscode.INotebook | undefined> {
if (this._notebooks.has(resource.fsPath)) {
return this._notebooks.get(resource.fsPath);
let notebook = this._notebooks.get(resource.fsPath);
if (!this.fillOutputs) {
notebook?.cells.forEach(cell => {
cell.clearOutputs();
});
}
return notebook;
}
let content = await vscode.workspace.fs.readFile(resource);
try {
let notebookJSON = JSON.parse(content.toString());
let notebook = new JupyterNotebook(
notebookJSON.metadata,
notebookJSON.cells.map((rawCell: any) => {
......@@ -78,6 +91,45 @@ export class NotebookProvider implements vscode.NotebookProvider {
})
);
if (this.fillOutputs) {
let preloadScript = false;
notebook!.cells.forEach(cell => {
if (cell.outputsFullFilled()) {
return;
}
if (!preloadScript) {
let containHTML = cell.containHTML();
if (containHTML) {
preloadScript = true;
const scriptPathOnDisk = vscode.Uri.file(
path.join(this._extensionPath, 'dist', 'ipywidgets.js')
);
let scriptUri = scriptPathOnDisk.with({ scheme: 'vscode-resource' });
cell.insertDependencies(
{
'output_type': 'display_data',
'data': {
'text/html': [
`<script src="${scriptUri}"></script>\n`,
]
}
}
);
cell.fillInOutputs();
} else {
cell.fillInOutputs();
}
} else {
cell.fillInOutputs();
}
});
}
this._notebooks.set(resource.fsPath, notebook);
return Promise.resolve(notebook);
......
......@@ -76,6 +76,13 @@ export class BackLayerWebView extends Disposable {
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
#container > div {
width: 100%;
padding: 9px;
background-color: var(--vscode-list-inactiveSelectionBackground);
}
</style>
</head>
<body style="overflow: hidden;">
<div id='container' class="widgetarea" style="position: absolute;width:100%;"></div>
......
......@@ -52,6 +52,10 @@ const notebookContribution: IJSONSchema = {
type: 'string',
description: nls.localize('contributes.notebook.selector.filenamePattern', 'Glob that the notebook is enabled for.'),
},
excludeFileNamePattern: {
type: 'string',
description: nls.localize('contributes.notebook.selector.excludeFileNamePattern', 'Glob that the notebook is disabled for.')
}
}
}
}
......
......@@ -45,6 +45,8 @@
.monaco-workbench .part.editor > .content .notebook-editor .output {
margin-left: 24px;
margin-right: 24px;
padding-left: 8px;
padding-right: 8px;
}
.monaco-workbench .part.editor > .content .notebook-editor .output p {
......
......@@ -416,4 +416,10 @@ registerThemingParticipant((theme, collector) => {
if (quoteBorder) {
collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor blockquote { border-color: ${quoteBorder}; }`);
}
const inactiveListItem = theme.getColor('list.inactiveSelectionBackground');
if (inactiveListItem) {
collector.addRule(`.monaco-workbench .part.editor > .content .notebook-editor .output { background-color: ${inactiveListItem}; }`);
}
});
......@@ -9,6 +9,7 @@ import { basename } from 'vs/base/common/resources';
export interface NotebookSelector {
readonly filenamePattern?: string;
readonly excludeFileNamePattern?: string;
}
export class NotebookProviderInfo {
......@@ -34,6 +35,13 @@ export class NotebookProviderInfo {
static selectorMatches(selector: NotebookSelector, resource: URI): boolean {
if (selector.filenamePattern) {
if (glob.match(selector.filenamePattern.toLowerCase(), basename(resource).toLowerCase())) {
if (selector.excludeFileNamePattern) {
if (glob.match(selector.excludeFileNamePattern.toLowerCase(), basename(resource).toLowerCase())) {
// should exclude
return false;
}
}
return true;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册