Ignition Script Console

If you have used Python scripting in Ignition, the odds are you have opened the Script Console from the Tools menu. (Yes, it is technically Jython, but for our purposes we’re just going to say Python.)

 
Ignition Script Console article decorative header
 

You can put Python code on the left pane, and the right pane is an “Interactive Interpreter”. If you are like most people—including some of us who have worked with Ignition since the FactorySQL days—you might have used the script console mostly to validate code, do a few print statements to make sure everything is working correctly. But past that, you probably haven’t thought much more about the tool. The script console has so much more power than just printing out variables.

Screenshot of the tools menu in Ignition with the Script Console highlighted.

Running Python Code in Ignition

The best way to conceptualize the Script Console’s interactive interpreter is to think of it like a terminal window or command prompt where you can interact with your Python program. When you click the “Execute” button, it basically runs your Python code as if you had saved it as script_console.py, and lets you interact with it as if you were running it on your machine through the normal Python engine.

This means you can type in commands, declare variables, execute functions from the left pane, and truly interact with your code.

A Simple Example

To demonstrate how to use the interactive interpreter, we’ll do a few basic math functions in the Python pane. These will let us add, subtract, multiply, and divide numbers. We will pass in variables to the functions and return a value with the relevant operation performed.

Screenshot of the script console with add, subtract, multiply, and divide functions defined.

Once we have these functions entered, we’ll hit the “Execute” button on the bottom of the window, and we will be ready to use the interpreter.

The first thing we’ll do is define a couple of variables just like we would in any Python program.

Screenshot of the script console calling the add function to add two numbers together

Once we have defined the variables by typing in the code, we’ll hit enter, and then call the add function to add them together and return the result. In this case 100+99 = 199

Next, we will define another variable and set it equal to the value of the add function, then print it out to see the value.

Screenshot of the script console defining a variable and setting it equal to the result of the add function.

If we want to print out a function’s return value, we can use a basic print statement passing in the function call.

Using the variable defined in the previous screenshot, a value is subtracted from it using the subtract function.

Next, we’ll call the multiply and divide functions with our new variable.

Example of using the multiply and divide functions

Multiple Lines of Code and Advanced Logic

If we want to test some more advanced logic in the interpreter, we can even do complex structures, like for loops and if statements. To do this, type in the first line then press enter. You will see an ellipsis at the beginning of the line indicating you can add another line to the loop or if statement. Indenting still matters here, as seen in this for loop. Note: you will have to press enter after the last line to get a blank line, then press enter one more time to execute the multi-line statement.

>>> for i in range (100):
...          print "i-"+str(i)
...
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9

You can clear the interpreter using the eraser icon on the top right of the script console window, and you can reset the interpreter to a clean version of your code by clicking the refresh icon. In the screenshot below, we have reset the environment and tried to print the value of x which at this point has not been declared or given a value, so it returns an error.

Screen Shot 2021-09-01 at 4.07.56 PM.png
Example of an error in the script console when trying to print an undefined variable

Script Console Wrap Up

Hopefully this post shed some more light on what is possible with the script console in the Ignition Designer! Please read our other posts on this site for more tips and tricks for Ignition. Feel free to reach out if you have any questions or want any deeper dives into specific Ignition features since we get to work with this software every day.

Previous
Previous

Domain Forwarding to Perspective

Next
Next

Securing Your Ignition Gateway