Compiling components

Jx doesn't read your .jx files every time it renders them. It first turns each one into a Python file, and renders by running that file. This is what makes Jx fast, and it also lets Jx find many mistakes before a page is ever rendered.

You don't have to do anything for this to work. This page explains what happens, so nothing surprises you.

The generated files

For each component, Jx writes a Python file with the same name next to it:

components/
  card.jx
  card.py          <- written by Jx
  dashboard.jx
  dashboard.py     <- written by Jx

These files are made from your components, so they don't belong in your repository. Add them to your .gitignore:

.gitignore
components/**/*.py

If you would rather keep them out of your components folder, tell the catalog where to put them:

catalog = Catalog("components/", output="build/views")
components/
  card.jx
  dashboard.jx
build/
  views/
    components/
      card.py
      dashboard.py

While you develop

Just edit your components. Before each render, Jx checks if a component or anything it imports has changed, and compiles it again if so.

This is the auto_reload option of the catalog, which is on by default.

In production

Compile everything once when you deploy, and turn auto_reload off, so Jx doesn't check the files on every render:

app.py
catalog = Catalog("components/", auto_reload=DEBUG)
deploy step
from myapp import catalog

catalog.compile()

compile() compiles every component of the catalog. If any of them has an error, it tells you all of them at once, so a mistake stops the deploy instead of a page.

Tip

If you forget the deploy step, your site still works: a component that isn't compiled yet is compiled the first time it is rendered. Compiling ahead only moves that work, and its errors, to the deploy.

If you want to be sure that nothing is ever compiled in production, create the catalog with compiler=False. Then rendering a component that wasn't compiled is an error:

catalog = Catalog("components/", auto_reload=False, compiler=False)

Errors

A mistake in a component is found when it is compiled, with the file, the line and the column where it is:

components/page.jx:2:1:
  `<Card>` needs the argument `title` (card.jx)
components/page.jx:2:1:
  `<Card>` needs the argument `url` (card.jx)
components/menu.jx:1:1:
  Unclosed block: expected `{% endif %}` before the end of the file

These are some of the mistakes Jx finds this way:

  • An import of a file that doesn't exist.
  • A component that is used but was not imported.
  • A component used without an argument it requires.
  • A tag or a block that was never closed.
  • A filter that doesn't exist.
  • A variable named with a word that Python reserves, like class or return.

Jx raises two errors of its own. Both can be imported from jx:

  • CompileError: a component has a mistake. The message lists every one found.
  • ComponentNotCompiledError: a component has to be compiled and the catalog can't do it, because it was created with compiler=False.

To find these mistakes without rendering anything, for example in your tests or in CI, use jx check.