Run Javascript from Python

Subscribe to my newsletter and never miss my upcoming articles

Hello Reader, hope you and your family is in good health!

Javascript is a very powerful language, honestly, I am a baby in Js but I have always wondered, can Js integrated with Python. Recently, I was surfing the Internet and Js2Py module appeared in my Google Search.

I am attending a Technical Writing Bootcamp at @hashnode. Hashnode Bootcamp II is a free virtual Bootcamp to help beginner technical writers to improve their writing skill.

This article is inspired by the latest session by Chris, @dailydevtips, he suggested to write about something that I have encountered and googled in past.

In this Blog article, we will learn how to Run Javascript commands from inside Python script . We will make use of the Js2Py module.

Check out the Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video Tutorial for better Understanding and a step by step Guide of the same.

What will be covered in this Blog

1. Js2Py Introduction
2. How to Run Javascript commands from inside Python script

Js2Py Introduction:

You can translate JavaScript to Python using Js2Py. Js2Py is a Python module which can used to translator JavaScript to Python & acts as a JavaScript interpreter written in 100% pure Python.

  • It translates any valid JavaScript (ECMA Script 5.1) to Python.
  • Translation is fully automatic.
  • Does not have any dependencies - uses only standard python library.

Limitations:

It has three limitations:

  • strict mode is ignored.

  • with statement is not supported.

  • Indirect call to eval will is treated as direct call to eval (hence always evals in local scope)

If you wish to know more about it, you can refer to Js2Py Documentation. Use this link to navigate to the documentation.

Now that you are aware of Js2Py basics, we can move forward to the coding section. Let's get started!

Time to Code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

code.png

In order to access the Python library, you need to install it into your Python environment, use the following command to install Js2Py

pip install js2py

Now, let's import the package in our Python script.

import js2py

Let's start with a simple Hello World!.

#example 1
#a js command
js1 = 'console.log( "Hello World!" )'

res1 = js2py.eval_js(js1)

Let's look at the Equivalent python code:

print('Hello World!')

So here, we stored the javascript command in js1 . We will make use of eval.js method from js2py module here, pass in our js code in it and store it in res1. Finally we are output our result.

#print the result
res1

#OUTPUT: Hello World!

Now, let's look at another example! Let's create a addition function in Javascript.

#exapmle 2
#a js function
js2 = '''function add(a, b){
    return a + b;
    }'''

Let's look at the Equivalent python code:

def add(a, b):
    return a+b

Let's store it in js2 and in order to make our program more interactive, let's take the input from user inside the python script.

a = int(input('Enter a num: '))

We will make use of eval.js method from js2py module here, pass in our js function in it and store it in res2.

res2 = js2py.eval_js(js2)

Finally, let's print out our result. So, save and run the python script.

print(res2(a,3))

And it will output the addition of two numbers. It will vary as it depends upon the user input.

This is a basic working example of Js2Py. This is all about the Running Javascript commands from inside Python script. That's it!

Simple, isn't it? Hope this tutorial has helped. I would strongly recommend you to Check out the YouTube video of the same and don't forget to subscribe my Channel.

You can play around with the Js2Py library and even explore more features.

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Thank you for reading, I would love to connect with you at Twitter | LinkedIn.

Do share your valuable suggestions, I appreciate your honest feedback!

You should definitely check out my other Blogs:

Resources:

See you in my next Blog article, Take care

Chris Bongers's photo

Nice article!

Love the use of something you googled into a real-world article, and I didn't even know you could use JS like that 👀

Tapas Adhikary's photo

Good... I may end up learning Python same way! Some day..😀

Ayushi Rawat's photo

Thank you Tapas

Diana Chin's photo

Pretty neat! I'm working with both JavaScript and Python so this is helpful :)

Ayushi Rawat's photo

Glad you liked it

Deactivated User's photo

Thanks for sharing this Ayushi ! Really nice trick to run Javascript from #python platform...