Skip to content
fronthonPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

jsx2py

React components, TSX or JSX, written in Python for Reacthon.

export function TodoList({ title }: { title: string }) {
  const [items, setItems] = useState<string[]>([])
  const [draft, setDraft] = useState("")

  const add = () => {
    if (draft.trim() === "") return
    setItems([...items, draft.trim()])
    setDraft("")
  }

  return (
    <section>
      <h2>{title}</h2>
      <input value={draft} onChange={(event) => setDraft(event.target.value)} />
      <button onClick={add} disabled={!draft}>
        Add
      </button>
      <ul>
        {items.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
      <p>{items.length} to do</p>
    </section>
  )
}

becomes

from reacthon import use_state


def TodoList(title=None, **_):
    items, set_items = use_state([])
    draft, set_draft = use_state("")

    def add():
        if draft.strip() == "":
            return
        set_items([*items, draft.strip()])
        set_draft("")

    return t"""
        <section>
            <h2>{title}</h2>
            <input value={draft} onChange={(lambda event: set_draft(event.target.value))} />
            <button onClick={add} disabled={not draft}>Add</button>
            <ul>{[t'<li key={index}>{item}</li>' for index, item in enumerate(items)]}</ul>
            <p>{len(items)} to do</p>
        </section>
    """

a component of Reacthon, which React renders as it renders the TSX one. Each file becomes a .py of the same components, laid out as one writes Python by hand: its JSX a t-string, its imports from JavaScript a __js__, React's hooks Reacthon's, its own names in snake_case. TypeScript's own checker tells what each value is, a list, a text, a number, plain data or an object, and the Python keeps what JavaScript means where Python means something else: indexOf() is no index(), which raises, === compares an object by identity, ?? reads its left side once. What it cannot write, it names rather than guesses, and leaves the file out.

It writes Reacthon UI's components from shadcn/ui's, on its 24 presets, with their blocks and the pages of shadcn/ui's site, each rendered beside its TSX one and compared with it.

Convert a project

npx jsx2py src/components/team-card.tsx src/app

writes each TSX file it is given, or finds in a folder, a component or a page, in Python beside it, team_card.py, with the project's own tsconfig. JSX as well: .jsx, and a .js that holds JSX, as Create React App's do, its props typed by what the project hands them, as JavaScript does not type them. A folder gives its components and the files of the project they import, their data and hooks; its tests and stories, and what nothing imports, stay as they are.

An import of a file of the project that is Python, converted with it or before it, is Python's, from team_card import TeamCard, and shadcn/ui's @/components/ui/dialog the dialog.py that npx reacthon-ui add dialog writes, which it names if the project has not added it yet; a library's stays JavaScript's, in __js__. A .py already there is kept, as one may have changed it, unless --overwrite says.

From JavaScript, convertFiles(files) hands back each file's Python, or why it is left out:

import { convertFiles } from "jsx2py"

for (const { file, python, error } of convertFiles(["src/components/team-card.tsx"])) {
  // …
}

The helpers

What JavaScript means where Python means otherwise, the Python imports from jsx2py: index_of() as JavaScript's indexOf(), to_string() as its String(), splice(), at(), substring(), reduce(), keys() in an object's order. They are jsx2py/jsx2py.py, of the package, always at the version of jsx2py that wrote the calls, and its folder is one of those Reacthon finds Python in:

// vite.config.js
import { dirname } from "node:path"
import { fileURLToPath } from "node:url"

plugins: [reacthon({ path: [dirname(fileURLToPath(import.meta.resolve("jsx2py/jsx2py.py")))] }), react()]

shadcn/ui's cn() and cva(), class-variance-authority's, the Python imports from utils, as shadcn/ui's TSX imports them from @/lib/utils: jsx2py/utils.py, which npx reacthon-ui add writes into a project as src/lib/utils.py, its own, as shadcn/ui's is.

Licence

Copyright (C) 2026 Florent Gallaire [email protected]

jsx2py is MIT, the licence of React. See LICENSE.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages