Fields and widgets¶
What a field is (FieldInfo, built by niceview.Field()), how a model's fields are resolved
into a set of them (Fields), how a layout arranges them — and the model-free half, which
renders a single widget from a single FieldInfo. See Field types for the
type→widget mapping.
Field metadata¶
Field
¶
Field(**kwargs: Unpack[_FieldInfoInputs]) -> FieldInfo
Create FieldInfo instance with the provided keyword arguments. This is a convenience function to create fields for forms and tables.
FieldInfo
¶
Per-field UI metadata, the rendering counterpart of pydantic's Field: not validation or serialization, but how a field looks and behaves in forms and tables.
Source code in niceview/fieldinfo.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
field_type
class-attribute
instance-attribute
¶
Python type of the value. Resolved from the model annotation by Fields; set it explicitly when building a FieldInfo by hand for render_field().
placeholder
class-attribute
instance-attribute
¶
placeholder: str | None = None
Placeholder text shown in an empty text-like widget.
required
class-attribute
instance-attribute
¶
required: bool | None = None
Whether the field must have a value. None until Fields resolves it from pydantic's is_required() — a different concept from pydantic's own required.
editable
class-attribute
instance-attribute
¶
editable: bool = True
Whether the widget accepts input; False renders it disabled.
hint
class-attribute
instance-attribute
¶
hint: str | None = None
Help text shown below the widget (Quasar's hint prop). Set explicitly; widgets without
a hint slot (see widgets.HINT_WIDGETS) ignore it.
description
class-attribute
instance-attribute
¶
description: str | None = None
What the model says the field means, resolved from pydantic's description. Carried as
metadata only — description_as decides at render time whether it becomes the hint, the
tooltip, or nothing at all, and an explicit hint/tooltip always wins over it.
widget_type
class-attribute
instance-attribute
¶
Which element renders the field; inferred from the type if omitted.
props
class-attribute
instance-attribute
¶
props: str | None = None
Quasar props string, merged on top of niceview's own.
style
class-attribute
instance-attribute
¶
style: str | None = None
Inline CSS style for the widget.
aggrid
class-attribute
instance-attribute
¶
Additional ag-grid column properties, e.g. {'headerName': 'My Column'}, merged on top of the computed ones.
options
class-attribute
instance-attribute
¶
Choices for select/radio/toggle/checkbox_group widgets. Resolution order per widget: options, then literal_options (auto-extracted from Literal[...] types). checkbox_group lays out horizontally via props='inline' (same convention as ui.radio).
password
class-attribute
instance-attribute
¶
password: bool = False
Mask the input (ui.input only).
password_toggle_button
class-attribute
instance-attribute
¶
password_toggle_button: bool = False
Show/hide toggle for a password input.
autocomplete
class-attribute
instance-attribute
¶
Autocomplete suggestions for a text input.
validation
class-attribute
instance-attribute
¶
Extra validation beyond required: a NiceGUI ValidationFunction or dict.
precision
class-attribute
instance-attribute
¶
precision: int | None = None
Decimal places (ui.number).
prefix
class-attribute
instance-attribute
¶
prefix: str | None = None
Text shown before the value (ui.number).
suffix
class-attribute
instance-attribute
¶
suffix: str | None = None
Text shown after the value (ui.number).
number_format
class-attribute
instance-attribute
¶
number_format: str | None = None
Display format of ui.number, e.g. '%.2f'. Named number_format, not format, to keep it
apart from JSON Schema's format, which corresponds to widget_type.
clearable
class-attribute
instance-attribute
¶
clearable: bool = False
Offer a clear button. Honoured by the select-like widgets and by every text input, ui.color_input included; a widget without a clear affordance (checkbox, switch, radio, slider, rating, checkbox_group) ignores it. Clearing writes None into the field.
with_input
class-attribute
instance-attribute
¶
with_input: bool = False
Allow free-text filtering in ui.select.
multiple
class-attribute
instance-attribute
¶
multiple: bool = False
Allow selecting multiple values in ui.select.
key_generator
class-attribute
instance-attribute
¶
Generates a dict key for a new value typed into ui.select.
color_preview
class-attribute
instance-attribute
¶
color_preview: bool = False
Show a color swatch preview next to ui.color_input.
literal_options
class-attribute
instance-attribute
¶
literal_options: list | None = None
Choices auto-extracted from a Literal[...] annotation; set by Fields, not user-settable.
new_value_mode
class-attribute
instance-attribute
¶
new_value_mode: Literal["add", "add-unique", "toggle"] = (
"add-unique"
)
How ui.input_chips treats a typed value not already in the list.
item_type
class-attribute
instance-attribute
¶
item_type: type | None = None
Item's pydantic type for editgrid/modelselect fields.
table_label
class-attribute
instance-attribute
¶
table_label: str = ''
Column header label; defaults to the field's label.
table_hidden
class-attribute
instance-attribute
¶
table_hidden: bool = False
Hide the column in a table/grid (the field may still show in a form).
table_align
class-attribute
instance-attribute
¶
table_align: Literal["left", "center", "right"] | None = (
None
)
Horizontal text alignment of the cell.
table_cell_style
class-attribute
instance-attribute
¶
table_cell_style: str = ''
Extra CSS for the cell, merged with table_align.
table_sortable
class-attribute
instance-attribute
¶
table_sortable: bool = True
Whether the column can be sorted.
table_sort
class-attribute
instance-attribute
¶
table_sort: Literal[None, 'asc', 'desc'] | None = None
Default sort order for the column.
table_filterable
class-attribute
instance-attribute
¶
table_filterable: bool = True
Show a filter row for the column; filter type inferred from the field type.
Fields
¶
Bases: Mapping[str, FieldInfo]
Fields and field information for datamodel based UI components.
Source code in niceview/fields.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
layout
property
¶
layout: LayoutGroup
The form layout: a tree of rows, columns and sections over the fields. Without an explicit
layout this is a flat group in field order, which renders exactly as before.
Grids and lists ignore the tree and read the flattened field_names.
is_included
¶
Check if the field is included (and not excluded) in the fields. Exclude private fields (starting with '_') by default.
Source code in niceview/fields.py
validation_errors
¶
Validate the model with the new value and return a list of validation errors. If there are no validation errors, return None.
Source code in niceview/fields.py
validation_error_list
¶
Validate the model with the new value and return a list of validation error messages.
Source code in niceview/fields.py
Layout¶
The tree a layout notation parses into. '@name' becomes a LayoutAction, a field name a
LayoutField, a nested list a LayoutGroup — see
Components for the notation itself.
LayoutGroup
dataclass
¶
A container in a form layout: a row, a column, or — with a title — a section. Nesting alternates row and column; a titled group is always a column, so that a section reads the same wherever it sits.
A section comes in two shapes, told apart by card: '# Title' draws a card around its
fields, '## Title' only sets the heading above them. Both headings look the same.
Source code in niceview/fields.py
LayoutField
dataclass
¶
One field in a form layout, with the CSS classes given after its colon (if any).
Source code in niceview/fields.py
LayoutAction
dataclass
¶
One action button in a form layout, written '@name' and looked up in the form's actions.
An action is layout, not a field: it has no value, no validation and no place in the model.
Source code in niceview/fields.py
parse_layout
¶
parse_layout(
spec: Any,
valid_names: Container[str],
*,
valid_actions: Container[str] = (),
row: bool = False,
path: str = "layout",
) -> LayoutGroup
Parse a nested field layout into LayoutGroups, LayoutFields and LayoutActions.
A list holds field names; a nested list opens a container (rows and columns alternate).
Leading strings are metadata for their own group — '# Title' makes it a titled card,
'## Title' a section with the same heading but no card around it, ':classes' replaces the
container's default CSS classes. A field name may carry classes of its own after a colon:
'streetw-2/3' (only the first colon separates, so Tailwind prefixes stay intact).
'@name' is an action button rather than a field, and must be declared in valid_actions —
the form's actions table, which holds the callback the name cannot carry.
Raises ValueError with the position of the offending element.
Source code in niceview/fields.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
layout_field_names
¶
layout_field_names(group: LayoutGroup) -> list[str]
All field names in a layout, in rendering order. Actions are not fields and not listed.
Source code in niceview/fields.py
layout_action_names
¶
layout_action_names(group: LayoutGroup) -> list[str]
All action names in a layout, in rendering order.
Source code in niceview/fields.py
Widgets without a model¶
render_field
¶
render_field(
field_info: FieldInfo,
value: Any = None,
*,
local_tz: str | None = None,
required_marker: str
| None
| _FromChromeText = FROM_CHROME_TEXT,
description_as: DescriptionTarget = DESCRIPTION_AS,
) -> Any
Render a single widget from a FieldInfo in the current NiceGUI context, initialised to
value, and return it.
The widget-building half of ModelForm without the model: no Fields, no item, no adapter,
no autosave, no change events — the caller reads the widget itself, via
field_value(widget, field_info) for the same value conversions ModelForm applies.
fi = niceview.Field(label='Name', widget_type='ui.input', props='outlined dense', classes='w-full')
widget = niceview.render_field(fi, 'Alice')
...
name = niceview.field_value(widget, fi)
field_info.widget_type is required — without a model there is nothing to infer it from.
label / placeholder / hint / tooltip / props / classes / style / options and the
widget-specific attributes (min, max, step, multiple, ...) are applied as in a ModelForm,
the application-wide FieldStyle included: the category's props (input_props / control_props)
beneath the field's own, and default_classes when the field brings none. Only a ModelForm's
per-form layers (base_props, its own default_classes, the layout) do not apply here — there
is no form. Validation works the same: required rejects an empty value, then
field_info.validation runs as NiceGUI's own validation would. What a ModelForm adds on
top — validating the whole item against a Pydantic model — is the only other difference.
required also appends required_marker to the label — ChromeText's by default; pass
required_marker=None for none.
field_info.description is help text without a fixed place: description_as decides
whether it is rendered as the hint, as the tooltip, or not at all. It is the slot for text
that came from a schema rather than from the person laying out the form — an explicit
hint or tooltip on the FieldInfo always wins over it.
Raises ValueError if widget_type is missing, unknown, or one of 'editgrid' / 'modelselect' (both need a model type and a repository — use ModelForm for those).
Source code in niceview/widgets.py
field_value
¶
Read a widget rendered by render_field() and convert its value to the field's Python type.
The inverse of the value handling in render_field(): ISO strings become date / time /
datetime / timedelta objects, numbers become int or float according to
field_info.field_type, comma-separated chips are split.
field_info.field_type (default str) drives the conversions that depend on the target
type — set it to int to get ints out of a 'ui.number', to list[str] for a
comma-separated 'ui.input', or to T | None to have an empty selection read back as None.
Raises ValueError for 'editgrid' and 'modelselect', which need a model and a repository.
Source code in niceview/widgets.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 | |
to_widget_value
¶
Convert a Python value to the representation its widget expects.
date / time / datetime / timedelta become ISO strings (a value that already is a string is passed through unchanged, so JSON-sourced data works without pre-conversion); a None multi-selection becomes an empty list. Everything else is returned unchanged.
'modelselect' is not handled here — the key lookup needs a repository (see ModelForm).
Source code in niceview/widgets.py
CheckboxGroup
¶
Composite widget for list[Literal[...]] fields rendered as a row/column of ui.checkbox elements. There is no built-in NiceGUI/Quasar multi-select checkbox-group widget, so this composes plain ui.checkbox elements and exposes the .value / on_value_change() surface that ModelForm's value-conversion and event-wiring code expects from a widget.
checkboxes (options -> ui.checkbox) and widget (the ui.row/ui.column holding them)
are public so callers can style individual checkboxes or the container after rendering,
e.g. form.w('perms', CheckboxGroup).checkboxes['admin'].classes('text-negative').
Source code in niceview/widgets.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
set_options
¶
Replace the checkboxes with a new option set, keeping the current selection where possible.
Source code in niceview/widgets.py
reserves_bottom_space
¶
reserves_bottom_space(
field_info: FieldInfo,
description_as: DescriptionTarget = DESCRIPTION_AS,
) -> bool
Whether this field is taller than its box: Quasar keeps a strip of 20px free below a field
that can show a message, so that the layout does not jump when one appears
(q-field--with-bottom).
Two things ask for that strip, and both are niceview's own doing: a validation — NiceGUI
reserves the space for it (error=False), and ModelForm wires one on every VALIDATED_WIDGET
— and a hint, on the QInput based widgets that have a slot for one. Everything else, a
switch or a slider or a group of checkboxes, is exactly as tall as it looks.
Independent of outlined, filled and friends: those change how tall the box is, not what
sits below it.