Introduction
Jx is a Python component library. A component is a .jx file: a small piece of HTML with a header that declares its arguments and dependencies. Components import each other and compose into pages. The output is plain HTML.
If you've used Django or Flask templates, the syntax in the body will feel familiar: Jx uses Jinja's syntax, so {% if %}, {% for %}, {{ value }}, and many of the filters work.
Jx compiles each component to a Python function, so rendering a page is calling plain Python code. The new bits are the header (a few {# ... #} directives at the top of the file) and the way components are invoked (as HTML tags, not via {% include %}).
Here's the smallest useful Jx component:
{# myapp/views/components/card.jx #}
{#def title #}
<article class="Card">
<h3>{{ title }}</h3>
<div class="Card-body">{{ content }}</div>
</article>
And here's a page that uses it:
{# myapp/views/home/show.jx #}
{#import "components/card.jx" #}
<Card title="Welcome">
Glad you're here.
</Card>
The page produces:
<article class="Card">
<h3>Welcome</h3>
<div class="Card-body">Glad you're here.</div>
</article>
Two things to notice. First, the component is invoked as <Card> rather than {% include %} or {{ card() }} - Jx's parser recognizes capitalized tag names and treats them as components.
Second, {{ content }} inside the component renders whatever was between the open and close tags - the same idea as a slot in Vue or {% block %} in Django, but driven by tag nesting rather than inheritance.
What Jx Does Not Do
A few HTML-shaped things you might expect that Jx leaves alone:
- No CSS scoping. A component named
Carddoesn't get its CSS auto-scoped to itself; you write your selectors yourself. - Prop types are checked, but only the built-in ones.
#def title: stris checked withisinstancewhen the component renders (and at compile time when the value is a literal); required vs optional is enforced too. - No client-side reactivity. Jx is a server-side rendering library. To re-render parts of a page in the browser, pair Jx with htmx or Turbo Frames - both work because the component output is plain HTML.