RunPythonScript

MacroModule

genre

FlowControl

author

MeVis Medical Solutions AG

package

MeVisLab/Standard

definition

RunPythonScript.def

keywords

python, scripting, trigger, fieldlistener

Purpose

The module RunPythonScript allows for executing 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 performing in-place calculations.

Usage

This 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 Python’s 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 submodule 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, as well as a single MLBase object input, 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 for assigning 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), or updateOutputValue(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 uses field.updateValue() (instead of just field.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!

Windows

Default Panel

../../../Modules/Macros/Helpers/mhelp/Images/Screenshots/RunPythonScript._default.png

Input Fields

inBase (hidden)

name: inBase, type: MLBase

Input object that should be made available to the Python code under the name given in In Base Comment (analogous to parameter fields).

This input connector is hidden by default. Press the Space key in the network view of the IDE to make it visible.

Note

Execution of the given Python code is only triggered when the Base input field is touched. Some Base objects have an own notification mechanism that will not trigger execution. One can usually connect a Python function to these notifications, but the code model of RunPythonScript (where the entire code is evaluated anew when triggered) is ill-suited for this, as disconnecting from notifications again is a daunting task.

Parameter Fields

Field Index

Auto Apply0: Bool

In Base Comment: String

Init Code (can be called using initCmd() ): String

Auto Update: Bool

In Base Type: String

Out Comment0: String

Code (code): String

In Comment0: String

Out Comment1: String

Code (showCode): Bool

In Comment1: String

Out Comment2: String

Console: Bool

In Comment2: String

Out Comment3: String

Edit field titles (reload after editing to clean up layout): Bool

In Comment3: String

Out Comment4: String

Execute (execute): Trigger

In Comment4: String

Out Comment5: String

Execute (init): Trigger

In Comment5: String

Out Comment6: String

Execute (wakeUp): Trigger

In Comment6: String

Out Comment7: String

Execute (finalize): Trigger

In Comment7: String

Out Comment8: String

executionFinished: Trigger

In Comment8: String

Out Comment9: String

Fields: Bool

In Comment9: String

Out0: String

Finalize Code (finalizeCode): String

In0: String

Wake Up Code (can be called using wakeUpCmd() ): String

Finalize Code (showFinalizeCode): Bool

Init Code: Bool

WakeUp Code: Bool

Visible Fields

Execute (execute)

name: execute, type: Trigger

When pressed, the main portion of the passed script is executed.

Execute (init)

name: init, type: Trigger

When pressed, the init portion of the passed script is executed.

Execute (wakeUp)

name: wakeUp, type: Trigger

When pressed, the wakeUp portion of the passed script is executed.

Execute (finalize)

name: finalize, type: Trigger

When pressed, the finalize script is executed.

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 Base Comment

name: inBaseComment, type: String, default: inBase

Analogously to In Comment0, specifies the variable name under which to pass inBase to the code

In Base Type

name: inBaseType, type: String, persistent: no

The type name of the object currently connected to inBase.

In Comment0

name: inComment0, type: String, default: in0

inComment0 - inComment9

Speaking names of the corresponding parameters, see Details.

In Comment1

name: inComment1, type: String, default: in1

In Comment2

name: inComment2, type: String, default: in2

In Comment3

name: inComment3, type: String, default: in3

In Comment4

name: inComment4, type: String, default: in4

In Comment5

name: inComment5, type: String, default: in5

In Comment6

name: inComment6, type: String, default: in6

In Comment7

name: inComment7, type: String, default: in7

In Comment8

name: inComment8, type: String, default: in8

In Comment9

name: inComment9, type: String, default: in9

Auto Update

name: autoUpdate, type: Bool, default: TRUE

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.

Out Comment0

name: outComment0, type: String, default: out0

Out Comment1

name: outComment1, type: String, default: out1

Out Comment2

name: outComment2, type: String, default: out2

Out Comment3

name: outComment3, type: String, default: out3

Out Comment4

name: outComment4, type: String, default: out4

Out Comment5

name: outComment5, type: String, default: out5

Out Comment6

name: outComment6, type: String, default: out6

Out Comment7

name: outComment7, type: String, default: out7

Out Comment8

name: outComment8, type: String, default: out8

Out Comment9

name: outComment9, type: String, default: out9

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 initialization 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).

Code (code)

name: code, type: String

Type the main code here.

Finalize Code (finalizeCode)

name: finalizeCode, type: String

Code called when (i.e., just before) the module is deleted, or when Execute is triggered explicitly.

Fields

name: showFields, type: Bool, default: FALSE

Init Code

name: showInitCode, type: Bool, default: FALSE

WakeUp Code

name: showWakeUpCode, type: Bool, default: FALSE

Code (showCode)

name: showCode, type: Bool, default: TRUE

Finalize Code (showFinalizeCode)

name: showFinalizeCode, type: Bool, default: FALSE

Console

name: showConsole, type: Bool, default: FALSE

Hidden Fields

executionFinished

name: executionFinished, type: Trigger