Command line scripting with SlicerDockers

Hi all,

A question regarding executing command line scripts while using SlicerDockers:

  • I am using GitHub - pieper/SlicerDockers: docker config files for slicer, everything works succesfully, I am able to access Slicer interface via the hosted vnc client.
  • If possible I would like to send command line requests to the running slicer instance, similar to Slicer.exe --python-code "slicer.util.loadScene('f:/2013-08-23-Scene.mrb')" or Slicer.exe --python-code "doSomething; doSomethingElse; etc." --testing --no-splash --no-main-window
  • Ultimately I am trying to figure out a way to create a way to open a slicer window from a custom web application, by creating a button which would execute a command, which opens a locally stored nifti file with the running slicer instance (and visible in the vnc window). I know that this exists - New DICOMweb features: launch Slicer from web browser and download/upload data sets to the cloud using DICOMweb but in my case, all the files can be stored in the local storage, no communication with external DICOMWeb databases is necessary.

Is it possible with SlicerDockers? Or is there a better way to achieve similar functionality?

Thank you in advance.

Yes, you can send commands to a running Slicer with the WebServer module and the slicer/exec endpoint. You can use the --python-code command line option to make sure the WebServer is running at startup. The exec endpoint allows you to execute arbitrary python code and get the results via http (of course be careful who has access to this endpoint!). You would also need to expose the port from your docker run command, 2016 by default. If you have shared the volume with the files to the docker instance then you can load the files using the passed in commands and have them show up in your vnc window.

1 Like

Thank you for your answer!
I not yet sure how can I launch the WebServer using GitHub - pieper/SlicerDockers: docker config files for slicer .
WebServer module does not seem to be available in the Slicer instance hosted in the vnc. Is there a way to enable it either after start or when starting the docker container/inside the docker container?

I managed to add the WebServer module using GitHub - pieper/SlicerWeb: Slicer modules that support web services and web applications and adding the --env SLICER_ARGUMENTS="--additional-module-paths /home/researcher/Documents/SlicerWeb/WebServer" argument to the docker run command. However this way does seem to be limited in functionality, as I can only open the http://localhost:2016/ (by exposing the port) but exec commands does not seem to be available (returns unknown command "/exec") probably because I do not see the option to start server in the Web Server module started in this way.

Slicer-5.0.3 has the Web Server module built in, so there is no need to set up anything else. You can start Slicer (with the exec interface enabled) from the command line like this:

"%localappdata%\NA-MIC\Slicer 5.0.3\Slicer.exe" --python-code "wslogic=getModuleLogic('WebServer'); wslogic.addDefaultRequestHandlers(enableExec=True); wslogic.start()"

Right, but for my usecase, I need the Slicer to be running in a docker container and accessible via vnc client, so I am using GitHub - pieper/SlicerDockers: docker config files for slicer . However, SlicerDockers seems to run the Slicer version 5.0.2 version, so thats probably why the Web Server module in not available in the launched Slicer instance. Maybe I can try to manually rebuild the docker with Slicer version 5.0.3 or are there are better alternatives?

The web server in 5.0.3 is refactored and improved, so I would recommend it over the experimental version that was developed in a separate extension.

All you need to do is to change the version to 5.0.3 and rebuild the image. Maybe @pieper could do it and upload the new image to dockerhub.

That would certainly be excellent :+1: Otherwise, I will try to rebuild the image on my end, thanks

@NaglisR did you run stevepieper/slicer:5.0.2 ? that should have the WebServer option built in. I haven’t had a chance to update to 5.0.3 yet but 5.0.2 should work the same mostly. Let us know how it goes.

It seems to me that 5.0.2 does not yet have the inbuilt Web Server module, checked that by downloading .
I am trying to rebuild the SlicerDockers image after changing the version SlicerDockers/ at master · pieper/SlicerDockers · GitHub and the download link SlicerDockers/Dockerfile at 5949f71fab141740ba0da7c03af3825cc9b91798 · pieper/SlicerDockers · GitHub
However, I am not yet able to rebuilt the image, as it seems some minor adjustments have to be made elsewhere (probably names of some atributes have to be updated?).

Step 7/7 : RUN for ext in ${SLICER_EXTS} ; do echo "Installing ${ext}" ;   EXTENSION_TO_INSTALL=${ext}     su researcher -c "xvfb-run --auto-servernum       /opt/slicer/Slicer --python-script /tmp/" ; done
 ---> Running in 975b22429285
Installing MarkupsToModel
QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-researcher'
Switch to module:  "Welcome"
installing MarkupsToModel
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "<string>", line 7, in <module>
AttributeError: qSlicerExtensionsManagerModel has no attribute named 'retrieveExtensionMetadataByName'

Thanks testing this - I was mistaken, the 5.0.2 version didn’t have the WebServer module as you discovered. I’m looking into updating the images to include a 5.0.3 version. I’ll report back if I get one uploaded. We should be able to fix the extensions (slicer-plus image) but the base slicer image wouldn’t need the extension manager so you can probably run that even if the build script doesn’t complete.

If you want to experiment without fixing the docker, you can also just copy the WebServer code directly from the Slicer source code into the module path of the 5.0.2 instance. There’s no compilation needed and I don’t think there’s even any configuration done during the cmake process, just copying the files.

It turned out to be easy enough to update the base image, so now this should work to run 5.0.3 with the WebServer pre-installed.

docker run -d -p 8080:8080 --name slicer stevepieper/slicer:5.0.3

Yes, pulled the stevepieper/slicer:5.0.3, everything seems to be working now, thank you!

1 Like

Great! Your use case with a custom web app and containerized Slicer access sounds pretty interesting. If you can share your app source code, or just a small demo script, that would be good for others to learn from.

FYI, here is an extension install code snippet that is compatible with all Slicer versions (including the latest Slicer-5.0.3):

In Slicer-5.1 and later the installation got simpler, see in the script repository.