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), 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

Parameter Fields

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

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

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