0

I translate some JS files using js2py library. I need to get, from the newly created python files, a variable which has the same name in every file.

with os.scandir(path=r'path_to_folder') as folder:
       for file in folder:
            if file.name.endswith('.js'):
                new_name = os.path.basename(file) + ".py"
                path = os.path.basename(file)
                js2py.translate_file(path, new_name)
                print("Translated")
                print()

It works to translate every JS file into python, but I need those variables and importing from new_name('from new_name import var') doesn't work. Is there another way of getting those variables?

2
  • Does from new_name import * work? Commented Sep 3, 2020 at 18:28
  • I tried at first like that, but python doesn't recognize new_name. Now I try to translate js files in just one python file and import dynamically the variable, but it works only for the first js file. Commented Sep 7, 2020 at 10:21

1 Answer 1

1

I found this on Js2Py GitHub.

>>> print(js2py.translate_js('var $ = 5'))
from js2py.pyjs import *
# setting scope
var = Scope( JS_BUILTINS )
set_global_object(var)

# Code follows:
var.registers(['$'])
var.put('$', Js(5.0))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.