Live Python blocks allow scripts to be edited and updated at any time, even when a project is running. You can also use them to copy and paste scripts between projects, making them very flexible. We recommend that you use Live Python blocks in favor of Python Scripting blocks.
The Live Python block can be configured with a set number of inputs and outputs, allowing you to wire it to other devices in your design.
The inputs and outputs on the block can be referenced from Python code. You can work with values from other devices in the design via the inputs, and assign values to other devices using both the inputs and the outputs.
In Python code, the inputs and outputs are referenced using the arrays inputs[] and outputs[]. For example, the statement below will retrieve the value received via the first input on the block, and assign it to the variable x.
x = inputs[0].value_get()
When you open the block, it displays an icon for opening the code editor, and two fields for displaying status messages and error messages. You can wire these fields to other devices in your project to perform different operations when their values change.
Note: Before you can type in or paste in script to the device, you must emulate or deploy the project.
Tip: To use a different editor program, on the Tools menu, click User Preferences. Then, under External String Editor, click Browse to choose a new program. You can specify any editor program you want to use, but we recommend that it is language aware. This will make it easier to read the code that you write, and minimize the number of errors.
As soon as the project is emulated or deployed, you can double-click the edit icon to update the Python script for the block.
By default, the Python script is run when the value received on one of the inputs changes, but you can customize the behavior to suit your project. For more information, see Controlling the execution of Python code.
If there is an error in the Python script, an error message will be displayed in the Error box on the control surface. For more information see Debugging Python scripts.
See also |