# Can a Python script detect which Perspective view or binding it is running in?

**URL:** <https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [December 17, 2024, 4:11pm UTC](https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937 "2024-12-17T16:11:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [December 17, 2024, 4:11pm UTC](https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937/1 "2024-12-17T16:11:14Z")

</div>

I know how to use ApplicationScope to determine which context a script is running in, and can tell if it's in Perspective, but I'm wondering if there's any other info the script can grab about it's context.

I'm especially looking for info like which Perspective view and which property binding or message handler the script is being run within.

We're adding debug wrapping around some of our most heavily used low-level script functions to try and get a sense whether they're being mis-used or abused by any particular views.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [December 17, 2024, 4:14pm UTC](https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937/2 "2024-12-17T16:14:23Z")

</div>

For bindings, you might could leverage the Integration Toolkit's debugging expressions. [Debugging - Integration Toolkit for Ignition](https://www.automation-pros.com/toolkit/doc/debugging.html)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 17, 2024, 4:24pm UTC](https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937/3 "2024-12-17T16:24:51Z")

</div>

Possibly a starting point here:

> [@Get current username / Session in scripting in perspective](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/9):
>
> The ugly, hacky, unsupported, completely-liable-to-break-on-any-upgrade way to get magical access to the current session is using a public static ThreadLocal, as mentioned here: As my first sentence alludes - this is a bad practice, liable to break at any time.

If you just _log_ (not print!) a message, you should get useful MDC output indicating some information.

Technically you could retrieve that at runtime, via the static method on the MDC class:  
`org.slf4j.MDC.get("key")`

As in:

```python
from org.slf4j import MDC
component = MDC.get("component")

```

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [December 17, 2024, 4:33pm UTC](https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937/4 "2024-12-17T16:33:54Z")

</div>

The MDC object gives access to things like `view` and `target` that I can see appended to some lines in the logs? That should be most of what I want.

Is there rhyme or reason why that MDC data isn't always present on the log lines? I haven't dug in to confirm, but I feel like I've seen logging output from a Perspective binding that didn't have that info. I'm trying to use this debug wrapping to generate a coverage report among other things and if some calls don't have context then that gets less useful.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 17, 2024, 4:35pm UTC](https://forum.inductiveautomation.com/t/can-a-python-script-detect-which-perspective-view-or-binding-it-is-running-in/99937/5 "2024-12-17T16:35:12Z")

</div>

I'd have to see a particular log to be sure. In general, MDC works as a 'stacking' context side-channel for information - so we're in charge of manually putting that context onto the stack as well as removing it afterwards (so it doesn't 'leak' onto other things executing on the same thread after). Bugs are definitely possible, though my expectation is that most of them will have shaken out by now in core areas like Perspective components.
