| Time | Pressure | Temperature |
|---|---|---|
| 12 | 53 | 25 |
| 13 | 63 | 24 |
| 14 | 73 | 23 |
class StaticTable(StaticTableWidget):
"""
Static data table build with :class:`~wildewidgets.StaticTableWidget`.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.add_heading("Time")
self.add_heading("Pressure")
self.add_heading("Temperature")
self.add_row([12, 53, 25])
self.add_row([13, 63, 24])
self.add_row([14, 73, 23])
| Time | Pressure | Temperature |
|---|---|---|
| 12 | 53 | 25 |
| 13 | 63 | 24 |
| 14 | 73 | 23 |
def get_widget(self) -> DataTable:
"""
Return the data table.
Returns:
The data table.
"""
table = DataTable()
table.is_data_list = False
table.add_column("time")
table.add_column("pressure")
table.add_column("temperature")
table.add_row(time=12, pressure=53, temperature=25)
table.add_row(time=13, pressure=63, temperature=24)
table.add_row(time=14, pressure=73, temperature=23)
return table
| Name | Time | Pressure | Temperature | Restricted | Open |
|---|
class TestTable(DataTable):
"""
Test table with DatatablesJS.
"""
#: The model to use for the table.
model = Measurement
#: The ID of the table.
table_id: str = "data_measurement"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Add all the columns.
self.add_column("name")
self.add_column("time", searchable=False)
self.add_column("pressure", align="right")
self.add_column("temperature", align="right")
self.add_column("restricted", visible=False)
self.add_column("open", sortable=False)
# Set the `restricted` filter.
filtr = DataTableFilter()
filtr.add_choice("True", "True")
filtr.add_choice("False", "False")
self.add_filter("restricted", filtr)
# Set the `open` filter.
filtr = DataTableFilter()
filtr.add_choice("True", "True")
filtr.add_choice("False", "False")
self.add_filter("open", filtr)
# Set the `pressure` filter.
filtr = DataTableFilter()
filtr.add_choice("< 1000", "level_1000")
filtr.add_choice("1000-2000", "level_2000")
filtr.add_choice("2000-3000", "level_3000")
self.add_filter("pressure", filtr)
def filter_pressure_column(self, qs, column, value) -> QuerySet: # noqa: ARG002
"""
Filter endpoint for the ``pressure`` column.
Args:
qs: The queryset to filter.
column: The column to filter (unused).
value (str): The value to filter by.
Returns:
The filtered queryset.
"""
if value == "level_1000":
qs = qs.filter(pressure__lt=1000)
elif value == "level_2000":
qs = qs.filter(pressure__lt=2000).filter(pressure__gte=1000)
elif value == "level_3000":
qs = qs.filter(pressure__lt=3000).filter(pressure__gte=2000)
else:
qs = qs.filter(pressure__contains=value)
return qs
def filter_restricted_column(self, qs, column, value) -> QuerySet: # noqa: ARG002
"""
Filter endpoint for the ``restricted`` column.
Args:
qs: The queryset to filter.
column: The column to filter (unused).
value: The value to filter by.
Returns:
The filtered queryset.
"""
test = value == "True"
return qs.filter(restricted=test)
def filter_open_column(self, qs, column, value) -> QuerySet: # noqa: ARG002
"""
Filter endpoint for the ``open`` column.
Args:
qs: The queryset to filter.
column: The column to filter (unused).
value (str): The value to filter by.
Returns:
The filtered queryset.
"""
test = value == "True"
return qs.filter(open=test)
| Book Title | Authors | ISBN |
|---|
class BookModelTable(BasicModelTable):
"""
Book model table with DatatablesJS (AJAX).
"""
#: The model to use for the table.
model: type[Model] = Book
#: The fields to display in the table.
fields: list[str] = ["title", "authors__full_name", "isbn"] # noqa: RUF012
#: The alignment of the columns.
alignment: ClassVar[dict[str, Literal["left", "right", "center"]]] = { # type: ignore[misc]
"authors": "left",
}
#: The verbose names of the columns.
verbose_names: dict[str, str] = {"authors__full_name": "Authors"} # noqa: RUF012
#: Whether to show the dataTables.js buttons
buttons: bool = True
#: Whether to stripe the table rows.
striped: bool = True
def render_authors__full_name_column(self, row, column): # noqa: ARG002
"""
Return the full name of the authors, split into multiple lines if there
are multiple authors.
Args:
row: The row to render.
column: The column to render.
Returns:
The full name of the authors.
"""
authors = row.authors.all()
if authors.count() > 1:
return f"{authors[0].full_name} ... "
return authors[0].full_name
| Name | Pressure | Temperature |
|---|
class PressureCellWidget(Block):
"""
Pressure cell widget with FontIcon. This is used to demonstrate the use of
cell widgets in a table.
"""
def __init__(self, *args, row=None, column="", **kwargs): # noqa: ARG002
value = row.pressure
if value > 2000: # noqa: PLR2004
icon = "thermometer-high"
color = "red"
elif value > 1000: # noqa: PLR2004
icon = "thermometer-half"
color = "orange"
else:
icon = "thermometer-low"
color = "green"
super().__init__(
HorizontalLayoutBlock(
FontIcon(icon=icon, color=color), f"{value}", justify="end"
),
*args,
**kwargs,
)
class WidgetCellTable(WidgetCellMixin, BasicModelTable): # type: ignore[misc]
"""
Table with widget cells. This is used to demonstrate the use of widget cells
in a table.
"""
#: The fields to display in the table.
fields: list[str] = ["name", "pressure", "temperature"] # noqa: RUF012
#: The cell widgets to use for the table.
cell_widgets: dict[str, type[Widget]] = {"pressure": PressureCellWidget} # noqa: RUF012
#: The model to use for the table.
model: type[Model] = Measurement
#: The alignment of the columns.
alignment: ClassVar[dict[str, Literal["left", "right", "center"]]] = { # type: ignore[misc]
"pressure": "right",
"temperature": "right",
}
#: Whether to stripe the table rows.
striped: bool = True