Skip to content

Commit 6193e64

Browse files
committed
working version in lab4
1 parent 9e0fb9f commit 6193e64

File tree

10 files changed

+6874
-78
lines changed

10 files changed

+6874
-78
lines changed

binder/environment.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# a mybinder.org-ready environment for demoing jupyterlab_snippets
1+
# a mybinder.org-ready environment for demoing jupyterlab-snippets
22
# this environment may also be used locally on Linux/MacOS/Windows, e.g.
33
#
44
# conda env update --file binder/environment.yml
@@ -18,4 +18,4 @@ dependencies:
1818
- pip
1919
- wheel
2020
# additional packages for demos
21-
# - ipywidgets
21+
# - ipywidgets

binder/postBuild

100755100644
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
""" perform a development install of jupyterlab_snippets
2+
""" perform a development install of jupyterlab-snippets
33
44
On Binder, this will run _after_ the environment has been fully created from
55
the environment.yml in this directory.
@@ -52,5 +52,5 @@ _("jupyter", "server", "extension", "list")
5252
_("jupyter", "labextension", "list")
5353

5454

55-
print("JupyterLab with jupyterlab_snippets is ready to run with:\n")
55+
print("JupyterLab with jupyterlab-snippets is ready to run with:\n")
5656
print("\tjupyter lab\n")

jupyterlab_snippets/__init__.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import json
2-
from pathlib import Path
3-
4-
from ._version import __version__
1+
try:
2+
from ._version import __version__
3+
except ImportError:
4+
# Fallback when using the package in dev mode without installing
5+
# in editable mode with pip. It is highly recommended to install
6+
# the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs
7+
import warnings
8+
warnings.warn("Importing 'jupyterlab_snippets' outside a proper installation.")
9+
__version__ = "dev"
510
from .handlers import setup_handlers
611
from .loader import SnippetsLoader
712

8-
HERE = Path(__file__).parent.resolve()
9-
10-
with (HERE / "labextension" / "package.json").open() as fid:
11-
data = json.load(fid)
12-
1313

1414
def _jupyter_labextension_paths():
15-
return [{"src": "labextension", "dest": data["name"]}]
16-
15+
return [{
16+
"src": "labextension",
17+
"dest": "jupyterlab_snippets"
18+
}]
1719

1820
def _jupyter_server_extension_points():
1921
return [{"module": "jupyterlab_snippets"}]

jupyterlab_snippets/_version.py

-19
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,4 @@
212212
"value-no-vendor-prefix": null
213213
}
214214
}
215-
}
215+
}

src/handler.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { URLExt } from '@jupyterlab/coreutils';
22

33
import { ServerConnection } from '@jupyterlab/services';
44

5+
// Ref: https://github.com/jupyterlab/extension-examples/blob/main/server-extension/src/handler.ts
6+
57
/**
68
* Call the API extension
79
*
@@ -15,11 +17,7 @@ export async function requestAPI<T>(
1517
): Promise<T> {
1618
// Make request to Jupyter API
1719
const settings = ServerConnection.makeSettings();
18-
const requestUrl = URLExt.join(
19-
settings.baseUrl,
20-
'jupyterlab-snippets', // API Namespace
21-
endPoint
22-
);
20+
const requestUrl = URLExt.join(settings.baseUrl, 'snippets', endPoint);
2321

2422
let response: Response;
2523
try {

src/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace CommandIDs {
3434
type Tree = Map<string, Tree>;
3535

3636
/**
37-
* Convert the list of snippets a tree.
37+
* Convert the list of snippets to a tree.
3838
* @param snippets The list of snippets.
3939
*/
4040
function toTree(snippets: Snippet[]) {
@@ -45,7 +45,7 @@ function toTree(snippets: Snippet[]) {
4545
if (!node.has(part)) {
4646
node.set(part, new Map<string, Tree>());
4747
}
48-
node = node.get(part);
48+
node = node.get(part)!;
4949
});
5050
});
5151
return tree;
@@ -92,6 +92,8 @@ const extension: JupyterFrontEndPlugin<void> = {
9292
menu: IMainMenu | null,
9393
notebookTracker: INotebookTracker | null
9494
) => {
95+
console.log('JupyterLab extension jupyterlab-snippets is activated!');
96+
9597
const { commands } = app;
9698

9799
const isEnabled = () => {
@@ -122,11 +124,11 @@ const extension: JupyterFrontEndPlugin<void> = {
122124
return;
123125
}
124126

125-
const current = notebookTracker.currentWidget;
126-
const notebook = current.content;
127+
const current = notebookTracker!.currentWidget;
128+
const notebook = current!.content;
127129
NotebookActions.insertBelow(notebook);
128130
const activeCell = notebook.activeCell;
129-
activeCell.model.value.text = content;
131+
activeCell!.model.sharedModel.setSource(content);
130132
},
131133
isEnabled
132134
});

src/snippets.ts

+1-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { URLExt } from '@jupyterlab/coreutils';
2-
3-
import { ServerConnection } from '@jupyterlab/services';
1+
import { requestAPI } from './handler';
42

53
/**
64
* The type for a Snippet.
@@ -32,33 +30,3 @@ export async function fetchSnippet(snippet: Snippet): Promise<ISnippetContent> {
3230
};
3331
return requestAPI<ISnippetContent>('get', request);
3432
}
35-
36-
/**
37-
* Call the API extension
38-
*
39-
* @param endPoint API REST end point for the extension
40-
* @param init Initial values for the request
41-
* @returns The response body interpreted as JSON
42-
*/
43-
async function requestAPI<T>(
44-
endPoint = '',
45-
init: RequestInit = {}
46-
): Promise<T> {
47-
const settings = ServerConnection.makeSettings();
48-
const requestUrl = URLExt.join(settings.baseUrl, 'snippets', endPoint);
49-
50-
let response: Response;
51-
try {
52-
response = await ServerConnection.makeRequest(requestUrl, init, settings);
53-
} catch (error) {
54-
throw new ServerConnection.NetworkError(error);
55-
}
56-
57-
const data = await response.json();
58-
59-
if (!response.ok) {
60-
throw new ServerConnection.ResponseError(response, data.message);
61-
}
62-
63-
return data;
64-
}

style/base.css

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
See the JupyterLab Developer Guide for useful CSS Patterns:
3+
4+
https://jupyterlab.readthedocs.io/en/stable/developer/css.html
5+
*/

0 commit comments

Comments
 (0)