Skip to content

Commit dacc210

Browse files
authored
Merge pull request #88 from aws-asolidu/1.4
Add sagemaker-open-notebook-extension
2 parents f89a978 + 13762ab commit dacc210

File tree

14 files changed

+535
-2
lines changed

14 files changed

+535
-2
lines changed

patched-vscode/build/gulpfile.extensions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const compilations = [
6363
'extensions/sagemaker-extension/tsconfig.json',
6464
'extensions/sagemaker-idle-extension/tsconfig.json',
6565
'extensions/sagemaker-terminal-crash-mitigation/tsconfig.json',
66+
'extensions/sagemaker-open-notebook-extension/tsconfig.json',
6667
'extensions/tunnel-forwarding/tsconfig.json',
6768
'extensions/typescript-language-features/test-workspace/tsconfig.json',
6869
'extensions/typescript-language-features/web/tsconfig.json',

patched-vscode/build/npm/dirs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const dirs = [
4242
'extensions/sagemaker-extension',
4343
'extensions/sagemaker-idle-extension',
4444
'extensions/sagemaker-terminal-crash-mitigation',
45+
'extensions/sagemaker-open-notebook-extension',
4546
'extensions/search-result',
4647
'extensions/simple-browser',
4748
'extensions/tunnel-forwarding',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.vscode/**
2+
.vscode-test/**
3+
out/test/**
4+
out/**
5+
test/**
6+
src/**
7+
tsconfig.json
8+
out/test/**
9+
out/**
10+
cgmanifest.json
11+
yarn.lock
12+
preview-src/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Code Editor Open Notebook Extension
2+
3+
The Open Notebook extension enables users to download, transform, and display sample notebooks from a public Amazon S3 bucket owned by the SageMaker team. This extension streamlines the process of accessing and working with SageMaker sample notebooks directly within Code Editor.
4+
5+
## Features
6+
7+
- Download sample notebooks from a specified S3 bucket
8+
- Transform notebooks for compatibility with VSCode
9+
- Display notebooks within the Code Editor environment
10+
- Utilize URL parameters to open specific notebooks
11+
12+
## Usage
13+
14+
The extension uses parameters from the URL to open the desired notebook. The required parameters are:
15+
- Notebook key: The identifier for the specific notebook in the S3 bucket
16+
- Cluster ID: The ID of the SageMaker cluster
17+
- Region: The AWS region where the S3 bucket is located
18+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright Amazon.com Inc. or its affiliates. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withBrowserDefaults = require('../shared.webpack.config').browser;
11+
12+
module.exports = withBrowserDefaults({
13+
context: __dirname,
14+
entry: {
15+
extension: './src/extension.ts'
16+
},
17+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright Amazon.com Inc. or its affiliates. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
//@ts-check
7+
8+
'use strict';
9+
10+
const withDefaults = require('../shared.webpack.config');
11+
12+
module.exports = withDefaults({
13+
context: __dirname,
14+
resolve: {
15+
mainFields: ['module', 'main']
16+
},
17+
entry: {
18+
extension: './src/extension.ts',
19+
}
20+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "sagemaker-open-notebook-extension",
3+
"displayName": "Sagemaker open notebook Extension",
4+
"description": "To download and open sample notebook when open code editor",
5+
"extensionKind": [
6+
"workspace"
7+
],
8+
"version": "1.0.0",
9+
"publisher": "sagemaker",
10+
"license": "MIT",
11+
"engines": {
12+
"vscode": "^1.70.0"
13+
},
14+
"main": "./out/extension",
15+
"categories": [
16+
"Other"
17+
],
18+
"activationEvents": [
19+
"*"
20+
],
21+
"capabilities": {
22+
"virtualWorkspaces": true,
23+
"untrustedWorkspaces": {
24+
"supported": true
25+
}
26+
},
27+
"contributes": {
28+
"configuration": {
29+
"type": "object",
30+
"title": "Sagemaker Open Notebook Extension",
31+
"properties": {}
32+
},
33+
"commands": [
34+
]
35+
},
36+
"scripts": {
37+
"compile": "gulp compile-extension:sagemaker-open-notebook-extension",
38+
"watch": "npm run build-preview && gulp watch-extension:sagemaker-open-notebook-extension",
39+
"vscode:prepublish": "npm run build-ext",
40+
"build-ext": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:sagemaker-open-notebook-extension ./tsconfig.json"
41+
},
42+
"dependencies": {},
43+
"repository": {}
44+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
import * as vscode from 'vscode';
3+
import * as https from 'https';
4+
import * as fs from 'fs';
5+
import * as path from 'path';
6+
import * as os from 'os';
7+
import * as console from 'console';
8+
9+
export function activate() {
10+
const config = vscode.workspace.getConfiguration('extensions.openNotebookData');
11+
const notebookKey = config.get('notebookKey') as string;
12+
const clusterId = config.get('clusterId') as string;
13+
const region = config.get('region') as string;
14+
if(notebookKey){
15+
loadAndDisplayNotebook(notebookKey, clusterId, region);
16+
}
17+
18+
}
19+
20+
async function loadAndDisplayNotebook(fileKey: string, clusterId: string, region: string) {
21+
const bucketName = `jumpstart-cache-prod-${region}`;
22+
const url = `https://${bucketName}.s3.${region}.amazonaws.com/${fileKey}`;
23+
try {
24+
let content = await downloadFile(url);
25+
content = processNotebookContent(content, clusterId, region);
26+
const tempDir = os.tmpdir();
27+
const tempFilePath = path.join(tempDir, 'downloaded-notebook.ipynb');
28+
fs.writeFileSync(tempFilePath, content);
29+
const uri = vscode.Uri.file(tempFilePath);
30+
await openNotebookDocument(uri);
31+
} catch (error) {
32+
vscode.window.showErrorMessage('Error downloading or opening notebook: ' + error.message);
33+
}
34+
}
35+
36+
function processNotebookContent(content: string, clusterId: string, region: string): string {
37+
const notebook = JSON.parse(content);
38+
notebook.cells = notebook.cells.map((cell: any) => {
39+
if (cell.metadata &&
40+
cell.metadata.jumpStartAlterations &&
41+
cell.metadata.jumpStartAlterations.includes('clusterId')) {
42+
cell.source = [
43+
"%%bash\n",
44+
`aws ssm start-session --target sagemaker-cluster:${clusterId} --region ${region}`
45+
];
46+
cell.cell_type = "code";
47+
}
48+
return cell;
49+
});
50+
return JSON.stringify(notebook, null, 2);
51+
}
52+
53+
async function openNotebookDocument(uri: vscode.Uri) {
54+
try {
55+
// Open the notebook document
56+
const document = await vscode.workspace.openNotebookDocument(uri);
57+
// Show the notebook document in a notebook editor
58+
await vscode.window.showNotebookDocument(document);
59+
} catch (error) {
60+
console.error('Failed to open notebook:', error);
61+
vscode.window.showErrorMessage('Failed to open notebook: ' + error.message);
62+
}
63+
}
64+
65+
function downloadFile(url: string): Promise<string> {
66+
return new Promise((resolve, reject) => {
67+
https.get(url, (response) => {
68+
let data = '';
69+
response.on('data', (chunk) => {
70+
data += chunk;
71+
});
72+
response.on('end', () => {
73+
resolve(data);
74+
});
75+
}).on('error', (error) => {
76+
reject(error);
77+
});
78+
});
79+
}
80+
export function deactivate() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"compilerOptions": {
4+
"outDir": "./out"
5+
},
6+
"include": [
7+
"../sagemaker-open-notebook-extension/src/**/*",
8+
"../../src/vscode-dts/vscode.d.ts"
9+
]
10+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+

0 commit comments

Comments
 (0)