<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Will Pringle</title>
    <description>The latest articles on DEV Community by Will Pringle (@willp).</description>
    <link>https://dev.to/willp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1132387%2Fdcd92252-f27d-4986-9002-e83fec956e94.jpg</url>
      <title>DEV Community: Will Pringle</title>
      <link>https://dev.to/willp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/willp"/>
    <language>en</language>
    <item>
      <title>Call JavaScript Code in Python</title>
      <dc:creator>Will Pringle</dc:creator>
      <pubDate>Thu, 03 Aug 2023 15:26:03 +0000</pubDate>
      <link>https://dev.to/willp/call-javascript-code-in-python-551a</link>
      <guid>https://dev.to/willp/call-javascript-code-in-python-551a</guid>
      <description>&lt;p&gt;This article introduces the usage of &lt;strong&gt;PythonMonkey&lt;/strong&gt;, &lt;strong&gt;a new Python library for running JavaScript code in Python&lt;/strong&gt; easily and efficiently. &lt;a href="https://docs.pythonmonkey.io/"&gt;Link to PythonMonkey docs&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Running JavaScript code in Python is valuable for a number of reasons such as leveraging the JavaScript ecosystem in Python or testing your JS code using Python to name a couple.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Required Installation
&lt;/h2&gt;

&lt;p&gt;Install PythonMonkey using pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip3 install pythonmonkey
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Simple Example
&lt;/h2&gt;

&lt;p&gt;Before getting into the details, let's use PythonMonkey to execute some JavaScript code in Python. We'll just do this in the Python repl by typing &lt;code&gt;python3&lt;/code&gt; into the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pythonmonkey&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pm&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" 'Hello World'.toUpperCase(); "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s"&gt;'HELLO WORLD'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Loading a JavaScript Module in Python Example
&lt;/h2&gt;

&lt;p&gt;In this example, we'll have two files: &lt;code&gt;my-javascript-module.js&lt;/code&gt; and &lt;code&gt;main.py&lt;/code&gt; which imports the functions from the JavaScript code:&lt;br&gt;
&lt;strong&gt;my-javascript-module.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello, world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;main.py&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pythonmonkey&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pm&lt;/span&gt;
&lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'./my-javascript-module'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# this prints hello, world
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which outputs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;hello, world&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  PythonMonkey API for JS/Py Interoperability
&lt;/h2&gt;

&lt;p&gt;Below are the functions available for use in Python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pythonmonkey.eval(code_string)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Input: String containing JavaScript Code&lt;/li&gt;
&lt;li&gt;Evaluates JavaScript code within a JavaScript context. Will return an evaluated JavaScript structure.&lt;/li&gt;
&lt;li&gt;It can take an optional second argument for eval options, but we won't use it in this tutorial.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pythonmonkey.require(moduleIdentifier)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Input: Path to the JavaScript Module or identifier as per CommonJS spec.&lt;/li&gt;
&lt;li&gt;Loads a JavaScript file / module in Python for use. This is the same usage as Node.js' &lt;code&gt;require&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pythonmonkey.globalThis&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;An object that contains the global variables defined in the JavaScript context. For instance, you could define a variable using &lt;code&gt;pm.eval('const a = 4;')&lt;/code&gt; and access it in python using &lt;code&gt;pm.globalThis.a&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pythonmonkey.*&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;PythonMonkey also exports the JavaScript API as part of the module. For instance, you could use the WebAssembly API in Python through &lt;code&gt;pythonmonkey.WebAssembly&lt;/code&gt; or instantiate a new JavaScript Date using &lt;code&gt;pythonmonkey.Date&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pythonmonkey.new(js_constructor)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Input: A JavaScript constructor.&lt;/li&gt;
&lt;li&gt;Since Python lacks a "new" keyword for instantiating Objects but JavaScript needs it, PythonMonkey provides a &lt;code&gt;pythonmonkey.new()&lt;/code&gt; function which returns a factory function for the constructor passed in. For instance, &lt;code&gt;my_date = pythonmonkey.new(pythonmonkey.Date)(0)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For full and up to date documentation for PythonMonkey, refer to the PythonMonkey docs: &lt;a href="https://docs.pythonmonkey.io/"&gt;https://docs.pythonmonkey.io/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Example (using WASM in Python)
&lt;/h2&gt;

&lt;p&gt;PythonMonkey can be used to execute WebAssembly in Python!&lt;/p&gt;

&lt;p&gt;We'll read a WebAssembly &lt;code&gt;.wasm&lt;/code&gt; file in Python and load it using &lt;code&gt;pythonmonkey.WebAssembly&lt;/code&gt; which has a function "&lt;code&gt;factorial()&lt;/code&gt;":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt; &lt;span class="c1"&gt;# we'll use asyncio to deal with an event loop
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pythonmonkey&lt;/span&gt;

&lt;span class="c1"&gt;# we'll put our code in an async python function
&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_fn&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="c1"&gt;# read the factorial.wasm binary file
&lt;/span&gt;  &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'factorial.wasm'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'rb'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;wasm_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;bytearray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

  &lt;span class="c1"&gt;# instantiate the WebAssembly code
&lt;/span&gt;  &lt;span class="n"&gt;wasm_fact&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;pythonmonkey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WebAssembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wasm_bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;

  &lt;span class="c1"&gt;# return the "fac" factorial function from the wasm module
&lt;/span&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;wasm_fact&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fac&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;# await the promise which returns the factorial WebAssembly function
&lt;/span&gt;&lt;span class="n"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;async_fn&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# execute WebAssembly code in Python!
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# this outputs "24.0" since factorial(4) == 24
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# this outputs "120.0"
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# this outputs "720.0"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This outputs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;24.0&lt;br&gt;
120.0&lt;br&gt;
720.0&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For more advanced examples, check out my article on &lt;a href="https://medium.com/@willkantorpringle/executing-rust-in-python-using-webassembly-d361eb5583da"&gt;Executing Rust in Python using WebAssembly &amp;amp; PythonMonkey&lt;/a&gt; or &lt;a href="https://medium.com/@willkantorpringle/calling-c-function-from-python-using-webassembly-63a305248951"&gt;Calling C functions in Python using WebAssembly&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, PythonMonkey is an effective Python library for executing JavaScript in Python. With PythonMonkey you can call JavaScript from Python and vice versa all by using a Python Library. PythonMonkey can also be used to load JavaScript files or modules within Python and even execute WebAssembly code directly in Python.&lt;/p&gt;

&lt;p&gt;Check out PythonMonkey's &lt;a href="https://medium.com/@willkantorpringle/pythonmonkey-javascript-wasm-interop-in-python-using-spidermonkey-bindings-4a8efce2e598"&gt;release article&lt;/a&gt; and consider giving it a star on &lt;a href="https://github.com/Distributive-Network/PythonMonkey"&gt;the project's GitHub page to show your support&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Enjoy mixing JavaScript with Python using PythonMonkey!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
