1+ import os
12from jupyterlab_commands_toolkit .tools import execute_command
23
4+ from .utils import get_serverapp
5+
36
47async def open_file (file_path : str ):
58 """
69 Opens a file in JupyterLab main area
10+
11+ Args:
12+ file_path: Path to the file relative to jupyter root
13+
14+ Returns:
15+ dict: A dictionary containing the response from JupyterLab
16+ The structure includes:
17+ - success (bool): Whether the command executed successfully
18+ - result (any): The return value after opening the file
19+ - error (str, optional): Error message if the command failed
20+
21+ Raises:
22+ asyncio.TimeoutError: If the frontend doesn't respond within the timeout period
23+
724 """
25+ # If file_path is absolute, convert it to a path relative to the jupyter root
26+ if os .path .isabs (file_path ):
27+ try :
28+ serverapp = get_serverapp ()
29+ root_dir = serverapp .root_dir
30+ except Exception :
31+ # Fallback to current working directory if server app is not available
32+ root_dir = os .getcwd ()
33+
34+ try :
35+ # Convert absolute path to relative path from jupyter root
36+ file_path = os .path .relpath (file_path , root_dir )
37+ except ValueError :
38+ # If paths are on different drives (Windows), use the absolute path as is
39+ # JupyterLab may still be able to handle it
40+ pass
41+
842 return await execute_command ("docmanager:open" , {"path" : file_path })
943
1044
@@ -15,11 +49,36 @@ async def run_all_cells():
1549 return await execute_command ("notebook:run-all-cells" )
1650
1751
52+ async def get_active_cell_index () -> int :
53+ pass
54+
55+ async def run_active_cell ():
56+ """Runs the currently selected/active cell"""
57+
58+ return await execute_command ("notebook:run-cell" )
59+
60+ async def select_cell_below ():
61+ """
62+ Moves the cursor down to select the cell below the currently selected cell
63+ """
64+ return await execute_command ("notebook:move-cursor-down" )
65+
66+ async def select_cell_above ():
67+ """
68+ Moves the cursor up to select the cell above the currently selected cell
69+ """
70+ return await execute_command ("notebook:move-cursor-up" )
71+
1872async def restart_kernel ():
1973 """
2074 Restarts the notebook kernel, useful when new packages are installed
2175 """
2276 return await execute_command ("notebook:restart-kernel" )
2377
2478
25- toolkit = [open_file , run_all_cells , restart_kernel ]
79+ toolkit = [
80+ open_file ,
81+ run_active_cell ,
82+ run_all_cells ,
83+ restart_kernel
84+ ]
0 commit comments