RunPythonScript¶
-
MacroModule
¶ genre FlowControl
author MeVis Medical Solutions AG
package MeVisLab/Standard
definition RunPythonScript.def keywords python
,scripting
,trigger
,fieldlistener
Purpose¶
This module allows to execute Python scripts from within a MeVisLab network without incorporating a separate .script and .py file. This could also be considered as a MeVisLab scripting-console squeezed into a macro module. It may be useful to quickly incorporate scripting functionality within networks without .scripts. Also, it might become handy in conjunction with advanced math packages such as numpy for doing in-place calculations.
Usage¶
The module offers two sections for Python source code. The init section is only executed upon initialization of the module (or manually, to update code changes). It can be used to initialize global variables or to define functions. The main section is updated whenever the Execute field is triggered. Alternatively, changes of the pre-defined input variables in0 - in9 can trigger execution of the main block.
Details¶
The module makes use of Pythons dynamic programming capabilities. The passed source-code is executed using the exec statement in python. No checks are performed on the passed source-code, so watch out what your scripts look like.
The context of the script (ctx variable) is that of the network that runs the module. The context of the RunPythonScript
-module that runs the script is available as RunPythonScript_ctx. (This was changed compared to previous versions of the module, in order to facilitate copying code from RunPythonScript
into separate .py files and vice versa.) Have a look at the example to see how it works.
The sub-module that holds the user’s Python code is also available as RunPythonScriptContext_ctx.
There are 10 pre-defined input- and 10 additional output-parameters available, to ease communication between a script and a network. Their values can be accessed via RunPythonScript_ctx.field(“out0”).value, for instance, but there are more convenient ways:
The input parameters are additionally injected directly into the Python namespace. The
Edit field titles (reload after editing to clean up layout)
option allows to assign speaking names to the parameters, and the input parameters will be made available under that name. That means, if the “in0” parameter is assigned the name “slice”, its value will be available as Python variable “slice”.Integers and floats will be recognized and converted to native types, so you could directly use the variable “slice” without writing int(slice), for instance. Note, however, that the type depends on the value, so a value of “35” will be interpreted as int, a value of “35.0” will become a float, and “35mm” will stay a string. (Technically, int/float conversions are tried in order.)
The output parameters can be set using
setOutputValue(name, value)
, orupdateOutputValue(name, value)
. Both functions expect name to be one of the configured speaking names (Edit field titles (reload after editing to clean up layout)
), and the latter usesfield.updateValue()
(instead of justfield.value
assignment) to suppress unnecessary notifications.
The input-parameters can also be configured to trigger an auto-execution of the passed script.
Tips¶
This module can also be used to execute Python code from the command line in a MeVisLab context.
Use MeVisLab with the -runappbatch RunPythonScript command line parameters, followed by the filename of a Python code file, and, optionally, an additional path to extra Python packages, these are added to sys.path.
Warning
Do not use this in an application, that is, do not add RunPythonScript to the allowed features of a run-time license, as the executed Python code is not checked. This would be a security risk!
Parameter Fields¶
Field Index¶
Auto Apply0 : Bool |
In Comment0 : String |
Out Comment0 : String |
Code (code) : String |
In Comment1 : String |
Out Comment1 : String |
Code (showCode) : Bool |
In Comment2 : String |
Out Comment2 : String |
Console : Bool |
In Comment3 : String |
Out Comment3 : String |
Edit field titles (reload after editing to clean up layout) : Bool |
In Comment4 : String |
Out Comment4 : String |
Execute (execute) : Trigger |
In Comment5 : String |
Out Comment5 : String |
Execute (init) : Trigger |
In Comment6 : String |
Out Comment6 : String |
Execute (wakeUp) : Trigger |
In Comment7 : String |
Out Comment7 : String |
Execute (finalize) : Trigger |
In Comment8 : String |
Out Comment8 : String |
executionFinished : Trigger |
In Comment9 : String |
Out Comment9 : String |
Fields : Bool |
In0 : String |
Out0 : String |
Finalize Code (finalizeCode) : String |
Init Code : Bool |
Wake Up Code (can be called using wakeUpCmd() ) : String |
Finalize Code (showFinalizeCode) : Bool |
Init Code (can be called using initCmd() ) : String |
WakeUp Code : Bool |
Visible Fields¶
Execute (execute)¶
-
name:
execute
, type:
Trigger
¶ This field triggers execution of the main portion of the passed script.
Execute (init)¶
-
name:
init
, type:
Trigger
¶ This field triggers execution of the init portion of the passed script.
Execute (wakeUp)¶
-
name:
wakeUp
, type:
Trigger
¶ This field triggers execution of the wakeUp portion of the passed script.
Execute (finalize)¶
-
name:
finalize
, type:
Trigger
¶ This field triggers execution of the finalize script.
In0¶
-
name:
in0
, type:
String
¶ in0 - in9
The input fields can be used to pass variables to the script. The values will be directly available as Python variables, and integer / float values will already be converted to int/float.
Edit field titles (reload after editing to clean up layout)¶
-
name:
editComments
, type:
Bool
, persistent:
no
¶
In Comment0¶
Auto Apply0¶
-
name:
autoApply0
, type:
Bool
, default:
TRUE
¶ auto-execute
The auto-execute checkboxes define whether changes to the field automatically trigger execution of the main script.
Out0¶
-
name:
out0
, type:
String
¶ out0 - out9
The output fields can be used, to send results back to the network.
Init Code (can be called using initCmd() )¶
-
name:
initCode
, type:
String
, default:
# For backward compatibility, set up CTX as alias for ctx, CTX = ctx # ctx is the network's context, not RunPythonScript's
¶ Type your init-code into this section.
Wake Up Code (can be called using wakeUpCmd() )¶
-
name:
wakeUpCode
, type:
String
¶ Type your wakeup code here. Called on module wakeUp (after connections of the networks are restored)