Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Running your JS code in Python (sheetjs.com)
63 points by sheetjs on Dec 27, 2013 | hide | past | favorite | 17 comments


> I suspect it would be possible to build up a wrapper that emulates the entire NodeJS javascript API in PyV8

http://pyv8.googlecode.com/svn/trunk/demos/node.py

But I think node itself can be used with pyv8 since they are both v8. There may be some issues with the event loops but that's not an extremely hard problem. IIRC, node-webkit (or appjs?) hacked up the node event loop with the event loop from chromium.

Another neat pyv8 trick is this (incomplete..) commonjs implementation:

https://github.com/flier/pyv8/blob/master/demos/commonjs/com...

.. so you can import node (or other commonjs) modules into python.

Although this isn't pyv8, it's possible to get python/javascript bindings for the webkit js environment (shared variables/functions/objects):

https://gist.github.com/kanzure/6581415

I am eager to figure out how to do a similar trick with blink or servo.


As an aside, if you want to handle XLS files from Python, you should use the xlrd, xlwt, and xlutils modules (available at http://python-excel.org ).


The reason I brought https://github.com/SheetJS/js-xls to python is precisely because xlrd couldn't handle many of my XLS files


You don't need to escape slashes if you use a regexp string like this: r'no need to escape slash\n'.


It's called a "raw string" rather than "regexp string", though that is the common use for it.


The other common use is Windows paths since traditionally backslashes are used as the delimiter. (Obviously hardcoded paths in code aren't generally a great idea, but that's another story.)


Very cool! In this case, since the injected text will end in a newline, that approach will work (and it appears to work with r"""triple-quoted\'s"""

But I do see syntax errors if the bare slash is at the end:

    >>> r'slash at end\'
      File "<stdin>", line 1
        r'slash at end\'
                   ^
    SyntaxError: EOL while scanning string literal


Yeah, that's about the only restriction. From the docs:

Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).


I've been working with PyV8 for nearly 3 years, and while it's an impressive achievement, I've had to start using a custom version to maintain stability. One of the major issues with PyV8 currently is that is builds against V8's master branch, as opposed to a known stable version. This means it breaks from time to time due to the speed of V8 development. There are also not releases of PyV8 per se. It's been v1.0 for quite awhile now, despite a year or more worth of fixes since the bump from v0.9 to v1.0.

I would LOVE to see tighter integration of Python and JavaScript, but if you are planning on using PyV8 in any production code (as I am), I would think very carefully about it.


I've been trying to integrate PyV8 into our production environment and have been experiencing a lot of trouble with it as well. There are two main issues that are killing it for us - the first is the challenge of building it, as you mention. There's also a difficult-to-reproduce segfault issue that crops up sometimes when V8's GC runs.

Is your custom version published online anywhere? It would be great to build off the stability work you already seem to have done.


I've noticed the GC problem that you mention as well. To me it seems very much that when objects travel between V8 and Python is when problems begin to crop up. Sadly, I have yet to be able to fix this. I believe this issue captures it most concisely: https://code.google.com/p/pyv8/issues/detail?id=193

I'll have to see about publishing my work publicly, as I don't explicitly own it. My biggest piece of advice is to always build from the latest branched version of v8, rather than the trunk. Right now, that would be 3.23. The branched version tends to be a bit more stable than the trunk. Sadly, since the PyV8 maintainer works from the trunk, I've often had to build a few revs back to get things to compile if there was a major structural v8 change.

If you'd be willing to share the details of your build environment, I can see if my work is even applicable and go about getting the permission to publish what I've done.

BTW, I want to be clear that in no way am I disparaging the PyV8 maintainer. He's been very responsive and helped fix some problems that would have taken me weeks to fix on my own. The project simply isn't focused on stability at the moment.


There is also a PHP extension to use the V8 engine:

http://www.php.net/manual/en/book.v8js.php


Awesome. I think the last part of your blog post is worth doing some research on, could be successful.


fun yak shaving exercise. is there a practical reason why you might want to do this?


I wrote a chess playing service to teach myself app engine a while back, and I wanted to verify valid moves on the client before sending them to the server, as well as verify them on server.

I didn't want to write that logic in both javascript (for client) and python / go (for server) and create a suite of tests to attempt to verify that the server and client logic was equivalent.

I looked around for ways to either run (minimal) python functions in javascript or else run (minimal) javascript functions in python, but at the time didn't come up with anything workable (within the constraints of using GAE).

* no classes, objects, function attributes, prototypes, etc etc necessary, just chess move validity checking


I once wanted to control the generation of Python code from selenium tests. The selenium UI does this with javascript (it has javascript code that can convert a selenium test to Python). Instead of rewriting their whole conversion library from js to Python, I used spidermonkey to just run the javascript.

The end result was, I had a Python module that could take a recorded selenium test and output Python code to run that test. The only dependency was spidermonkey.


This could be the beginning of something great for Python. Imagine if all the node.js modules became available on the Python. I'd love to use Python over Javascript.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: