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.)
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.
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.
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.
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.
If we want to print out a function’s return value, we can use a basic print statement passing in the function call.
Next, we’ll call the multiply and divide functions with our new variable.
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.
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.
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.