Upgrading from Jx 0.x
Jx 1.0 keeps the components you know: the imports, the {#def #}, the slots, attrs and the assets work the same. What changed is what is underneath.
In Jx 0.x a component was a Jinja template. In Jx 1.0 Jx has its own compiler, that turns each component into a Python function. Components render much faster, and many mistakes are found before anything is rendered. In exchange, the things that came from Jinja and are not part of Jx are gone.
This page lists everything you have to change.
Requirements
Jx 1.0 needs:
- Linux, macOS or Windows
- Python 3.14 or later
Steps
- Update the package:
pip install -U jxoruv add jx. - Update how you create the catalog. See The Catalog.
- Run
jx checkon your catalog. It lists every component that doesn't compile, with the file and the line. - Fix what it finds. Most of it will be in Your Components.
- Add the generated files to your
.gitignore, and compile when you deploy. See New Things to Do.
The Catalog
Options That Are Gone
| Option | What to do |
|---|---|
jinja_env |
There is no Jinja environment. Pass the functions and values your components use as globals, and your filters with filters |
extensions |
Jinja extensions don't work. For a block tag of your own, use custom tags |
tests |
There are no tests. See Tests |
file_ext |
Components always end in .jx |
bytecode_cache |
Not needed: the components are compiled to Python files |
asset_resolver |
Asset URLs are rendered as you write them |
Warning
Passing one of these options is not an error. Any argument the catalog doesn't know becomes a global, so Catalog("components/", jinja_env=env) runs and does nothing with it. Remove them by hand.
Methods That Are Gone or Renamed
| In Jx 0.x | In Jx 1.0 |
|---|---|
catalog.has(relpath) |
catalog.has_component(relpath) |
catalog.add_folder(path, prefix=..., assets=...) |
catalog.add_folder(path). See Prefixes |
catalog.add_package(...) |
Gone. See Prefixes |
catalog.list_components() |
Gone |
catalog.get_signature(relpath) |
Gone |
catalog.collect_assets(output) |
Gone |
catalog.jinja_env.globals[...] |
catalog.globals[...] |
catalog.jinja_env.filters[...] |
catalog.add_filters({...}) |
Prefixes
Prefixes and installable packages of components don't exist anymore. An import like this one is an error:
{#import "@ui/button.jx" as Button #}
Add the folder to the catalog without a prefix, and import its components by their path:
catalog.add_folder("components/")
catalog.add_folder("vendor/ui/")
{#import "button.jx" #}
All the folders share the same paths. If two have a file with the same path, the one of the folder added first is used, so you may have to move the components of a library into a subfolder with its name.
Errors
jx.JxException and most of the errors that inherited from it are gone. These are the errors of Jx 1.0:
| Error | When |
|---|---|
jx.CompileError |
A component has a mistake: its syntax, an import, a missing argument... It replaces TemplateSyntaxError, InvalidImport, MissingRequiredArgument, DuplicateDefDeclaration and InvalidArgument |
jx.ComponentNotFoundError |
There is no component with that path |
jx.ComponentNotCompiledError |
A component has to be compiled and the catalog was created with compiler=False |
jx.InvalidPropType |
An argument has a value of the wrong type |
An error that happens while a component renders, like using a name that doesn't exist, is the error of Python (KeyError, AttributeError, TypeError), with the file and the line of the component in the traceback.
_get_random_id
The global _get_random_id() was replaced by dom_id():
{# Before #}
{% set input_id = id or _get_random_id("input") %}
{# After #}
{% set input_id = id or dom_id("input", random=True) %}
Your Components
What you write in a component still looks like Jinja, but only the parts listed in Syntax exist. These are the changes you are most likely to need.
Names That Don't Exist
In Jinja, a name that doesn't exist is an empty value. In Jx it is an error:
{# Before: renders nothing if there is no `subtitle` #}
{% if subtitle %}<p>{{ subtitle }}</p>{% endif %}
{# After #}
{% if is_defined(subtitle) and subtitle %}<p>{{ subtitle }}</p>{% endif %}
Usually the best fix is to declare the name as an argument with a default: {#def subtitle="" #}.
Remember that an argument that is not in the {#def #} is not a variable: it is only in attrs.
Tests
Jinja's tests are gone. is is now the one of Python:
| Before | After |
|---|---|
x is defined |
is_defined(x) |
x is none |
x is none (it still works) |
x is odd, x is even |
x % 2 == 1, x % 2 == 0 |
x is divisibleby(3) |
x % 3 == 0 |
x is string, x is number... |
Check it in Python, before rendering |
x is in(items) |
x in items |
Filters
Some filters have the name they have in Python:
x | lengthandx | countare nowx | lenx | stringis nowx | str
These don't exist. Using one is an error when the component is compiled:
select,reject,selectattr,rejectattr: add a condition to the loop,{% for x in items if x.active %}map(attribute="name"): useitems | attr("name")items: usedata.items()format: use"{} items".format(n)center: usetext.center(80)filesizeformat,urlize,xmlattr,pprint: add your own withfilters
See the list of filters for the ones Jx has.
Statements
| Before | After |
|---|---|
{% include %}, {% extends %}, {% block %} |
Use components and layouts |
{% call %} |
The content or the slots of a component |
{% with a = 1 %} |
{% set a = 1 %} |
{% set x %}...{% endset %} |
Only {% set x = value %} |
{% set ns = namespace(n=0) %} |
Not needed: a {% set %} in a loop changes the variable made before it |
{% break %}, {% continue %} |
A condition in the loop |
Functions of Jinja
The globals Jinja added, like dict(), lipsum(), cycler() or joiner(), don't exist. range() is the only one that stays. If you need another, pass it as a global of the catalog.
Escaping
Before, the values were always escaped. Now it depends on the name of the file: it is on for .jx, .html.jx and .xml.jx, and off for the rest, like .txt.jx. If you have components that are not HTML and counted on the escaping, see Escaping.
Names Reserved by Python
A variable of a loop or a {% set %} can't be named with a word that Python reserves, like class, return or import.
New Things to Do
Ignore the Generated Files
Jx writes a .py file next to each component. Add them to your .gitignore:
components/**/*.py
Or tell the catalog to write them somewhere else, with output. See Compiling components.
Compile When You Deploy
In production, compile everything once before the app starts, and turn off auto_reload:
catalog = Catalog("components/", auto_reload=False)
catalog.compile()
See Compiling components.
New Things You Can Use
- Imports without
as:{#import "user-card.jx" #}gives you<UserCard>. The form withasstill works. See Imports. slot_filled("name"), to know if a slot was filled.- Custom tags, like
{% cache %}...{% endcache %}. pure, to call a function likeurl_foronly once.- Mistakes found when compiling: a missing argument, a component that was not imported or a filter that doesn't exist are found by
jx checkwithout rendering anything.