Controlling the execution of Python code

By default, Python code inside a Live Python block runs from start to finish whenever a value is received via one of the inputs on the block. The value could be from a button that has been clicked, or from a knob control that the user has gestured, for example. You can configure the block to monitor all of the inputs (several may be wired to different devices) or just the first or last input.

Note: User-defined Python variables do not retain the values assigned to them from the last time the code was executed. Only the built-in state variable does this. For more information, see Using the persistent state variable.

Another way to run Python code is automatically using a thread. Threaded code will run as soon as the project has been deployed or emulated, it does not wait for a value to be received on one of the inputs. For more information, see Using threading to run scripts automatically. The remainder of this section assumes you want to control execution of the script using the inputs on the Live Python block.

To specify when to execute the Python code

  1. Right-click on the Live Python block, and then click Properties.

  2. In the watch input list, click an option to specify which input value must change in order to trigger the execution of the Python code.

    any

    A change to the value of any input will trigger the execution of the code.

    first

    Only a change to the value of the first input will trigger the execution of the code.

    last

    Only a change to the value of the last input will trigger the execution of the code.

  3. Click OK.

See also

Using the state variable to store a value persistently

Storing and reusing values from Python code

Using threading to run scripts autonomously