This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.


Autoplay Off

Supplemental Videos


The Script Console in the Designer and the Print command are described in this lesson.

Video recorded using: Ignition 8.1


(open in window)

[00:00] In this lesson, we'll examine the scripting console in the Ignition Designer. Before we dive too deeply into the Python language, we should get familiar with the location where we can run scripts independently of any components, as well as a place we can direct print command outputs to let us see what we're doing as we develop our scripts. Scripts can run in a lot of places, but the script console is only available in the designer, not in a client or anywhere else. It's a tool that allows you to run scripts on demand. How do we get to the script console? We simply go to the tools menu and select the script console option. In our script console, the left pane is a multiline buffer into which we can type our script commands. And the right pane is the interactive interpreter where our script output will be directed. Let's start with a very simple example, a print. A print statement just echoes any text to an output console. Any text strings in Python need to be enclosed within quotation marks.

[01:03] You can use either double or single quotes. You just need to be consistent and not mix and match the two styles. Both the starting and ending quotes must match. So we'll simply type print followed by some text inside double quotes. How about the traditional, "Hello World," like so. Then when I click the execute at the bottom, we can see that the Hello World without the quotes is printed. Let's add a second copy of this, only this time with single quotes. So print in single quotes, 'Hello World.' Executing this again, now we see both outputs. This shows us that single quotes are just fine too. And also the scripts can be more than one line. The text isn't executed upon a simple carriage return. The whole left side is executed in its entirety. That's really useful as we incrementally develop larger scripts. It's very important to note that the right side pane here isn't just a console.

[02:04] It's actually a live Python terminal. In it we can clear the output display or clear all variables from our workspace. Let's see that with our next examples. For example, here on the left side, we can clear the print statements and now do x = 10, simple assignment statement. We can execute that, of course, but since there's no print statement, nothing shows up on the right. But then we can clear the left side and do print x, then execute, and then we will see the value we assigned to x printed out. Next, suppose we've executed a few times and our output console is getting a bit cluttered. I can click on this clear icon at the upper right. I guess it's supposed to be an eraser and the output display is cleared, but our value for x is still present because if I do print x on the left side, it's still remembers what x was assigned from before. All we've done is cleared the previous output prints, not the underlying data.

[03:06] However, if I want to start from scratch completely, I can click over here on the reset button. This actually clears out the variables workspace and restores the interpreter back to its startup state. Now, if I try to print x on the left side, I get this error message because x no longer exists in the workspace. As you can probably imagine, the script console is a great tool to use when learning how to write Python scripts or test them out in the Ignition Designer. However, it's important to keep the proper scope in mind, since that dictates where your output's gonna show up. For example, if we set x back equal to 10 and run these commands again on the left side of the script console, that output will get directed to the right side pane. However, if we've got a designer window open with some component on it, say a button, and that component was running a script with a print statement, that print output would be directed to the designer or perhaps client console, which I could access via the tools, console menu option.

[04:09] And it would print down here. And if perhaps we had a tag change script on one of our tags, it was printing out some sort of statement or string, we wouldn't see that output in the output console of the designer or the client. Instead, we'd see that output in the gateway's output console, which in this case would be the wrapper log. So this concept of scope is very important to keep in mind when writing any scripts in Ignition. The type of the script determines the scope as well as where the output will be directed. So in this lesson, we saw the use of the script console to test out Python script commands as we write them. This can be a very useful tool when developing any component or other scripting in the Ignition Designer.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.