Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Splitting code in subfolder #40

Open
laurentletg opened this issue Jul 12, 2024 · 3 comments · May be fixed by #56
Open

Splitting code in subfolder #40

laurentletg opened this issue Jul 12, 2024 · 3 comments · May be fixed by #56
Assignees
Labels
priority high anything within the big picture, not nit-picky

Comments

@laurentletg
Copy link
Collaborator

Is your feature request related to a problem? Please describe.
The main script is too long.

Describe the solution you'd like
While the ideal solution would be to split the code in subfolders/modules, my attempts to make it work in Slicer always end up with ModuleNotFoundError. This is usually easy to perform in standard Python scripts but not through Slicer. There is a post about this here but I was not able to make it work even after updating the path like so:

script_directory = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(script_directory, 'name_of_module'))
@AcastaPaloma AcastaPaloma linked a pull request Sep 1, 2024 that will close this issue
@maxradx
Copy link
Member

maxradx commented Sep 24, 2024

I have asked the 3D Slicer forum. Here is one answer: https://discourse.slicer.org/t/slicer-development-code-fragmentation/38516/3

  • To test!

@AcastaPaloma AcastaPaloma added the priority high anything within the big picture, not nit-picky label Oct 20, 2024
@maxradx maxradx self-assigned this Oct 23, 2024
@maxradx
Copy link
Member

maxradx commented Oct 24, 2024

Hi,

Issue 40 will be solved soon in @AcastaPaloma pull requests. In addition, we found and tested how to use/split the code in different files in a subFolder.

Example of organization:

image

On the same level than the file SlicerCART.py (main script file), an additional folder is created (ie, subfolder), which contains one or multiple python files that have content to be re-used in the main script (SlicerCART.py file).

In the main .py file (e.g. SlicerCART.py), you use the following syntax: import subFolder.SlicerConfigurationWindows as aaa where:
subfolder is the name of the subfolder
‘SlicerConfigurationWindows’ is the name of the child python file
‘aaa’ is the abbreviation used in the main script to call the method

In a child python file (e.g. SlicerConfigurationWindow.py), you can insert your functions that you want to use in the main python script. You can also import the content of another child python file in the same subfolder by doing import subFolder.CheckFunctions as cf where:
subfolder is the folder location of this child python file
‘CheckFunctions’ is the name of the other python file to be re-used in this child python file
‘cf’ is the abbreviation used in this child python file to call the method

*Note that Slicer python interpreter will always consider the path from the main python file associated with the module. This means that if you want to import a child python file at the same level than another python file, you must specify the location according to the main python file. For example: to import subFolder.CheckFunctions.py in subFolder.SlicerConfigurationWindows.py, you must write import subFolder.CheckFunctions as cf in the code line of subFolder.SlicerConfigurationWindows (see image below).

image

In each child python file (e.g. CheckFunctions.py), it is recommended to add the following imports:

from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin

Thanks.

@laurentletg
Copy link
Collaborator Author

Thanks for your investigation.

This seems to be the standard Python way of modularizing code. Is there any specific item besides adding the 2 lines you mentioned at the end of your post ?

Few questions:

  • Should you add a __init__.py file in subFolder ?
  • What naming conventions should be used for subFolder ? Per PEP8 it think modules should be lowercase but I know that Slicer uses C++/CamelCase also.
  • In the past I was able to split some of the code (e.g. qt components) outside of the main module script but each change to these separate scripts required a 3D Slicer restart instead of using the reload button : this is annoying when developing a module because of the time it takes to restart Slicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority high anything within the big picture, not nit-picky
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants