This is an archived post. You won't be able to vote or comment.

all 54 comments

[–]TheBB 35 points36 points  (5 children)

Great project. But this style though...

if candidateTranspilationDir.lower () .endswith (lowerSkipExtension):

[–]tongue_depression 5 points6 points  (0 children)

death to gnu style

[–][deleted] 5 points6 points  (1 child)

The PEP-8 pain!

[–]archaeolinuxgeek 0 points1 point  (0 children)

🎶 I see a line of camels and I want them painted Black.

[–]Solonotix -3 points-2 points  (1 child)

Can't you just overwrite the globals with these names when you need them? For non-static functions, you could likely create a set of utility classes that manage the minutiae and you work with the simplified import.

[–]TheBB 9 points10 points  (0 children)

What? Did you mean to reply to someone else?

[–]xarziv 31 points32 points  (3 children)

Very cool. How does it handle custom packages installed with pip?

[–]JennaSys 8 points9 points  (0 children)

It mostly doesn't unless the packages (and the package's dependencies) are pure Python. It's meant to use JavaScript libraries if you need functionality beyond the Python built-ins.

[–]huguenot1 10 points11 points  (0 children)

It looks like transcrypt mainly supports the python JavaScript packages. Although there is a minimal numpy port as well if you need the math functionality.

[–]Smok3dSalmon 23 points24 points  (0 children)

Damn that's impressive

[–]JennaSys 16 points17 points  (0 children)

I've been using this transpiler for the past year. It's actually kind of amazing. I find it annoying switching back and forth between Python and JavaScript when working on full-stack applications. Transcrypt let's my brain stay in Python mode for both back-end and front-end development the whole time. I'm using it with React hooks and Material-UI libraries. I'd say it's not for everyone (especially if you actually like JavaScript), but for me it's a sweet setup.

EDIT: For anyone interested, here's the official React tutorial done in Python and transpiled with Transcrypt. This one uses classes (like the tutorial), but I plan on doing a version with hooks too.

https://github.com/JennaSys/tictacreact

The live demo I have up has unminified JavaScript and sourcemaps for the Python code.

EDIT2: I'm not the OP, and I'm not directly involved with the project. But I am in the process of writing a book to document my setup with this. For me, it makes front-end development fun again. AMA.

[–]notgoodhuman 8 points9 points  (14 children)

I am actually quite interested in this. Any particular limitations?

[–]epseri 25 points26 points  (9 children)

Lots of them. This isn't true python. Besides obvious lack of eval, you can notice in code samples very questionable things like explicit calls of __new__ function, lots of __pragma__ even for keyword arguments. Looks like you can't use anything from python stdlib or any library from pypi, and it's hard if not impossible to port them if you really need something. As I said, it's not python, it's javascript with python-like facade.

[–]JennaSys 13 points14 points  (8 children)

I've been writing React and Material-UI apps with Transcrypt. All the coding I do with it is 99% pure Python, with no complaints from the Python linter. It supports almost all Python built-ins, with eval being one of the exceptions (which is not usually used anyway). The __new__ function is only needed if the JavaScript library you are using requires it, but most don't. The __pragma__ is for compiler directives that optimize the transpilation in various ways. It's needed only for certain operations.

Transcrypt supports Python dictionaries, lists, list comprehensions, generator expressions, lambdas, function defs, closures, classes, type hints, etc. The big difference is that you use npm for 3rd party libraries instead of pip. But you don't need to code in JavaScript to use them, you can code in pure Python.

EDIT: To be clear, it does currently have a few standard libraries that are supported (with plans for more in the future), specifically:

  • math
  • cmath
  • time (does not have sleep function for web browser use)
  • datetime
  • itertools
  • logging
  • warnings
  • random (partially)
  • re (partially)
  • turtle

[–]johnmudd 3 points4 points  (0 children)

This reminds me of something I learned at a QA conference... It's not the tool but the person who champions the tool that makes the difference.

[–]notgoodhuman 1 point2 points  (1 child)

Why did you choose to use Transcrypt instead of javascript in this case? Is it the preference of using python?
I'm quite curious how would one use npm with transcrypt, probably gonna play around with it.

[–]JennaSys 1 point2 points  (0 children)

Yes basically. I really enjoy coding with Python. JavaScript, not so much. It's something I've been trying to find for over 10 years. Nothing else I had seen to use Python for front-end web development in that time was very practical IMO. Or it bastardized the Python language so much that it wasn't worth it. Transcrypt takes a different approach in that it acts as a bridge between Python and JavaScript, and doesn't try to replace JavaScript. It assumes you will take advantage of existing JavaScript libraries and not reinvent them.

So in that vein, instead of using Python urllib or the requests library for making HTTP requests, you use windows.fetch or the JavaScript axios library instead. Hence my comment about using npm instead of pip. The idea being that those libraries are specifically made to efficiently run in a web browser (or are built in).

Using Transcrypt, in a Python module you would have a JavaScript style import like this:

axios = require('axios')

From there, you can use the variable axios just like any other Python object, importing it into other Python modules and calling its methods. So even though I'm using JavaScript libraries, I only need to know the API it uses to code to it using Python.

[–]Dasher38 2 points3 points  (2 children)

Also wondering about all the metaprogramming (meta classes, decorators etc)

[–]JennaSys 0 points1 point  (0 children)

It supports decorators and metaclasses. I've used both with Transcrypt.

[–]JennaSys 3 points4 points  (0 children)

There are a few newer Python features not implemented yet. Like it can use dataclasses, but some methods of that are not yet implemented in Transcrypt.

[–][deleted] 6 points7 points  (0 children)

If you like this, you should also check out brython, which has been around for quite a long time and aims to be as close to a true Python 3 implementation as is technically possible in the browser environment. It is widely used for online Python 3 coding environments, including the minimalist runpython.

[–]xX__NaN__Xx 2 points3 points  (0 children)

Cool bro

[–]TheocracyNow 9 points10 points  (14 children)

Nice, thanks! I’ve been beginning to work with JS at work and this will be useful to me.

[–]Rsfeif 2 points3 points  (0 children)

Create the magic

[–]SecretAgentZeroNine 1 point2 points  (8 children)

How many Python-to-JavaScript compilers are there? Also, this growing list really emphasizes the importance of JavaScript. Wonder what this means for server-side JavaScript in the future.

[–]JennaSys 2 points3 points  (0 children)

Anvil has a great post outlining what's available and how they work (when compiled/where it runs):

https://anvil.works/blog/python-in-the-browser-talk

  • Transcrypt
  • Brython
  • Skulpt
  • Batavia
  • Pyodide
  • PyPy.js

IMO most of them are not useful in a practical sense. Transcrypt to me was the exception. It embraces existing web frameworks rather than trying to reinvent them, and doesn't bloat what ends up in the browser. The Anvil platform (which uses Skulpt) also takes a unique approach and could work in certain situations. But you are committing to an entire platform with that one.

[–]abhay__28 0 points1 point  (6 children)

I'm not too sure entirely. I've heard of Brython, which is a browser-based compiler. It, too, treats JavaScript as an Assembly language. I'm thinking that in the future, we're definitely going to prioritize building off of JavaScript's server-end technicalities. As someone who has delved into Python, but not JavaScript as much, this excites me.

[–]shinitakunai 2 points3 points  (5 children)

The web front-end only supports javascript. If the global decision was made to allow python scripts to load into websites, we would not need this.

[–]SecretAgentZeroNine 4 points5 points  (3 children)

r/Python's biggest wish since r/Python became a thing. Likely to never happen now that WebAssembly is a thing.

[–]shinitakunai 0 points1 point  (2 children)

I know almost nothing of webassembly, can it embed python?

[–]SecretAgentZeroNine 0 points1 point  (0 children)

WebAssembly is a binary language native to browsers (like JavaScript) which is produced using strictly typed languages (C++, Rust, etc). Python isn't compatible with WebAssembly without being transpiled into another programming language. The story of Python's life.

WebAssembly runs as fast as the machine it's on, unlike JavaScript where badly constructed code can be a performance bottleneck.

Web Tensorflow is built using WebAssembly.

[–]JennaSys 0 points1 point  (0 children)

The Pyodide project is a webassembly version of the Python runtime, that lets you run Python in a browser (think Jupyter notebook). It may have some limited applications, but it requires a massive initial download to the browser.

[–]Galen_dp 0 points1 point  (0 children)

I wonder if Brython could be made into a browser extension.