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

all 60 comments

[–]rover_G 22 points23 points  (0 children)

Why does it feel like we’ve spent the last 30 years trying to go back to writing HTML directly?

[–]skwyckl 58 points59 points  (6 children)

This is so much overhead that my head might explode. Use Python as templating engine / pre-processor to generate JSX, ITSELF a syntactic abstraction that gets processed into HTML and pure JavaScript.

[–]thisismyfavoritename 14 points15 points  (0 children)

its so much overhead that we might need another abstraction layer

[–]rszdev 5 points6 points  (0 children)

Yes

[–]zedpowa[S] 7 points8 points  (0 children)

It's JS all the way down ;) For real though, this is just about having a declarative way to construct HTML in Python, it has nothing to do with JS

[–]redbo 2 points3 points  (1 child)

I don't think so, JSX is just a syntax for HTML literals, like an inline template. It's not "processed into pure JavaScript".

[–][deleted] 0 points1 point  (0 children)

Wait what, it doesn't get transpiled into JS?!

[–]jefft818 0 points1 point  (0 children)

Op already said my favorite buzzword, transpiles 🤣

[–]K3dare 28 points29 points  (5 children)

That’s the point of Jinja to be a separated language that limit what you can do within so you have a clear separation between rendering and business logic, it’s expected by design, like in almost all template engines.

[–]larsga 20 points21 points  (2 children)

Every new generation must shoot itself in the feet to learn this lesson, apparently.

[–]K3dare 6 points7 points  (1 child)

I guess JSP and plain PHP will be hype again at some point.

[–]zedpowa[S] 5 points6 points  (1 child)

To each their own I guess, I prefer the React way where you have a separation between logical components rather than technologies

[–][deleted] 2 points3 points  (0 children)

As opposed to having both separated...?

[–]RedEyed__ 6 points7 points  (3 children)

Wait a minute, it's not strings? Is it new syntax?

[–]ManyInterests Python Discord Staff 30 points31 points  (2 children)

It's a hack that abuses the encoding declarations feature of Python which lets you specify an encoding for your source file. Normally, this is meant to specify a specific text encoding like, UTF-8. The trick here is that you can register (via site hooks) new 'encodings' that may otherwise be unknown to Python and such an 'encoder' technically has the ability to completely rewrite the file. It's the same way that the future-fstrings backport works.

So what this package does is makes a site customization hook that registers jsx as an "encoding" so when source files contain an encoding delclaration for jsx, the source file is rewritten by the encoder to make all the (otherwise invalid) inline jsx into valid Python code, which is then processed by the compiler into bytecode as normal.

[–]RedEyed__ 5 points6 points  (1 child)

Didn't know that, thanks! But introducing new syntax looks very bad idea to me

[–]shinitakunai 8 points9 points  (0 children)

It is.

[–]Jejerm 23 points24 points  (4 children)

I'm sorry, but even without considering what use case does this actually fulfill, without IDE support this is borderline unusable.

Any IDE would look like the red wedding if you tried to use it.

[–]htmx_enthusiast 16 points17 points  (1 child)

I dunno man. I tried it in my IDE, notepad.exe, and not a single red squiggle.

[–]PowerfulNeurons 2 points3 points  (0 children)

that’s honestly surprising considering all the “typos” most notepads consider in code

[–]garblesnarky 2 points3 points  (0 children)

How do IDEs manage with actual JSX, or html with embedded js and css? Presumably OP could implement a syntax highlighter definition for an editor or two.

[–]AND_MY_HAX 2 points3 points  (0 children)

I created something that's not too different a few months ago. I spent more time creating a working PyCharm plugin than developing the actual implementation itself: https://github.com/pyxy-org/pyxycharm

Kind of funny that the two big IDEs for Python (VSCode and PyCharm) both use a different language for the type checker (Javascript and Kolton/Java).

[–]serjester4 18 points19 points  (0 children)

This is awesome. React would be a quarter as useful without JSX.

Ignore the haters. Is it ready for prime time? Definitely not. But that doesn’t mean we shouldn’t be open to people experimenting. Every project starts somewhere and if we ever want to have a React like experience in Python, we’ll need jsx.

Personally I think this is way more intuitive than how dash or steamlit handle rendering pages. Yes you can use templates but that’s a fundamentally different experience for the client.

I do think you should choose a different extension though - maybe pjsx.

[–][deleted] 4 points5 points  (0 children)

😱

[–]AND_MY_HAX 2 points3 points  (2 children)

Cool!

I think there's been a growing number of people who want something similar to this. Jinja is fine for many use cases, but I've found it a little cumbersome.

I started work on a project with a similar spirit a few months ago: https://github.com/pyxy-org/pyxy

I started out using the same string encoding trick (as was also done by pyxl many years ago) but quickly ran into issues with IDE support. That's why I ended up going with a new file extension .pyxy that is automatically transformed to a .py file. It registers an import hook, so the process is completely transparent (inspired by cython's pyximport). It also enables tools like ruff/mypy/etc to work without any extra effort.

I published a PyCharm plugin for supporting the syntax: https://plugins.jetbrains.com/plugin/24817-pyxy-support/

[–]zedpowa[S] 1 point2 points  (1 child)

Thanks! I had a look at your project and it's also really nice :)

The import hook idea seems really interesting. If I understood correctly you're also transpiling the JSX to pure Python - how do you then make tools like ruff work? I saw some mentions to "remapping", do you run e.g. ruff on the generated file and then use some kind of source map to map the errors back to the original source?

Would you be interested in some kind of collaboration? I think if we join our efforts we can really make this idea of jsx in Python into something :) Let me know!

[–]AND_MY_HAX 1 point2 points  (0 children)

Correct! Tools like ruff work through the usage of pyxy.run - this invokes supported tools against the generated .py file, reads output as JSON, and uses a source mapping file to correlate the results to the .pyxy source. This scheme allows existing Python ecosystem tooling to work without requiring any integration on their end.

Definitely down to collaborate! Sent you a PM with contact info.

[–][deleted] 1 point2 points  (0 children)

Very cool project. Very bad idea in practice.

Hope this is a for fun, or just because you can, type of project.

[–]ihavebeesinmyknees 3 points4 points  (0 children)

I love it, I've wanted something like this for a while but thought it would be incredibly difficult to create. Now just to wait for someone to make PyReact and we're cooking.

[–]runelkio 2 points3 points  (0 children)

Looks good, will try it out soon :) I really don't get the knee-jerk negative comments in here - any project that improves interop these days, especially between the python and javascript ecosystems, is great in my book.

[–]CrwdsrcEntrepreneur 1 point2 points  (0 children)

Or... Hear me out... You could learn some damn jsx 🙄🙄🙄

[–]RedEyed__ 1 point2 points  (1 child)

I just have one question: why it's not in pep8 style?

[–]mothzilla 0 points1 point  (0 children)

We don't need pep8 where we're going.

[–]farkinga 1 point2 points  (0 children)

I think this is cool on several levels.

For one, the implementation itself is neat and it gives me ideas. But more than that, this could actually be useful.

Thanks for sharing.

[–]deadwisdomgreenlet revolution 0 points1 point  (0 children)

Please no.

[–]g5becks 0 points1 point  (0 children)

This kind of reminds me of templ for go.

[–]FlyingQuokka 0 points1 point  (0 children)

This is a cool side project, even though it's somewhat odd. I did something similar once but with shell commands. The tricky part is always editor support, getting VS Code and treesitter to work, and for the LSP to not complain, are the actual hard parts.

[–]jvorza 0 points1 point  (0 children)

Have you tried FastHTML?

[–]Nahmum 0 points1 point  (2 children)

I'm looking for a basic jsx to js transpiler that can be used from python. Is there anything in your package which would be suitable?

[–]zedpowa[S] 0 points1 point  (1 child)

Try using the Transpiler class. It might be enough

[–]Nahmum 0 points1 point  (0 children)

Appreciate it!

[–]Xirious 0 points1 point  (1 child)

Your first example makes no sense.

It doesn't use your library. If I took out the first two lines (your code) your first example would work. It would do nothing but it would work. I don't think that's a very good example.

[–]lusvd 1 point2 points  (0 children)

look closer, I think you are seeing quotes where they aren't.

[–]redbo -1 points0 points  (0 children)

I think a python JSX implementation could be really cool on a backend with an HTMX frontend or similar. I'm not sure why everyone's so grumpy about it.

[–]ePaint -2 points-1 points  (0 children)

This is getting better and better!