Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkPythonContext.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkPythonContext_h
14 #define mitkPythonContext_h
15 
16 #include <mitkFileSystem.h>
17 #include <MitkPythonExports.h>
18 
19 #include <memory>
20 #include <optional>
21 #include <string>
22 
23 namespace mitk
24 {
25  class Image;
26 
48  {
49  public:
65  explicit PythonContext(const std::string& venvName = {});
66 
71 
96  void Activate(bool importBindings = true);
97 
106  bool HasVariable(const std::string &varName);
107 
116  std::optional<bool> GetVariableAsBool(const std::string& varName);
117 
126  std::optional<int> GetVariableAsInt(const std::string& varName);
127 
136  std::optional<double> GetVariableAsDouble(const std::string& varName);
137 
146  std::optional<std::string> GetVariableAsString(const std::string& varName);
147 
160  void BindImage(mitk::Image* image, const std::string& varName);
161 
173  void Execute(const std::string &expression);
174 
186  void ExecuteFile(const fs::path& filePath);
187 
188  private:
189  struct Impl;
190  std::unique_ptr<Impl> m_Impl;
191  };
192 }
193 
194 #endif
195 
#define MITKPYTHON_EXPORT
Image class for storing multi-dimensional medical image data.
Definition: mitkImage.h:76
Provides a Python interpreter context for executing Python code and interacting with MITK data like i...
~PythonContext()
Destructor. Clears the internal Python dictionary.
std::optional< bool > GetVariableAsBool(const std::string &varName)
Retrieves a Python variable as a bool.
void BindImage(mitk::Image *image, const std::string &varName)
Binds an MITK image to a Python variable in the context's dictionary.
void ExecuteFile(const fs::path &filePath)
Executes a Python file within this context.
std::optional< std::string > GetVariableAsString(const std::string &varName)
Retrieves a Python variable as a string.
PythonContext(const std::string &venvName={})
Constructs a PythonContext and optionally creates or activates a virtual environment.
std::optional< double > GetVariableAsDouble(const std::string &varName)
Retrieves a Python variable as a double.
void Activate(bool importBindings=true)
Initializes the Python interpreter context and sets up module paths.
bool HasVariable(const std::string &varName)
Checks whether a Python variable with the given name exists.
std::optional< int > GetVariableAsInt(const std::string &varName)
Retrieves a Python variable as an int.
void Execute(const std::string &expression)
Executes arbitrary Python code within this context.
Portability header that provides a unified namespace alias for the C++ filesystem library.
class ITK_EXPORT Image
Find image slices visible on a given plane.