Provides a Python interpreter context for executing Python code and interacting with MITK data like images.
More...
#include <mitkPythonContext.h>
|
| | PythonContext (const std::string &venvName={}) |
| | Constructs a PythonContext and optionally creates or activates a virtual environment. More...
|
| |
| | ~PythonContext () |
| | Destructor. Clears the internal Python dictionary. More...
|
| |
| void | Activate (bool importBindings=true) |
| | Initializes the Python interpreter context and sets up module paths. More...
|
| |
| bool | HasVariable (const std::string &varName) |
| | Checks whether a Python variable with the given name exists. More...
|
| |
| std::optional< bool > | GetVariableAsBool (const std::string &varName) |
| | Retrieves a Python variable as a bool. More...
|
| |
| std::optional< int > | GetVariableAsInt (const std::string &varName) |
| | Retrieves a Python variable as an int. More...
|
| |
| std::optional< double > | GetVariableAsDouble (const std::string &varName) |
| | Retrieves a Python variable as a double. More...
|
| |
| std::optional< std::string > | GetVariableAsString (const std::string &varName) |
| | Retrieves a Python variable as a string. More...
|
| |
| void | BindImage (mitk::Image *image, const std::string &varName) |
| | Binds an MITK image to a Python variable in the context's dictionary. More...
|
| |
| void | Execute (const std::string &expression) |
| | Executes arbitrary Python code within this context. More...
|
| |
| void | ExecuteFile (const fs::path &filePath) |
| | Executes a Python file within this context. More...
|
| |
Provides a Python interpreter context for executing Python code and interacting with MITK data like images.
This class wraps a Python interpreter and maintains a dictionary for executing Python code. It allows binding MITK data like images to Python variables and retrieving Python variables from the context in a type-safe manner.
A typical usage pattern is:
ctx.Activate();
ctx.BindImage(image, "input_image");
ctx.Execute("result = process(input_image)");
auto value = ctx.GetVariableAsInt("result");
Provides a Python interpreter context for executing Python code and interacting with MITK data like i...
- See also
- mitk::nnInteractiveTool
Definition at line 47 of file mitkPythonContext.h.
◆ PythonContext()
| mitk::PythonContext::PythonContext |
( |
const std::string & |
venvName = {} | ) |
|
|
explicit |
Constructs a PythonContext and optionally creates or activates a virtual environment.
If the Python interpreter has not been initialized yet, it will be initialized during construction.
- Parameters
-
| [in] | venvName | Name of the Python virtual environment to create or activate. If empty, no virtual environment is used. |
- Exceptions
-
- Postcondition
- The Python interpreter is initialized.
◆ ~PythonContext()
| mitk::PythonContext::~PythonContext |
( |
| ) |
|
Destructor. Clears the internal Python dictionary.
◆ Activate()
| void mitk::PythonContext::Activate |
( |
bool |
importBindings = true | ) |
|
Initializes the Python interpreter context and sets up module paths.
Always adds the base interpreter's and the active virtual environment's site-packages to sys.path, so a distribution installed in the venv is importable and its metadata is readable. With importBindings (the default) it additionally imports NumPy and the MITK Python module for data exchange.
Pass false to keep the context free of any venv native library. On Linux the activated venv becomes sys.prefix, so its site-packages precede the base on sys.path and importing NumPy would load it (and its compiled extensions) from the venv. A later "is any venv module loaded?" check would then report true and block an in-place update or uninstall. Metadata-only work (version and install probes) must therefore activate without bindings.
- Parameters
-
| [in] | importBindings | Import NumPy and the MITK module when true. |
- Precondition
- The PythonContext has been constructed.
- Exceptions
-
◆ BindImage()
| void mitk::PythonContext::BindImage |
( |
mitk::Image * |
image, |
|
|
const std::string & |
varName |
|
) |
| |
Binds an MITK image to a Python variable in the context's dictionary.
The image is passed by reference to Python (no copy). If the image pointer is nullptr, the variable is set to Python's None.
- Parameters
-
| [in] | image | Pointer to the Image to bind. If nullptr, the variable is set to None. |
| [in] | varName | Name of the Python variable to assign the image to. |
- Exceptions
-
◆ Execute()
| void mitk::PythonContext::Execute |
( |
const std::string & |
expression | ) |
|
Executes arbitrary Python code within this context.
The code is executed using the context's dictionary, so variables set in previous calls are available in subsequent ones.
- Parameters
-
| [in] | expression | The Python code to execute. May contain multiple statements separated by newlines. |
- Exceptions
-
◆ ExecuteFile()
| void mitk::PythonContext::ExecuteFile |
( |
const fs::path & |
filePath | ) |
|
Executes a Python file within this context.
The file contents are executed using the same dictionary as Execute(), so variables and imports remain available across calls. The special file variable is set to the executed file path.
- Parameters
-
| [in] | filePath | Absolute or relative path to the Python file. |
- Exceptions
-
◆ GetVariableAsBool()
| std::optional<bool> mitk::PythonContext::GetVariableAsBool |
( |
const std::string & |
varName | ) |
|
Retrieves a Python variable as a bool.
- Parameters
-
| [in] | varName | Name of the Python variable. |
- Returns
- The variable value if it exists and can be cast to
bool, std::nullopt otherwise.
◆ GetVariableAsDouble()
| std::optional<double> mitk::PythonContext::GetVariableAsDouble |
( |
const std::string & |
varName | ) |
|
Retrieves a Python variable as a double.
- Parameters
-
| [in] | varName | Name of the Python variable. |
- Returns
- The variable value if it exists and can be cast to
double, std::nullopt otherwise.
◆ GetVariableAsInt()
| std::optional<int> mitk::PythonContext::GetVariableAsInt |
( |
const std::string & |
varName | ) |
|
Retrieves a Python variable as an int.
- Parameters
-
| [in] | varName | Name of the Python variable. |
- Returns
- The variable value if it exists and can be cast to
int, std::nullopt otherwise.
◆ GetVariableAsString()
| std::optional<std::string> mitk::PythonContext::GetVariableAsString |
( |
const std::string & |
varName | ) |
|
Retrieves a Python variable as a string.
- Parameters
-
| [in] | varName | Name of the Python variable. |
- Returns
- The variable value if it exists and can be cast to
std::string, std::nullopt otherwise.
◆ HasVariable()
| bool mitk::PythonContext::HasVariable |
( |
const std::string & |
varName | ) |
|
Checks whether a Python variable with the given name exists.
- Parameters
-
| [in] | varName | Name of the Python variable to check. |
- Returns
true if the variable exists in the context's dictionary, false otherwise.
The documentation for this class was generated from the following file: