Skip to main content

Grid

A Grid is a robust way to display an information-rich collection of items, and allow people to sort, group, and filter the content. Use Grid when information density is critical.

Examples​

Live demo

Basic grid​

import pglet
from pglet import Column, Grid, Stack, Text


class Person:
def __init__(
self, first_name: str, last_name: str, age: int = None, employee: bool = False
):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.employee = employee


with pglet.page("basic-grid") as page:

page.add(
Text("Basic grid", size="large"),
Stack(
width="50%",
controls=[
Grid(
columns=[
Column(name="First name", field_name="first_name"),
Column(name="Last name", field_name="last_name"),
Column(name="Age", field_name="age"),
],
items=[
Person(first_name="John", last_name="Smith", age=30),
Person(first_name="Samantha", last_name="Fox", age=43),
Person(first_name="Alice", last_name="Brown", age=25),
],
)
],
),
)

Sortable grid​

import pglet
from pglet import Column, Grid, Text


class Person:
def __init__(
self, first_name: str, last_name: str, age: int = None, employee: bool = False
):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.employee = employee


with pglet.page("sortable-grid") as page:

page.add(
Text("Sortable grid with resizable columns and selectable rows", size="large"),
Grid(
selection_mode="single",
preserve_selection=True,
columns=[
Column(
resizable=True,
sortable="string",
name="First name",
field_name="first_name",
),
Column(
resizable=True,
sortable="string",
sorted="asc",
name="Last name",
field_name="last_name",
),
Column(resizable=True, sortable="number", name="Age", field_name="age"),
],
items=[
Person(first_name="John", last_name="Smith", age=30),
Person(first_name="Samantha", last_name="Fox", age=43),
Person(first_name="Alice", last_name="Brown", age=25),
],
),
)


Compact grid​

import pglet
from pglet import Column, Grid, Text


class Person:
def __init__(
self, first_name: str, last_name: str, age: int = None, employee: bool = False
):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.employee = employee


with pglet.page("compact-grid") as page:

page.add(
page.add(
Text("Compact grid with no header and multiple selection", size="large"),
Grid(
compact=True,
header_visible=False,
selection_mode="multiple",
preserve_selection=True,
columns=[
Column(max_width=100, field_name="first_name"),
Column(max_width=100, field_name="last_name"),
Column(max_width=100, field_name="age"),
],
items=[
Person(first_name="John", last_name="Smith", age=30),
Person(first_name="Samantha", last_name="Fox", age=43),
Person(first_name="Alice", last_name="Brown", age=25),
],
),
)
)

Dynamic grid​

import pglet
from pglet import Button, Checkbox, Column, Grid, Stack, Text, Textbox, Toolbar, toolbar


class Person:
def __init__(
self, first_name: str, last_name: str, age: int = None, employee: bool = False
):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.employee = employee


with pglet.page("dynamic-grid") as page:

grid = None

def delete_records(e):
for r in grid.selected_items:
grid.items.remove(r)
page.update()

delete_records = toolbar.Item(
text="Delete records", icon="Delete", disabled=True, on_click=delete_records
)
grid_toolbar = Toolbar(items=[delete_records])

def grid_items_selected(e):
delete_records.disabled = len(e.control.selected_items) == 0
delete_records.update()

grid = Grid(
selection_mode="multiple",
compact=True,
header_visible=True,
columns=[
Column(
name="First name", template_controls=[Textbox(value="{first_name}")]
),
Column(name="Last name", template_controls=[Textbox(value="{last_name}")]),
Column(name="Age", template_controls=[Text(value="{age}")]),
Column(
name="Is employee", template_controls=[Checkbox(value_field="employee")]
),
],
items=[
Person(first_name="John", last_name="Smith", age=30, employee=False),
Person(first_name="Jack", last_name="Brown", age=43, employee=True),
Person(first_name="Alice", last_name="Fox", age=25, employee=False),
],
margin=0,
on_select=grid_items_selected,
)

first_name = Textbox("First name")
last_name = Textbox("Last name")
age = Textbox("Age")

def add_record(e):
grid.items.append(
Person(
first_name=first_name.value,
last_name=last_name.value,
age=age.value,
employee=True,
)
)
first_name.value = ""
last_name.value = ""
age.value = ""
page.update()

page.add(
Text("Dynamic grid with template columns", size="large"),
grid_toolbar,
grid,
Text("Add new employee record", size="medium"),
Stack(horizontal=True, controls=[first_name, last_name, age]),
Button("Add record", on_click=add_record),
)

input()

Properties​

NameTypeDefaultDescription
selectionstringnoneItems selection mode: none, single or multiple.
compactboolfalseWhether to render Grid in a compact form.
headerVisiblebooltrueWhether Grid header is visible.
preserveSelectionboolfalseBy default, selection is cleared when clicking on an empty (non-focusable) section of the screen. Setting this value to true overrides that behavior and maintains selection.
shimmerLinesint0Whether to display shimmer lines if items collection is empty. 0 - do not display shimmer.

Events​

NameDescription
selectFires when one or more Grid items are selected or de-selected.
itemInvokeFires when Grid item is invoked with double-click or Enter.

Child controls​

Columns control​

Serves as a container for column controls.

Child controls​

Items control​

Serves as a container for item controls.

Child controls​

Column control​

Describes Grid column.

Properties​

NameTypeDefaultDescription
namestringColumn header text.
iconstringColumn header icon next to the text.
iconOnlyboolfalseDisplay only header icon.
fieldNamestringItem's property name to display in the column.
sortablestringWhether client-side sorting is enabled for this column. Supported values: string - column is sortable as a string; number - column is sortable as a number; false or empty - column is not sortable.
sortFieldstringItem's property name used for sorting. Sort by fieldName if not specified.
sortedstringfalseWhether the items are sorted by sortField or fieldName and in what direction. Supported values: false (unsorted), asc or desc.
resizableboolfalseWhether the column is resizable.
minWidthnumberMinimum width of the column.
maxWidthnumberMaximum width of the column.
onClickboolfalseWhether column header is clickable.

Events​

NameDescription
clickFires when Grid column is clicked.

Child controls​

Any controls to enable template column.

Item control​

Item control holds the data for a single row.

Properties​

Item control can have any properties.