Python 3 in Embedded Script

Posted on
Wed Apr 21, 2021 4:17 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

Thanks, I had tried to do this through the shell command without success. Then back to the original problem of getting the output from the Python3 and inserting into Indigo.

Posted on
Wed Apr 21, 2021 4:18 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python 3 in Embedded Script

Oh, wait. You need to get the stdout value from the Python3 script back into a variable, don't you. That's doable, but more work.

Something like this should work.

Code: Select all
p = Popen(["/usr/bin/python3", '/path/to/py3script.py', 'Arg1','Arg2'], stdin=PIPE, stdout=PIPE, bufsize=1, universal_newlines=True)
val = p.stdout.readline()


Then put val into an Indigo variable.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Apr 21, 2021 4:25 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

I'll give this a try and let you know

Posted on
Wed Apr 21, 2021 5:03 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Python 3 in Embedded Script

I'll just add that you can also access some information through Indigo's Restful API--which doesn't require you to have access to Indigo objects directly. The recommended method is to use the Python Requests library (especially with authentication).

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Wed Apr 21, 2021 5:08 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python 3 in Embedded Script

FlyingDiver wrote:
Unfortunately, it does not appear that you can include arguments with the Indigo action.


Actually, you can. Script:

Code: Select all
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import sys

print(sys.argv[0], sys.argv[1], sys.argv[2])


Action:

grab46.png
grab46.png (96.97 KiB) Viewed 2146 times


Value of script_output variable after running:

Code: Select all
/Users/jay/Projects/Python/test_py3_script.py first_arg second_arg


You can even include variable and device state substitutions.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Apr 21, 2021 5:19 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python 3 in Embedded Script

ryanbuckner wrote:
One more thing I had to do that wasn’t obvious. The directions using shell paths in Indigo say to use backslash as escape characters for spaces. This errored in the Indigo interface. Using quotes around the path worked.


Odd, I can't reproduce that. I used this in a run shell script action (altered from the above post) and it worked:

Code: Select all
/Users/jay/Projects/Python\ Test/test\ py3\ script.py first_arg second_arg

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Apr 21, 2021 6:16 pm
FlyingDiver offline
User avatar
Posts: 7216
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python 3 in Embedded Script

Well, darn. Ignore that script I posted above.

Jay - maybe update the description text to include that info?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Apr 22, 2021 8:57 am
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python 3 in Embedded Script

FlyingDiver wrote:
Jay - maybe update the description text to include that info?


It's in the docs for that action, but I'll look at adding it to the dialog itself.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Apr 22, 2021 5:25 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

jay (support) wrote:
ryanbuckner wrote:
One more thing I had to do that wasn’t obvious. The directions using shell paths in Indigo say to use backslash as escape characters for spaces. This errored in the Indigo interface. Using quotes around the path worked.


Odd, I can't reproduce that. I used this in a run shell script action (altered from the above post) and it worked:

Code: Select all
/Users/jay/Projects/Python\ Test/test\ py3\ script.py first_arg second_arg


Oh, I used the backslash instead of the space, not together to escape it. My fault.

Posted on
Thu Apr 22, 2021 5:34 pm
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

This worked for me as an embedded script. Easier to test than the shell :

Code: Select all
import subprocess

# first grab the lat/long from indigo and convert to string
lat = str(indigo.devices[105421725].states["position_lat"])          # State "position_lat" of "Ryan's BMW 540ix" from the BMW Connected Plugin
long = str(indigo.devices[105421725].states["position_lon"])     # State "position_lon" of "Ryan's BMW 540ix"' from the BMW Connected Plugin

# call the subprocess with 2 arguments and store in p
p = subprocess.check_output(["/usr/bin/python3", '/Library/Application Support/Perceptive Automation/Python2-includes/bmwgeo.py', lat, long], stderr=subprocess.STDOUT)

#receive the output and store in indigo variable
indigo.variable.updateValue(339870612, value=p)



Here's the code I used for the GeofFence. This is the file called by the subprocess and must be executable

Code: Select all
#!/usr/bin/python3
from shapely.geometry import Point, Polygon
import sys

def main():
   #define the polygon fence
    coords = [
        (39.008234937234256, -77.37684452531161),
        (39.00809324520176, -77.37627518191934),
        (39.00733602026089, -77.37671994280922),
        (39.0076547326009, -77.37724949835487)
    ]
    homeFence = Polygon(coords)

    lat = float(sys.argv[1]) #39.00777
    long = float(sys.argv[2]) #-77.37668600000001

    #create a point for your car
    bmw = Point(lat, long)

    #return a true/false to see if your car is inside the Polygon
    print("{}".format(bmw.within(homeFence)), file = sys.stdout)


if __name__ == "__main__":
    main()

Posted on
Mon May 17, 2021 10:30 am
ryanbuckner offline
Posts: 1080
Joined: Oct 08, 2011
Location: Northern Virginia

Re: Python 3 in Embedded Script

Adding more to this in case some folks run into issues. I found out that the response from the subprocess returns a string with a trailing "/n" which is invisible in the Indigo Variable window. It was causing my conditions and control page images to fail. I used the rstrip() to fix this issue:

Code: Select all
indigo.variable.updateValue(339870612, value=p.rstrip())

Posted on
Fri Aug 20, 2021 3:57 pm
geoffsharris offline
Posts: 45
Joined: Nov 26, 2012

Re: Python 3 in Embedded Script

Quick question for using the non system python.

I have 3.9.1 installed and all libraries I'm interested in set up using .pyenv

I can access the system version of python 3.8 using #! /usr/bin/python3 with a test pass of a variable, but if I try and use # /Users/username/.pyenv/shims/python with all 3.9.1 and all of the libraries I have installed, I get:

Run Shell Script action: script file doesn't exist: /Library/Application Support/Perceptive Automation/Scripts/test.py

any thoughts on what the shebang link should be to use the .pyenv python and associated libraries?

Who is online

Users browsing this forum: No registered users and 8 guests