Private Extension my own python code

Operating system: window 11
Slicer version: 4.13.0

Hello,
I have two questions regarding ‘private extension’.

Q1. Is it correct that the extension created through the extension wizard works locally by default and becomes public only when it is submitted to Slicer’s index(?)

  • Create an extension file through ‘create extension’ of the extension wizard. ( I got it )

  • Add Module to Extention

Q2) What is ‘Add Module to Extension’ ?
Since a Python code file (.py) is usually called a module, I thought it was to add the function file I created in this part. However, I think I am wrong. Where should I add the source code files that I have implemented and other files necessary for source code operation?

Q1. Is it correct that the extension created through the extension wizard works locally by default and becomes public only when it is submitted to Slicer’s index(?)

That is correct. The Extension Wizard module will create the local files that make up an extension. Once you have developed your extension, you need to make a PR to the ExtensionsIndex (GitHub - Slicer/ExtensionsIndex: Slicer extensions index).

Q2) What is ‘Add Module to Extension’ ?
Since a Python code file (.py) is usually called a module, I thought it was to add the function file I created in this part. However, I think I am wrong. Where should I add the source code files that I have implemented and other files necessary for source code operation?

An extension is made up of one or more modules (there are also different types of modules). You need to add at least one module for your extension to work, so in your case, you would probably want to add a Python module. This will generate the structure of a basic module, including a .py file where you can move your code.

It is worth mentioning that 3D slicer will ask you whether the path to your module should be added to the list of modules that Slicer will load. Doing it so, will have your module loaded in your current installation/build of 3D Slicer.

image

I tried the method you told me before asking the question.

But I have qt module problems (I have included a key board shortcut custom function in my code.)

In my opinion, it seems to be a problem that occurs because the act of loading my custom module precedes the loading of the qt module. Should I do add Pyqt5 in my codes?

What is exactly the error that you get? Which version of Slicer are you
using?

I’ve just created a new extension with a scripted module (all defaults)
and it works flawlessly (3D Slicer git main branch).

Note that “Slicer module” is a separate concept from “Python module”
https://slicer.readthedocs.io/en/latest/developer_guide/module_overview.html

As I wrote above, I am using slicer -4.13.0-2021-08-24 version.

My code contains code to customize the ‘short cut’ of the 3d slicer.

If you delete the ‘short cut’ custom part and proceed with extension, the following error occurs.

If you don’t remove the ‘short cut’ part, you will get an error like below.
image

The order in which I proceeded with the extension is as follows.

step 1) extension wizard - create extension - name( test )

image

image

step 2) Add module to Extension (test_my_code)

image

step 3) Add my custom code into test_my_code folder

===============
I thought adding the module in step 2) was adding the code file I created.
However, when I click ‘add module to extension’, the screen that appears is as follows.

image
I think my understanding of this part is wrong, but since the Python code file itself is called a module, shouldn’t I be able to add the code I created in step 2?

Hello,

I would recommend you to switch to Slicer 5 (probably nothing to do with this issue, though).

Using Slicer 5, through the ExtensionWizard I got this structure:

/tmp/TestExtension/
├── CMakeLists.txt
├── TestExtension.png
└── TestModule
    ├── CMakeLists.txt
    ├── __pycache__
    │   └── TestModule.cpython-39.pyc
    ├── Resources
    │   ├── Icons
    │   │   └── TestModule.png
    │   └── UI
    │       └── TestModule.ui
    ├── Testing
    │   ├── CMakeLists.txt
    │   └── Python
    │       └── CMakeLists.txt
    └── TestModule.py

I added the following path to the Slicer module paths: /tmp/TestExtension/TestModule.

Maybe as a starting point use only the files generated by the extensions wizard.

You could verify that you have similar structure and your modules path
looks similar.

Hello again

Unfortunately, since the slicer version has been unified( in my company), it cannot be changed.
There is no problem with this configuration in my version either.
The current problem situation is that the problem occurs from the moment the custom code is added.
I temporarily solved the problem by replacing it with a method of adding code through the ‘Ctrl+r’ short cut key, but I’m still curious about the sure method for the extension.

The above structure is automatically formed after the ‘Create Extension’ and ‘Add module to entension’ processes.
If so, where is your custom code included in the structure above?
In my slicer, I included my custom code directly in the directory after the above structure was created automatically.

like this

/tmp/TestExtension/
├── CMakeLists.txt
├── TestExtension.png
└── TestModule
    ├── CMakeLists.txt
    ├── __pycache__
    │   └── TestModule.cpython-39.pyc
    ├── Resources
    │   ├── Icons
    │   │   └── TestModule.png
    │   └── UI
    │       └── TestModule.ui
    ├── Testing
    │   ├── CMakeLists.txt
    │   └── Python
    │       └── CMakeLists.txt
    └── TestModule.py
    │
    └── my_custom_code.py (new !)

The part I found odd in the previous question is the ‘TestModule’ part.
This part is the output of the ‘Add module to extension’ process, I thought I was adding my custom code here, but it actually seemed to create a dummy folder.
Is it correct way to add custom code additions directly under these dummy modules?

Place your custom code in the template/example Slicer scripted loadable module that is TestModule.py. You must maintain the template structure contained in TestModule.py. It will not work to copy all code in my_custom_code.py and replace everything in TestModule.py.

1 Like

@jamesobutler
What you mean is not to add custom code inside TestModule, but to copy and add my CustomCode inside the TestModule.py code while keeping the contents of the existing TestModule.py?

TestModule.py is an example/template of a Slicer scripted loadable module that does some example functionality. You will replace that with your custom code, however you need to keep the framework of the classes in place (ScriptedLoadableModule/ScriptedLoadableModuleWidget/ScriptedLoadableModuleLogic). The framework makes sure the Slicer scripted loadable module is setup correctly upon starting Slicer.

@jamesobutler

As you said, I think I can just copy-paste my custom code into the generated code file, but it’s frustrating because there are so many unknown errors. you’re driving me crazy 3D slicer~~~

edit :

I often use argparser in code, but I don’t know why it doesn’t work, so I just changed it to a global variable and solved it. but it doesn’t seem clean

The error you show looks like pure python issues rather than Slicer. Does your code take command line input arguments or is it built with GUI input/selection in mind? Are you creating a Slicer scripted module or a Slicer scripted CLI module? See module types at Module Overview — 3D Slicer documentation. A scripted CLI is an option in the extension wizard of the latest stable Slicer.

The problem didn’t occur in all cases except when pasting the custom code into the extension module file. I don’t think it’s a code problem. I’ll have to keep thinking about why argparser only gives an error in this case.