GUIDE / START

Anatomy of a .janel file

The three sections of a component, the rules each one follows, and how the template forms a tree.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 04 / 29~3 min read · try examples at your own pace
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

What you will learn

  • The three sections of a .janel file and which ones are required.
  • When the script runs, and what it may contain.
  • How the template forms a tree with a single root.
  • Where <window> may and may not be used.

One file, up to three sections

A .janel file describes one component: a named piece of interface together with its logic. It has up to three sections, in any order. Only the template is required. Keeping logic, structure and appearance in separate sections lets you read each one on its own.

SectionRequiredRules
<script lang="python"> … </script>NoAt most one. lang is optional and only python is accepted.
TemplateYesEverything outside script and style. It must have exactly one root element.
<style> … </style>NoAt most one. A supported subset of CSS-like properties.

The script runs once per instance

An instance is one use of a component. When an instance is created, its script runs once, in a fresh namespace of its own. Two instances of the same component never share their local state. Clicks and other updates do not run the script again; they call the functions it defined.

The script is ordinary Python. It can import any library and any module of your project: the src folder is on the import path, so from services.users import display_name works. Syntax errors are reported when the file is compiled, with file, line and column.

src/App.janel
<script lang="python">
from janelkit import state
from services.users import display_name   # src/services/users.py

name = state("")
</script>

<column padding={24} gap={12}>
  <textfield bind:value={name} placeholder="Your name" label="Your name" />
  <text>Hello, {display_name(name)}</text>
</column>
Copy

Complete file · Save as src/App.janel.

How it works

  1. services/users.py is an ordinary Python module with no interface. The starter project includes it.
  2. name = state("") runs once, when this component is created.
  3. The <text> calls display_name with the current name every time name changes. The script itself does not run again.

What you should see

In a project created with janel new, the window first shows Hello, invitado (the starter’s default name, in Spanish). Typing ada lovelace changes it to Hello, Ada Lovelace.

Common mistakes

  • Code at the top of the script runs once. Put work that must happen on a click inside a function connected to that click.
  • The script is trusted code with your program’s permissions. Do not load .janel files from untrusted sources.

The template is a tree with one root

Elements contain other elements, like folders inside folders. An element that contains others is their parent; the elements inside are its children. Indentation only helps people read; the tags decide the nesting. A paired tag such as <column> … </column> encloses its children; an element with no children can close itself with />.

The template must have exactly one root, the element that contains everything else. Comments are written <!-- like this --> and do not appear in the window.

janel
<!-- Window is the root; everything else is inside it. -->
<window title="A tree" width={360} height={220}>
  <column padding={24} gap={8}>
    <text>First child of the Column</text>
    <row gap={8}>
      <button>Left</button>
      <button>Right</button>
    </row>
  </column>
</window>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

How it works

  1. <window> is the root. It has one child, a <column>.
  2. The <column> has two children: a <text> and a <row>.
  3. The <row> has two children, the buttons, placed side by side.

What you should see

A window with a line of text and, below it, two buttons next to each other.

<window> belongs only to the main component

The root component of the application, src/App.janel, may use <window> as its root to set the title and size. If it does not, JanelKit wraps it in a 640 by 480 window and the content fills it. Components used inside other components must not have <window> as their root: they become part of a window, they do not open one.

To open a second window, use open_window, covered with navigation.

janel
<script lang="python">
from janelkit import state
count = state(0)
</script>

<!-- No Window here: JanelKit opens a 640 × 480 window for it. -->
<column padding={24} gap={12}>
  <text>Count: {count}</text>
  <button on:click={count += 1}>Add one</button>
</column>
Copy

Complete component · Save as src/App.janel in a scaffolded project.

What you should see

A 640 by 480 window with the count and an Add one button.

Learn moreWindows and screens ↗

The style section is local to the component

Style rules apply to the elements written in this component’s template, never to the inside of another component. A rule such as .page { padding: 24px; } matches every element with class="page". Note the dot: it belongs in the rule, not in the class attribute.

janel
<column class="page">
  <text>Styled by the rule below</text>
</column>

<style>
.page { padding: 24px; gap: 12px; }
</style>
Copy

Fragment · Add it to an existing component; it is not a complete file.

Learn moreStyles and themes ↗

Practice: find the parts

Open the src/App.janel that janel new created. Identify the script, the root element and the style section. Then open src/components/Counter.janel: its root is a <row>, not a <window>, because it is used inside App.janel.

Common mistakes

  • Two script or two style sections in one file are an error. Merge them.
  • Two top-level elements are an error. Wrap them in a <column> or <row>.
  • A <window> inside a component that another component uses is an error.

Key points

  • A .janel file has a template and, optionally, one script and one style section.
  • The script runs once per instance; later updates call its functions.
  • The template is a tree with one root; only the main component may use <window>.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.