How do I run a python script through Tulip Player?

I have a python script that interfaces with the control board of the product and runs a diagnostic test. The python script will then upload the test results to the corresponding product ID in the “Results” table using the Table API. At the moment I have to click out of the Tulip Player to run the python script using an external software (cmd, VSCode, etc.), is there a way that I can make a custom widget to run the python script within the Tulip Player?

It looks like the custom widget builder supports Javascript, so maybe I could use JS to run the python program? Just wondering if there’s a more straightforward way to accomplish this.

Thanks!

Hey @Daman -

I don’t have a perfect answer for you (because I have been yet to find one) but this is how I achieved this back when I was a Tulip Customer:

  1. Tulip App populates a table with the script to be run.
  2. A scheduler runs a python script ever~15 seconds to see if any new records have been added to that table
  3. When it finds a new record, it runs the corresponding script.

I used this approach extensively to implement my own email service to get around some of our specific email needs.

Another avenue is to use something like Flask in Python to host an api (see a guide here) that could then be called by a connector function. @Ethan has done this in the past, so he might have some more details about what that looks like practically.

Like you eluded to, custom widgets are a pretty slick avenue to run real code in Tulip Apps but they lack the ability to execute files stored locally on a given machine (this is an inherent limitation of anything webbased, OS doesn’t expose file structure). I don’t know of a great way to trigger a local python script from a custom widget (outside of the aforementioned table approach).

Hope this helps,
Pete

Yep, setting up an API with Flask and Flask Restful was not too complicated. Since you want to run local code you’d have to figure out how to let a local server be accessed through the internet so the Tulip Connector could reach it. Might be easier to run a local python script constantly in the background that is checking a table for updates like Pete suggested. But you could just have one record in the table with a True/False flag. Tulip sets to true (to run the script) and then the script sets it back to False.