onPageStartup session event

If anyone has time to help, I am trying to set certain pages to have a dark theme in a project. I used

system.perspective.setTheme(name = "dark",pageId = "/xxxxxx-dash")

in a page startup script, but i am having random results.

Can you go into more detail about what the ‘random results’ are that you are encountering?

Also, version of Ignition/Perspective would be helpful.

Ignition 8.1.5
Perspective 2.1.5
The page is configured as /press1-dash in the page configuration. I just checked the logs and is says:

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 2, in onPageStartup at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.lambda$operateOnPage$0(AbstractScriptingFunctions.java:57) at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.operateOnSession(AbstractScriptingFunctions.java:120) at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.operateOnPage(AbstractScriptingFunctions.java:47) at com.inductiveautomation.perspective.gateway.script.PerspectiveScriptingFunctions.setTheme(PerspectiveScriptingFunctions.java:360) at jdk.internal.reflect.GeneratedMethodAccessor1733.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Page "/press1-dash" not found.

at org.python.core.Py.JavaError(Py.java:552)

at org.python.core.Py.JavaError(Py.java:543)

at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)

at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:539)

at org.python.core.PyObject.__call__(PyObject.java:413)

at org.python.pycode._pyx2388.onPageStartup$1(:2)

at org.python.pycode._pyx2388.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:171)

at org.python.core.PyBaseCode.call(PyBaseCode.java:308)

at org.python.core.PyFunction.function___call__(PyFunction.java:471)

at org.python.core.PyFunction.__call__(PyFunction.java:466)

at org.python.core.PyFunction.__call__(PyFunction.java:461)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:842)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:824)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:687)

at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:993)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:752)

at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:133)

at com.inductiveautomation.perspective.gateway.script.ScriptRunner.run(ScriptRunner.java:23)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 2, in onPageStartup at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.lambda$operateOnPage$0(AbstractScriptingFunctions.java:57) at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.operateOnSession(AbstractScriptingFunctions.java:120) at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.operateOnPage(AbstractScriptingFunctions.java:47) at com.inductiveautomation.perspective.gateway.script.PerspectiveScriptingFunctions.setTheme(PerspectiveScriptingFunctions.java:360) at jdk.internal.reflect.GeneratedMethodAccessor1733.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Page "/press1-dash" not found.

... 24 common frames omitted

Caused by: java.lang.IllegalArgumentException: Page "/press1-dash" not found.

at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.lambda$operateOnPage$0(AbstractScriptingFunctions.java:57)

at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.operateOnSession(AbstractScriptingFunctions.java:120)

at com.inductiveautomation.perspective.gateway.script.AbstractScriptingFunctions.operateOnPage(AbstractScriptingFunctions.java:47)

at com.inductiveautomation.perspective.gateway.script.PerspectiveScriptingFunctions.setTheme(PerspectiveScriptingFunctions.java:360)

at jdk.internal.reflect.GeneratedMethodAccessor1733.invoke(Unknown Source)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.base/java.lang.reflect.Method.invoke(Unknown Source)

at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)

... 21 common frames omitted

Depending on how I word it, if I omit the pageId it will set everything to dark mode. I understand now that it is setting the entire project to dark mode.

The pageId has nothing to do with the configured URL of the page and everything to do with the unique identifier given to the page (tab) at runtime. Don’t let the Designer fool you when you look at page.props.pageId in the Designer - there is no such thing as a page in the Designer, but the View injects some dummy values to use as IDs.

An actual Page ID, taken from the AppBar:

Screen Shot 2021-08-09 at 11.08.14 PM

Some things to note:
If you omit the pageId, then the current page will be set to use the dark theme. Unless you have logic to check against certain page criteria, this onPageStartup Event will fire for every page, and so while it might look like you’re setting it for the Session, you’re actually setting it for each page as the page opens. If you need to set the entire session at once, you should set session.props.theme.

When I follow the example from Inductive university and run:

system.perspective.print("The page path is: " + page.props.path)

I get the actual path to the page.
I have dashboards in the project and would like to have only them in dark mode. what is the proper use of system.perspective.setTheme(name, [sessionId], [pageId]) to change the theme on one select page.
I have placed it on the root container of my pages and it works from there without the pageId, I am just wondering the proper way to use the variables of the script.

Booo! Hisss!

Don't! I know it is popular with cool young developers, but it is hard on older eyeballs. If you think about overall brightness vs. aperture (pupil) vs. depth of field, it is an obvious no-no.

1 Like

I know, but I am at the mercy of the departments that I build for.

1 Like

In the Page Startup Event:

if page.props.path == "/press1-dash":
    system.perspective.setTheme('dark')

OR
Configure an onStartup Event for the root container for the primary View of that page with JUST the system call to set the theme.

Root container onStartup Event:

system.perspective.setTheme('dark')

Thank you, I ended up using the second method.