Extension for 3D Slicer that allows the application to be used from Jupyter notebook
Demo video: https://youtu.be/oZ3_cRXX2QM
You can use this option for a quick start. No installation or setup is needed, just click the link below and start using Slicer via Jupyter notebook in your web browser.
When you click on the link, Binder launches 3D Slicer with SlicerJupyter extension on their cloud servers. Binder is a free service and server resources are quite limited. Also, there is no interactive access to the graphical user interface. Therefore, this option is only recommended for testing, demos, or simple computations or visualizations.
SlicerJupyter
extension in Extension Manager (in the application menu choose View/Extension Manager, click Install button of SlicerJupyter, wait for the installation to complete, and click Restart
)JupyterKernel
module (open the module finder by click the "Search" icon on the toolbar, or hitting Ctrl/Cmd-F, then type its name)Start Jupyter server
buttonFollow all the installation steps above (start Jupyter server once with the default JupyterLab user interface), then restart Slicer. After this, a classic notebook server can be started by typing this into the Python console in Slicer:
slicer.util._executePythonModule('notebook',['--notebook-dir', 'some/path/to/workspace'])
Slicer's Python kernel can be used in Jupyter servers in external Python environments. Kernel specification installation command is displayed in Jupyter server in external Python environment
section in JupyterKernel
module.
You need to install and set up these Python packages: jupyter jupyterlab ipywidgets pandas ipyevents ipycanvas
.
import JupyterNotebooksLib as slicernb
slicernb.ViewDisplay()
slicernb.ViewInteractiveWidget()
Tab
key for auto-completeShift
+Tab
for showing documentation for a method (hit multiple times to show more details). Note: method name must be complete (you can use Tab
key to complete the name) and the cursor must be inside the name or right after it (not in the parentheses). For example, type slicer.util.getNode
and hit Shift
+Tab
.You may see warning messages about upgrading pip, such as this:
WARNING: You are using pip version 20.1.1; however, version 20.3.3 is available.
You should consider upgrading via the '/Applications/Slicer.app/Contents/bin/./python-real -m pip install --upgrade pip' command.
In general, it is not necessary to upgrade pip, so you can ignore this warning. If you do want to upgrade it then you need to use the Slicer's "Python launcher" (instead of python-real). Slicer's Python launcher is called PythonSlicer
and it sets up Slicer's virtual Python environment so that the real Python executable (python-real) can run correctly.
You may get warning about installing scripts in folder that is not on PATH:
WARNING: The script pyjson5 is installed in ‘/Applications/Slicer.app/Contents/lib/Python/bin’ which is not on PATH.
This warning is displayed to warn you that the installed script will not run by simply typing its name anywhere in a terminal. This can be safely ignored.
If a Jupyter server is kept running then it will automatically restart all kernel instances (Slicer applications) that it manages. If the browser window is accidentally closed before shutting down the server, then you can get the address of all running servers by typing the following into any Slicer Python console:
slicer.util._executePythonModule('jupyter', ['notebook', 'list'])
Open the address in a web browser and click "Quit" button to shutdown the server.
You can get started by looking at example Slicer notebooks here.
Slicer_DIR
CMake variable to the .../Slicer-build
Example:
jupyter-kernelspec install /tmp/SlicerJupyter-build/inner-build/share/Slicer-4.13/qt-loadable-modules/JupyterKernel/Slicer-4.13/ --replace --user
Type this into Slicer's Python console to manually start a kernel that a notebook can connect to:
connection_file=r'C:\Users\andra\AppData\Roaming\jupyter\runtime\kernel-3100f53f-3433-40f9-8978-c72ed8f88515.json'
print('Jupyter connection file: ['+connection_file+']')
slicer.modules.jupyterkernel.startKernel(connection_file)
Path of connection_file
is printed on jupyter notebook's terminal window.
These commands must be the last commands in a cell.
__kernel_debug_enable()
: enable detailed logging of all incoming Jupyter requests__kernel_debug_disable()
: enable detailed logging of all incoming Jupyter requests