Where to import libraries used on Perspective Components

UPDATE: I ended up importing the library at the top of the Project Library my component calls scripts from. In this case, my component is a Button and it's event calls functions in the Button script. So I have the import statement at the top of the Button script.

ORIGINAL POST
I'm running Ignition 8.1 with a Trial License and I have a general question regarding importing libraries. In general, say I want to use a library in a script called by a component event. There are several instances of this component so I don't want to include the "from xxx import xxx" statement on every event. So where do I make the "from xxx import xxx" call?

(Me not programmer, me make sparky sparks) :monkey_face:

I tried defining a Project Library script that is meant to import all libraries I need on startup. I think I need to call this script from the Session Events > Startup event. The following are snippets of the Project Library script, Session Event Startup function call, and usage of the library on the component onClick event (top to bottom):


My test method:

  • Restart session on browser
  • Go to view with component that uses this library in script tied to its onClick event
  • Go to preview mode
  • Click on component
  • Get undefined Timer error
  • Pull out hair

If I click on a component in the active Perspective Session I also get the error in the Gateway logs.

In reality, if you need to use a library function in a script, you need to import that library function on every script that calls it.
If you find yourself making lots of similar scripts, write the script in the Script Library, and just call it from the event each time.
The import is only called on runtime of the script, so multiple instances of the component doesn't matter unless you are running the script constantly.

I presume you're importing external libraries e.g. from Jython or Java libraries and not just project script libraries (the latter don't need to be imported)

3 Likes