Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python to JavaScript converter [closed]

I want to convert some basic snippets to JavaScript, just pure Python I wrote myself to pure JavaScript. Is there any thing out there? Here the code.

items = init['items']

itemsKeyedById = {i["id"]: i for i in items} # hard to convert.

for item in items:
    if item["parents"][0]['isRoot'] == False:
        parent = itemsKeyedById[item["parents"][0]['id']]

        if "children" not in parent:
            parent["children"] = []
        parent["children"].append(item)

topLevelItems = [item for item in items if item["parents"][0]['isRoot'] == True] # hard to convert.
try:
    return json.dumps(topLevelItems[0]);  
except:
    return '[]'

@Davide: Sadly this question has been closed, otherwise I'd write an answer instead of a comment. The best way to do have python as client side these days is with https://github.com/qquick/Transcrypt

like image 508
Kivylius Avatar asked Mar 23 '14 19:03

Kivylius


People also ask

Is there a Python to JavaScript converter?

Jiphy. The Jiphy name is an abbreviation of “JavaScript in, Python out.” In other words, Jiphy converts in both directions between the two languages. Plus, code from both languages can be intermixed before being converted to either target language.

Can you connect Python to JavaScript?

JS2PY works by translating JavaScript directly into Python. It indicates that you may run JS directly from Python code without installing large external engines like V8. To use the module it first has to be installed into the system, since it is not built-in. To use the module it has to be imported.

How do you call a JavaScript file from Python?

All you need is to make an ajax request to your pythoncode. You can do this with jquery http://api.jquery.com/jQuery.ajax/, or use just javascript $. ajax({ type: "POST", url: "~/pythoncode.py", data: { param: text} }). done(function( o ) { // do something });


3 Answers

You can actually run a Python interpreter directly in JS thanks to emscripten.

The project is called empythoned:

Empythoned is a build script that uses Emscripten to compile CPython for use in a browser. It attempts to compile the main interpreter as a single small executable and the whole standard library as dynamically loaded libraries.

but be warned:

The project is in its infancy. Right now the core interpreter works very well, but many of the libraries either don't work at all or contain various bugs.

like image 164
DJG Avatar answered Sep 28 '22 12:09

DJG


You might want to look into RapydScript. It is actively maintained (as of Oct 2014) and comes with a couple of cute examples which actually work.

like image 23
Matthias Urlichs Avatar answered Sep 28 '22 13:09

Matthias Urlichs


You should try this:

http://gatc.ca/projects/pyjsdl/

It works fine with regular python and even supports pygame.

like image 30
Remolten Avatar answered Sep 28 '22 12:09

Remolten