Python Extensions
Use Python to create Actions, Components, or Viewers with CamiTK’s Python API.
Getting Started with Python Extensions
Python extensions are ideal for rapid prototyping, machine learning integration, and scripting. DevStudio generates a Python file for each action for you to complete.
Steps to Create a Python Extension:
- Open DevStudio from the CamiTK menu.
- Define your extension's name, package requirements (if any), parameters, and input components.
- DevStudio generates the boilerplate code: a
.pyfor each action for you to edit and a.camitkcontaining the metadata of your extension. - As shown in the devstudio video click on Initialize to generate the boilerplate code for first time and then on Register to integrate it in CamiTK (it will then be automatically available in the corresponding Action menu).
- Complete the
processfunction in the generated Python file.
Python Action code template
When DevStudio generates the template code of an action, you will find four functions in the corresponding Python file. Some Python code is generated when DevStudio creates your action to show you an example of what is expected (see devstudio video). It is now up to you to modify the four functions/methods:
init: this function is automatically called when the extension is loaded by CamiTK. Modify it to enter code to initialize fields, state variables, or open connection to a device.targetDefinedis run when the user changes the input data (called target in an action) in the interface by selecting a component and then click on your action to trigger it. Use this if you need to update the action state/fields depending on the input data.parameterChangedis run when a parameter value is modified is the GUI automatically generated from your action parameters you added in DevStudio. Use this if you need to update the action state/fields depending on the input parameters.processthis is the main code, run when the user clicks on the Apply button. This is where you write the Python code that details your data processing.
Check the API and tutorials to see what is possible and some examples.
CamiTK Python API
Python API Documentation
The CamiTK Python API provides classes and methods to interact with CamiTK's core functionality. Explore the full documentation:
Python Tutorials
Tutorials
Learn by example! Check out our Python extension tutorials to understand how to:
- Create a Sphere MeshComponent.
- Process ImageComponents (e.g., threshold).
- Integrate PyTorch models for segmentation.
Python Versioning and Extension Isolation
Supported Python version
The version of python used in the extension is fixed when CamiTK is build. To minimize incompatibility between python versions, the python version used by your extension will depend on how you installed CamiTK and therefore on your OS.
Whether you have installed CamiTK from the official repository for Debian, Ubuntu and their derivatives, or whether you have built it from source, the version of python used is the one supported by the official repository and installed system-wide.
- If you have installed the stable version of CamiTK using the archive provided on the download page, we have included a built-in, non-intrusive version of python (via WinPython). You do not need to install python on your system, and CamiTK will not interfere with existing previous installation, if you have one.
- If you have built CamiTK from source, the installation process has installed a specific compatible version on your system.
When you built CamiTK from source, the installation process required python to be installed on your system, it is this version that will be used by your extensions
Which version of Python does CamiTK use?
To find out which version of Python CamiTK is using:
- In the CamiTK imp application go to Help → About and check the Python tab
- In the terminal run
camitk-config -cand look for the line starting withPython Status
Isolated environment
When you create a Python extension, CamiTK automatically creates a new python virtual environment dedicated to your extension and uses pip to install the specified requirements. Running in an isolated virtual environment ensures:
- greater reliability: the packages required by your extension not interfere with those already installed on your machine
- better reproducibility: your extension’s dependencies/requirements are specified via DevStudio, allowing you to easily share your extension with other CamiTK users or developers
About using Qt and VTK in your extensions
If your Python extension requires Qt or VTK, and to avoid any incompatibilities, CamiTK ensures that your extension is using the same version as the one used when CamiTK was built. This is done automatically by CamiTK’s Python Manager.
What it means in details:
- CamiTK is only compatible with PySide, not with PyQt. Although both implement a full Qt binding, there are some small differences in the import syntax and naming conventions for certain classes. PySide is maintained by the Qt company and is available under the LGPL licence, which is compatible with the CamiTK license.
- The supported version of PySide depends on the version of Qt used to build CamiTK: if CamiTK was built with Qt6 (the default for CamiTK >= 6.0), the requirement field must specify PySide6, if it was built on legacy systems using Qt5, the requirement field must specify PySide2.
- On Debian/Ubuntu and its derivatives, CamiTK does not install PySide or VTK via
pipwithin the extension’s virtual environment. Instead, it links to the system-wide installed versions to ensure compatibility. - If you specify a dependency pn VTK or Qt (and even if you specify a specific version in the requirement field using the
==syntax), CamiTK will ignore it and use the version corresponding to the build version.
Tip: If you’re wondering what impact this might have on your code, don’t worry – it shouldn’t really have any. To get started using Qt and VTK in your extensions, we recommend checking out the tutorials.