Skip to content

Commit 10ff2d9

Browse files
committed
Test version
1 parent d970cc0 commit 10ff2d9

File tree

6 files changed

+11750
-5
lines changed

6 files changed

+11750
-5
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ The `jlpm` command is JupyterLab's pinned version of
3434
[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
3535
`yarn` or `npm` in lieu of `jlpm` below.
3636

37+
#### Using uv (recommended)
38+
39+
```bash
40+
# Clone the repo to your local environment
41+
# Change directory to the nbname directory
42+
# Install JupyterLab and development dependencies
43+
uv add --dev "jupyterlab>=4.0.0,<5"
44+
# Install package in development mode
45+
uv run pip install -e "."
46+
# Link your development version of the extension with JupyterLab
47+
uv run jupyter labextension develop . --overwrite
48+
# Install JavaScript dependencies and build
49+
jlpm install
50+
jlpm build
51+
```
52+
53+
#### Using pip (alternative)
54+
3755
```bash
3856
# Clone the repo to your local environment
3957
# Change directory to the nbname directory
@@ -50,7 +68,9 @@ You can watch the source directory and run JupyterLab at the same time in differ
5068
```bash
5169
# Watch the source directory in one terminal, automatically rebuilding when needed
5270
jlpm watch
53-
# Run JupyterLab in another terminal
71+
# Run JupyterLab in another terminal (using uv if available)
72+
uv run jupyter lab
73+
# Or alternatively
5474
jupyter lab
5575
```
5676

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
"watch:labextension": "jupyter labextension watch ."
5757
},
5858
"dependencies": {
59-
"@jupyterlab/application": "^4.0.0"
59+
"@jupyterlab/application": "^4.0.0",
60+
"@jupyterlab/docregistry": "^4.0.0",
61+
"@jupyterlab/notebook": "^4.0.0",
62+
"@jupyterlab/services": "^7.0.0"
6063
},
6164
"devDependencies": {
6265
"@jupyterlab/builder": "^4.0.0",

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,8 @@ before-build-python = ["jlpm clean:all"]
7575

7676
[tool.check-wheel-contents]
7777
ignore = ["W002"]
78+
79+
[dependency-groups]
80+
dev = [
81+
"jupyterlab>=4.0.0,<5",
82+
]

src/index.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,71 @@ import {
22
JupyterFrontEnd,
33
JupyterFrontEndPlugin
44
} from '@jupyterlab/application';
5+
import { INotebookTracker } from '@jupyterlab/notebook';
56

67
/**
78
* Initialization data for the nbname extension.
89
*/
910
const plugin: JupyterFrontEndPlugin<void> = {
1011
id: 'nbname:plugin',
11-
description: 'A JupyterLab extension to retrieve the currently openend notebook filename.',
12+
description:
13+
'A JupyterLab extension to retrieve the currently openend notebook filename.',
1214
autoStart: true,
13-
activate: (app: JupyterFrontEnd) => {
14-
console.log('JupyterLab extension nbname is activated!');
15+
requires: [INotebookTracker],
16+
activate: (app: JupyterFrontEnd, tracker: INotebookTracker) => {
17+
// Function to inject the notebook name
18+
const injectNotebookName = (notebookPanel: any) => {
19+
if (!notebookPanel) {
20+
return;
21+
}
22+
const fullPath = notebookPanel.context.path;
23+
const filename = fullPath.split('/').pop();
24+
console.log(`Current notebook filename: ${filename}`);
25+
26+
// Get Kernel
27+
const sessionContext = notebookPanel.sessionContext;
28+
if (!sessionContext?.session?.kernel) {
29+
console.warn('No kernel available!');
30+
return;
31+
}
32+
33+
const kernel = sessionContext.session.kernel;
34+
35+
// Inject the filename
36+
const code = `__NOTEBOOK_FILE__ = "${filename}"`;
37+
const future = kernel.requestExecute({ code, silent: true });
38+
future.done
39+
.then(() => {
40+
console.log(
41+
`Successfully injected __NOTEBOOK_FILE__ = "${filename}"`
42+
);
43+
})
44+
.catch((error: Error) => {
45+
console.error('Failed to inject code:', error);
46+
});
47+
};
48+
49+
// Inject notebook name
50+
tracker.currentChanged.connect((tracker, notebookPanel) => {
51+
if (notebookPanel) {
52+
notebookPanel.sessionContext.ready.then(() => {
53+
injectNotebookName(notebookPanel);
54+
});
55+
56+
notebookPanel.context.pathChanged.connect(() => {
57+
injectNotebookName(notebookPanel);
58+
});
59+
}
60+
});
61+
62+
// Inject filename for currently opened notebook
63+
if (tracker.currentWidget) {
64+
tracker.currentWidget.sessionContext.ready.then(() => {
65+
injectNotebookName(tracker.currentWidget);
66+
});
67+
}
68+
69+
console.log('Notebook filename injection is now active');
1570
}
1671
};
1772

0 commit comments

Comments
 (0)