4

I have a special case in some application code where I need to run some non-trivial validation/math on the frontend (for responsiveness) as well as on the backend (for data integrity). I want to make sure they're in sync, and not write the code twice. Is there a way to compile something to both python and javascript?

I'm currently looking at Transcrypt (python -> javascript transpiler), but the main problem is that it doesn't have very much of the stdlib available, and I'm not sure how to go about stubbing out my own versions. There are also some footguns that would be annoying to dance around, like changing methods called "get" to "py_get".

I've also seen Js2Py, which takes the opposite approach of running javascript inside python. This seems potentially better, since I could write my shared code as a library using regular old javascript tooling, then import it and call it from python.

So here's what I'm looking for:

  • I want to be able to write a stateless application library in a single language. Clojure, python, javascript preferred, or a common business logic DSL that has bindings in both languages.
  • I want to compile to both python and javascript, and run my tests in both places to make sure it all worked.
  • I want it to be fast in both places (I currently call a pure js library from python using subprocess, but it's kinda slow).
  • I want access to the source language's stdlib (short of browser apis/system apis), e.g. unittest, functools. Additional libraries are a plus.
jstaab
  • 3,449
  • 1
  • 27
  • 40
  • 1
    Don't know if brython would be useful to you, if it allows access to standard lib imports, a quick look into the FAQ I would say it does... http://brython.info/ – progmatico Feb 01 '18 at 22:20
  • I did see Brython, but it's very focused on running python for building a web application frontend; I don't need all the browser context, custom ajax-specific dependency resolution, etc. I just want something that compiles to a vanillajs module I can import with node/webpack, and that runs unmodified in python (3.6). – jstaab Feb 01 '18 at 22:42

1 Answers1

0

What I ended up doing was using javascript, running it via nodejs via python's subprocess module, and calling it over http with requests. Quick and dirty! Lots of small things to deal with on that end, including restarting dead processes, etc.

This did the trick, but the particularities of my use case meant it gave me no end of trouble. In the end, I ended up taking the logic out of the frontend and using an ajax request to fetch the calculation from the server. The latency is very small, so the frontend remains responsive enough.

jstaab
  • 3,449
  • 1
  • 27
  • 40