Jupyterlab fix disguised as extension to fix action 'jump to definition' from notebook to the source file / definition within the context of the notebook's kernel. Without the fix package would need to be installed in the same environment as jupyterlab to allo jump to definition
Project description
jupyterlab_jump_to_definition_fix
JupyterLab extension that fixes "jump to definition" functionality for notebooks using Jedi static analysis in the kernel environment. This allows jumping to source code definitions for packages installed in the notebook's kernel, even if those packages are not installed in JupyterLab's own environment.
The Problem
JupyterLab's built-in LSP "jump to definition" only works for packages installed in the same Python environment as JupyterLab itself. Notebooks often run with kernels in different environments (conda envs, virtual envs, containers) where packages are actually installed, making the stock LSP functionality useless for kernel-installed packages.
The Fix
This extension replaces the stock LSP "jump to definition" command for Python notebooks with a Jedi-based implementation that runs in the kernel's Python environment.
How it works:
- Executes Jedi static analysis directly in the notebook's kernel
- Uses kernel's
sys.pathfor module resolution, finding packages installed in kernel environment - Analyzes all notebook cells as concatenated source to understand full context
- Converts absolute filesystem paths to JupyterLab-relative paths for file opening
- Seamlessly overrides stock LSP command - same keyboard shortcut, same menu entry
Implementation details:
- Frontend: Collects all code cell sources, calculates cursor position across cells, sends to kernel
- Backend: Provides Jedi introspection code template executed in kernel
- Jedi: Runs
Script.goto()withfollow_imports=Trueusing kernel's module search paths - Path resolution: Calculates server root from kernel CWD and notebook path
Fix Implementation Details
The extension consists of frontend TypeScript code and backend Python code working together to provide kernel-aware jump-to-definition.
Frontend Implementation (src/index.ts):
- Command Registration:
app.commands.addCommand(commandId)createsnotebook:jump-to-definition-kernelcommand that collects notebook context and executes Jedi analysis - Cell Source Collection: Command execute function iterates through
notebook.content.widgets, concatenates code cell sources, and calculates absolute cursor position accounting for multi-cell structure. Jedi requires 1-based line numbers - Kernel Execution:
kernel.requestExecute()sends Jedi introspection code to kernel,future.onIOPubhandler captures stdout (JSON result) while filtering stderr (debug logs) - Path Conversion: Secondary kernel execution gets CWD via
os.getcwd(), calculates JupyterLab server root from notebook path, converts absolute filesystem paths to server-relative paths fordocManager.openOrReveal() - Stock LSP Override:
overrideLSPCommand()function dynamically interceptslsp:jump-to-definitioncommand when it loads, preserves original icon and label, routes Python notebooks to Jedi implementation while delegating others to stock LSP
Backend Implementation (jupyterlab_jump_to_definition_fix/routes.py):
- Introspection Code Template:
IntrospectionCodeHandler.get()method provides Python code template executed in kernel environment that imports Jedi, createsjedi.Projectwith kernel'ssys.path, runsScript.goto()withfollow_imports=True, and returns JSON with file path and line number - API Handler: Route registered at
/jupyterlab_jump_to_definition_fix/introspection-codeendpoint serves template to frontend
Key Implementation Components:
jedi.Project(path=notebook_path, sys_path=sys.path): Creates Jedi project using kernel's module search paths for accurate resolutionjedi.Script(...).goto(follow_imports=True): Performs static analysis to find symbol definitions, following import chainskernel.requestExecute(): Executes Jedi code in kernel's Python environment where target packages are installed- IOPub message filtering: Separates stdout (JSON result) from stderr (debug output) for clean parsing
- Path calculation:
kernelCwd.endsWith(notebookDir)logic strips server root from absolute paths
Features
- Replaces stock LSP: Overrides
lsp:jump-to-definitioncommand for Python notebooks - Kernel-aware: Uses kernel's Python environment and
sys.pathfor module resolution - Static analysis: Jedi finds definitions without requiring code execution
- Same UX: Identical keyboard shortcut (
Ctrl+B/Cmd+B) and menu entries as stock LSP - Automatic fallback: Delegates to stock LSP for non-Python notebooks
Usage
- Open a Jupyter notebook with a Python kernel
- Place your cursor on or select a symbol (function name, class name, module attribute, etc.)
- Press
Ctrl+B(orCmd+Bon Mac), or run "Jump to Definition (Kernel Context)" from the command palette - The source file will open at the definition location
Examples of symbols you can jump to:
- Module:
numpyorpandas - Function:
np.array,pd.DataFrame - Method:
MyClass.my_method - Nested attributes:
sklearn.ensemble.RandomForestClassifier
This extension is composed of a Python package named jupyterlab_jump_to_definition_fix
for the server extension and a NPM package named jupyterlab_jump_to_definition_fix
for the frontend extension.
Requirements
- JupyterLab >= 4.0.0
- Python kernel (IPython/ipykernel)
Install
To install the extension, execute:
pip install jupyterlab_jump_to_definition_fix
Uninstall
To remove the extension, execute:
pip uninstall jupyterlab_jump_to_definition_fix
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jupyterlab_jump_to_definition_fix-1.0.62.tar.gz.
File metadata
- Download URL: jupyterlab_jump_to_definition_fix-1.0.62.tar.gz
- Upload date:
- Size: 207.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a5302aa28e424cfe0f166e30c761ac798303b7201f0a82478b6468bdcad11a3
|
|
| MD5 |
f0ea48448805797ce6597aaaac61f4c9
|
|
| BLAKE2b-256 |
7db898b33f9e4952c9de01cfab3576898b2a0a71ed5041b40630c6e523fc8fa0
|
File details
Details for the file jupyterlab_jump_to_definition_fix-1.0.62-py3-none-any.whl.
File metadata
- Download URL: jupyterlab_jump_to_definition_fix-1.0.62-py3-none-any.whl
- Upload date:
- Size: 22.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
988a47025e1a4a5c84317bd56fdb2b432d4e32767931dc87f7630992d5b0c827
|
|
| MD5 |
462170e0f5679d37d03ccafa48a7a049
|
|
| BLAKE2b-256 |
4100ccdc0cdf601cf1ef9155c46f0de16517c8cbf182b2c98d4a12f9640978e9
|