12

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

Kivylius
  • 6,357
  • 11
  • 44
  • 71
  • 2
    yuo could almost feed that to coffeescript, but why not just retype it in JS? nothing is so majorly different that you'de need to rewrite the logic, it's just syntax adjustments. ok, ok, if you want to support IE, then you need coffeescript or to convert the array comprehension, but that one is easy enough to write as a for-loop. – dandavis Mar 23 '14 at 19:58
  • I can rewriter most of it but lines like `topLevelItems = [item for item in items if item["parents"][0]['isRoot'] == True]` I would not know how to rewrite as I'm not that great at python and coffeescript wont convert it. – Kivylius Mar 23 '14 at 20:37
  • 3
    topLevelItems = items.filter(function(item){ return item.parents[0].isRoot;}); if topLevelItems is an object, just grab its values using an obj>arr util method. also, underscore might make it simpler to implement xbrowser. – dandavis Mar 23 '14 at 22:17
  • One use case for a converter is to implement the same logic on the server (Python) and the client (Javascript). In our case we use it for example for calculation the shipping cost: this isdone in the client for display purposes, but prone to manipulation. So final shipping costs are calculated on the server. It would be bad to maintain two separate shipping cost calculation codebases. – max Aug 26 '15 at 20:42
  • See also https://stackoverflow.com/questions/1873135/python-to-javascript – Cees Timmerman Jul 28 '17 at 09:40
  • 1
    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 – Davide Feb 20 '20 at 17:19

3 Answers3

5

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.

DJG
  • 6,413
  • 4
  • 30
  • 51
  • 1
    I just wnat to convert the python code to javascript code and thats it, I think this is running the python in javascript? And i have no idea how to use it. Is there a tutorial? – Kivylius Mar 23 '14 at 19:52
  • @CezarisLT `empythoned` is an `emscripten` project, so you would first have to follow the guide on `emscripten` and use that to compile the Python interpreter, `empythoned`, to Javascript. I tried searching for a precompiled Python in JS, but didn't find one. Anyway, if what you posted is the only thing you are trying to convert, it would be easier to simply rewrite it in Javascript, or Coffeescript as suggested by @dandavis. – DJG Mar 23 '14 at 20:18
  • 1
    See here for a more comprehensive list of Python-to-JavaScript converters: https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js#python – Anderson Green Nov 02 '14 at 04:44
2

You should try this:

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

It works fine with regular python and even supports pygame.

Remolten
  • 2,614
  • 2
  • 25
  • 29
2

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.

Matthias Urlichs
  • 2,301
  • 19
  • 29