# Use existing slicer modules with python and cli

**URL:** https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743
**Category:** Support
**Tags:** brainsfit, python, cli
**Created:** [May 1, 2018, 11:48am UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743 "2018-05-01T11:48:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hadasara](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@hadasara](https://discourse.slicer.org/u/hadasara)
#### Post date: [May 1, 2018, 11:48am UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743/1 "2018-05-01T11:48:43Z")

</div>

Hi,  
I followed this tutorial on how to use existing modules in slicer through cli library:  
https://www.youtube.com/embed/lK_p954iKZM?feature=oembed&wmode=opaque&list=PLJWCUXz3GeAfmYLiFcKus_c0jcsMnVsgb

It sounds not bad (considering my understanding in cli is close to 0…)  
Anyway, since I want to use registration module (such as BRAINSfit) I’d like to have the option to cancel the operation after running it. but the cli cancel command is not implemented yet in the cli.py file that I’ve found.

so my questions:

1. Is using slicer.cli is a good way execute existing modules proggramatically? If not- what is?
2. How can I cancel the running process with cli?

Thanks!

Here is part of the code- the apply btton, executing the add Scalar Volume module (but can’t stop in the middle):

```
   def onApplyButton(self):

        # allow cancel the process
        if self.InProgress:
            self.InProgress = False
            self.abortRequested = True
            raise ValueError("User requested cancel.")
            # self.cliNode.cancel() # not implemented
            self.applyutton.text = "Cancelling..."
            self.applyButton.enabled = False
            return

        self.InProgress = True
        self.applyButton.text = "Cancel"

        fixedImage = self.fixedSelector.currentNode()
        movingImage = self.moveImgSelector.currentNode()
        outputImage = self.outputSelector.currentNode()

        if self.registratoinMethod == 'addScalarVolumes':
          moduleVar = slicer.modules.addscalarvolumes
          parameters = {}
          parameters["inputVolume1"] = fixedImage.GetID()
          parameters["inputVolume2"] = movingImage.GetID()
          parameters["outputVolume"] = outputImage.GetID()
          parameters["interpolation order"] = 0

        # get the module as object
        slicer.app.processEvents()
        self.cliNode = slicer.cli.run(moduleVar,None, parameters)
        print self.cliNode.cancel.
        self.bar.setCommandLineModuleNode(self.cliNode)

    # logic.run(self.fixedSelector.currentNode(),
        # self.outputSelector.currentNode())
        # logic.run(fixedImage,movingImage,outputImage)

        # make the output volume appear in all the slice views
        slicer.util.setSliceViewerLayers(
            background=outputImage)
```

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [May 1, 2018, 1:18pm UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743/2 "2018-05-01T13:18:09Z")

</div>

1. Yes

2. Did you try calling `self.cliNode.Cancel()`? (that should probably be added to cli.py)

---

<div class="post-metadata">

### Author: ![hadasara](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@hadasara](https://discourse.slicer.org/u/hadasara)
#### Post date: [May 1, 2018, 2:40pm UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743/3 "2018-05-01T14:40:32Z")

</div>

> [@pieper](#):
>
> self.cliNode.Cancel()

Hi, thanks! followed your suggestion- I did, but it didn’t work.  
begginner question- maybe there is a way to access the cancel of the module itself? (the cancel button exist in some modules)

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [May 1, 2018, 3:30pm UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743/4 "2018-05-01T15:30:20Z")

</div>

Are you sure it didn’t work? In your example code you have `cancel` but the method is `Cancel` with a capital `C`. Calling `Cancel` should be the same as clicking the Cancel button on CLI interface widget.

---

<div class="post-metadata">

### Author: ![hadasara](https://avatars.discourse-cdn.com/v4/letter/h/c67d28/32.png) [@hadasara](https://discourse.slicer.org/u/hadasara)
#### Post date: [May 1, 2018, 3:43pm UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743/5 "2018-05-01T15:43:44Z")

</div>

Thanks!! This fixed the problem!!  
I changed it to:

if self.InProgress:  
self.InProgress = False  
self.abortRequested = True  
self.cliNode.Cancel()  
self.applyButton.text = “Cancelling…”  
self.applyButton.enabled = False  
return  
now it cancel the progress.

Could you help me with another question?

1. Is there a way to make something happen after the process is over? like change the name of the botton after it finishes.

2. How can I get through the cli the log, that is ussualy apear in the consol window while running the algorithm with the original loadable module?

I couldn’t find an explanation here:  
[https://www.slicer.org/wiki/Documentation/4.1/Modules/BRAINSFit](https://www.slicer.org/wiki/Documentation/4.1/Modules/BRAINSFit)

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [May 1, 2018, 4:36pm UTC](https://discourse.slicer.org/t/use-existing-slicer-modules-with-python-and-cli/2743/6 "2018-05-01T16:36:02Z")

</div>

Great - glad that worked.

If you observe events from the cliNode you can tell what state the process is in and react accordingly. You can review this documentation with the printStatus example:

[https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python\_scripting#Running\_a\_CLI\_from\_Python](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Running_a_CLI_from_Python)

Also to get the log info you can call `cliNode.GetOutputText()` and `cliNode.GetErrorText()`

(I just updated the wiki with this info).
