# Execute Javascript using PYV8

**URL:** <https://forums.plex.tv/t/execute-javascript-using-pyv8/183206>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [March 7, 2017, 5:50am UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206 "2017-03-07T05:50:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pimylifeup](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@pimylifeup](https://forums.plex.tv/u/pimylifeup)\
**Post date:** [March 7, 2017, 5:50am UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/1 "2017-03-07T05:50:55Z")

</div>

Is it possible to execute a .js in the Channel Plugin using PYV8 or alike.

I have a local .js file in the channel and I would like to use it from within the channel python code

---

<div class="post-metadata">

**Author:** ![czukowski](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/czukowski/32/9008_2.png) [@czukowski](https://forums.plex.tv/u/czukowski)\
**Post date:** [March 7, 2017, 3:29pm UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/2 "2017-03-07T15:29:25Z")

</div>

I’ve had some degree of success with [js2py](https://github.com/PiotrDabkowski/Js2Py "js2py"), but it was to execute Javascript from web pages. You may use just about any Python code if you place it under `Libraries/Shared` subdirectory and set PlexPluginCodePolicy to Elevated in the channel’s Info.plist, it will not be a subject of sandbox restrictions.

You could also consider rewriting your JavaScript to Python or running it in Node server, could be easier and/or more convenient in the end…

---

<div class="post-metadata">

**Author:** ![Twoure](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/twoure/32/91016_2.png) [@Twoure](https://forums.plex.tv/u/Twoure)\
**Post date:** [March 8, 2017, 1:03am UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/3 "2017-03-08T01:03:52Z")

</div>

I used to use [PyExecJS](https://github.com/doloopwhile/PyExecJS) before switching to [js2py](https://github.com/PiotrDabkowski/Js2Py). Here’s what was in my [Libraries/Shared](https://github.com/Twoure/KissNetwork.bundle/tree/v1.2.4/Contents/Libraries/Shared) at the time I was using PyExecJS. I was finding users who were having issues with PyExecJS `v1.4.0` so gave them a fall back method for `v1.1.0`.

* * *

**Edit:** here is an example of how I was testing on import  
[Contents/Services/URL/KissVideo/ServiceCode.pys#L14…L19](https://github.com/Twoure/KissNetwork.bundle/blob/v1.2.4/Contents/Services/URL/KissVideo/ServiceCode.pys#L14...L19)

---

<div class="post-metadata">

**Author:** ![pimylifeup](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@pimylifeup](https://forums.plex.tv/u/pimylifeup)\
**Post date:** [March 8, 2017, 5:26am UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/4 "2017-03-08T05:26:32Z")

</div>

> @czukowski said:  
> I’ve had some degree of success with [js2py](https://github.com/PiotrDabkowski/Js2Py "js2py"), but it was to execute Javascript from web pages. You may use just about any Python code if you place it under `Libraries/Shared` subdirectory and set PlexPluginCodePolicy to Elevated in the channel’s Info.plist, it will not be a subject of sandbox restrictions.
> 
> You could also consider rewriting your JavaScript to Python or running it in Node server, could be easier and/or more convenient in the end…

Sounds good.  
Could I use [pysqlite](https://pypi.python.org/pypi/pysqlite "pysqlite") by downloading and putting it in the `Libararies/Shared` folder and then import it in my init\_py.

Could you possibly maybe share a snippet of how I might achieve that.

My issue is that “sqlite3” import is restricted on most OS where PMS runs. Windows and Mac are exceptions, last time I checked.

I would like to either import [sql.js](https://github.com/kripken/sql.js/ "sql.js") or a python equivalent that will let me run some commands on a db.

My expertise is only in js and not python, what would you recommend I use

Thank you for your time

---

<div class="post-metadata">

**Author:** ![czukowski](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/czukowski/32/9008_2.png) [@czukowski](https://forums.plex.tv/u/czukowski)\
**Post date:** [March 8, 2017, 7:52pm UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/5 "2017-03-08T19:52:51Z")

</div>

@pimylifeup if all you need is to access sqlite database, I think you should explore options how to do it in Python, because it is likely that by executing JavaScript you still would not achieve your goal. Even though you can run JavaScript code from Python, it doesn’t necessarily have the functions to connect to the database.

That said, including libraries from `Libararies/Shared` should not have any restrictions. Using the code is really easy, like this:

```auto
import js2py

```

If you have troubles using sqlite3 extension on some platforms, it could be that it’s missing from Plex on those OSes. You could try to bundle it to your channel as well.

---

<div class="post-metadata">

**Author:** ![pimylifeup](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@pimylifeup](https://forums.plex.tv/u/pimylifeup)\
**Post date:** [March 9, 2017, 4:59am UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/6 "2017-03-09T04:59:35Z")

</div>

> @czukowski said:  
> @pimylifeup if all you need is to access sqlite database, I think you should explore options how to do it in Python, because it is likely that by executing JavaScript you still would not achieve your goal. Even though you can run JavaScript code from Python, it doesn’t necessarily have the functions to connect to the database.
> 
> That said, including libraries from `Libararies/Shared` should not have any restrictions. Using the code is really easy, like this:
> 
> ```py
> import js2py
> 
> ```
> 
> If you have troubles using sqlite3 extension on some platforms, it could be that it’s missing from Plex on those OSes. You could try to bundle it to your channel as well.

> @czukowski said:  
> @pimylifeup if all you need is to access sqlite database, I think you should explore options how to do it in Python, because it is likely that by executing JavaScript you still would not achieve your goal. Even though you can run JavaScript code from Python, it doesn’t necessarily have the functions to connect to the database.
> 
> That said, including libraries from `Libararies/Shared` should not have any restrictions. Using the code is really easy, like this:
> 
> ```py
> import js2py
> 
> ```
> 
> If you have troubles using sqlite3 extension on some platforms, it could be that it’s missing from Plex on those OSes. You could try to bundle it to your channel as well.

Thank you for your time and advise, I think my best bet would be to bundle the sqlite library in my channel.

---

<div class="post-metadata">

**Author:** ![pimylifeup](https://avatars.discourse-cdn.com/v4/letter/p/df705f/32.png) [@pimylifeup](https://forums.plex.tv/u/pimylifeup)\
**Post date:** [March 9, 2017, 4:29pm UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/7 "2017-03-09T16:29:58Z")

</div>

> @Twoure said:  
> I used to use [PyExecJS](https://github.com/doloopwhile/PyExecJS) before switching to [js2py](https://github.com/PiotrDabkowski/Js2Py). Here’s what was in my [Libraries/Shared](https://github.com/Twoure/KissNetwork.bundle/tree/v1.2.4/Contents/Libraries/Shared) at the time I was using PyExecJS. I was finding users who were having issues with PyExecJS `v1.4.0` so gave them a fall back method for `v1.1.0`.
> 
> * * *
> 
> **Edit:** here is an example of how I was testing on import  
> [Contents/Services/URL/KissVideo/ServiceCode.pys#L14…L19](https://github.com/Twoure/KissNetwork.bundle/blob/v1.2.4/Contents/Services/URL/KissVideo/ServiceCode.pys#L14...L19)

Thank you for sharing your coed.  
I will try my best to understand it (I am more of a js dev).

My issue is that “sqlite3” import is restricted on most OS where PMS runs. Windows and Mac are exceptions, last time I checked.

I would like to either import [sql.js](https://github.com/kripken/sql.js/ "sql.js") or a python equivalent that will let me run some commands on a db.

My expertise is only in js and not python, what would you recommend I use

Thank you for your time

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [January 8, 2020, 8:02pm UTC](https://forums.plex.tv/t/execute-javascript-using-pyv8/183206/8 "2020-01-08T20:02:49Z")

</div>


