GUIDE / FUNDAMENTALS

Expressions and attributes

What you can write inside braces, the forms an attribute can take, and how to format values.

Core janelkit Included with janelkit. Nothing extra to install.
CHAPTER 07 / 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

  • Which Python is allowed inside {…}, and why JavaScript is not.
  • The difference between a literal, an expression, a Boolean attribute and a shorthand.
  • How to format numbers and text like an f-string.
  • What happens when an expression has a mistake.

Braces hold one Python expression

An expression is Python that produces a value: count * 2, len(items) > 0, "Ready" if ready else "Wait". Inside braces you can write any expression, including calls to your own functions. You cannot write statements such as assignments, if blocks or loops; those belong in script functions, or in event actions, which are covered in Events.

Expressions are evaluated in Python, in the component’s namespace. The generated QML never runs your logic. The table translates the habits people bring from JavaScript.

IntentionWrite (Python)Not (JavaScript)
Equala == ba === b
And, or, nota and b, a or b, not aa && b, a || b, !a
Choose a value"Yes" if ok else "No"ok ? "Yes" : "No"
True, false, nothingTrue, False, Nonetrue, false, null
Lengthlen(items)items.length

Four ways to write an attribute

An attribute sets a property of an element. Quoted values are text. Braces hold an expression: if it is a fixed Python literal such as {420}, it is written into the window once; otherwise it becomes a binding that updates when its data changes. A bare attribute is True, and {name} is short for name={name}.

FormExampleMeaning
Literaltitle="Notes"Text. Numeric properties also accept width="640".
Expressionwidth={420}, enabled={count > 0}A Python value. Reactive when it reads state.
BooleanflatThe same as flat={True}.
Shorthand{enabled}The same as enabled={enabled}.
janel
<!-- count and enabled are defined in the script. -->
<button text="Save" enabled={count > 0} />
<button flat>Quiet action</button>
<button {enabled}>Continue</button>
Copy

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

Common mistakes

  • enabled="False" is the text "False", not a Boolean. Write enabled={False}.

Which names are unwrapped

A plain name that refers to state, a computed value or a component prop is replaced by its value: {count + 1} means count.value + 1. Only direct names are unwrapped. If a state lives on another object, such as a module, reach its value explicitly with .value, or import the state by name so it becomes a direct name.

Names are looked up when the expression runs. Inside a row of a list, a lambda always sees that row’s item.

janel
<!-- prefs is a module that defines theme = state("light"). -->
<text>Theme: {prefs.theme.value}</text>
Copy

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

Common mistakes

  • Inside a template lambda, state names are already numbers or strings, so lambda: count.set(0) fails. Pass a script function instead.

Format values like an f-string

Text interpolation accepts Python format specifications and conversions, exactly as in an f-string: {price:.2f}, {total:,}, {ratio:.0%}, {name!r}, {n:>4}. None is shown as empty text. Colons inside brackets, parentheses or strings do not start a format, so {items[1:3]} works; a lambda needs parentheses.

janel
<script lang="python">
from janelkit import state
price = state(1234.5)
ratio = state(0.256)
name = state("Ada")
</script>

<window title="Formatting" width={380} height={240}>
  <column padding={24} gap={8}>
    <text>Price: {price:,.2f}</text>
    <text>Share: {ratio:.0%}</text>
    <text>Name: {name!r}</text>
    <button on:click={price += 100}>Raise the price</button>
  </column>
</window>
Copy

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

How it works

  1. :,.2f adds thousands separators and keeps two decimals.
  2. :.0% multiplies by 100, rounds to whole numbers and adds the percent sign.
  3. !r shows the value as Python would write it, here with quotes.
  4. Formatting only applies to text between tags, not to attribute expressions.

What you should see

Price: 1,234.50, Share: 26% and Name: 'Ada'. Raise the price changes the first line to Price: 1,334.50.

What happens when an expression is wrong

A name that the script does not define is a compile error that points at the line and suggests the closest name. Diagnostics are written in Spanish for now, for example: 'cuont' no está definido — ¿quisiste decir 'count'? ('cuont' is not defined, did you mean 'count'?). This check is turned off if the script uses from x import * or globals(). An expression that raises an exception while running, such as formatting text with .2f, is logged with its file and line, and the application keeps running.

Expressions are trusted code, like the script. Never build template source from text the user typed.

  1. Misspell a state name in a {…} and run the app: read the suggestion in the terminal.
  2. Write {"Discount" if price > 15 else "Regular price"} in a <text> and change price to cross 15.

Learn moreErrors and troubleshooting ↗

Key points

  • Braces contain one Python expression; statements belong in functions.
  • Quotes are text, braces are Python, a bare attribute is True.
  • State names are unwrapped; text can be formatted like an f-string.

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