Time | Pressure | Temperature |
---|---|---|
12 | 53 | 25 |
13 | 63 | 24 |
14 | 73 | 23 |
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)
Name | Time | Pressure | Temperature | Restricted | Open |
---|
class TestTable(DataTable):
model = Measurement
table_id = "data_measurement"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
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)
filtr = DataTableFilter()
filtr.add_choice("True", "True")
filtr.add_choice("False", "False")
self.add_filter('restricted', filtr)
filtr = DataTableFilter()
filtr.add_choice("True", "True")
filtr.add_choice("False", "False")
self.add_filter('open', filtr)
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):
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):
test = value == 'True'
qs = qs.filter(restricted=test)
return qs
def filter_open_column(self, qs, column, value):
test = value == 'True'
qs = qs.filter(open=test)
return qs
Book Title | Authors | ISBN |
---|
class BookModelTable(BasicModelTable):
fields = ['title', 'authors__full_name', 'isbn']
model = Book
alignment = {'authors': 'left'}
verbose_names = {'authors__full_name': 'Authors'}
buttons = True
striped = True
def render_authors__full_name_column(self, row, column):
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):
def __init__(self, *args, row=None, column="", **kwargs):
value = row.pressure
if value > 2000:
icon = "thermometer-high"
color = "red"
elif value > 1000:
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):
fields = ['name', 'pressure', 'temperature']
cell_widgets = {'pressure': PressureCellWidget}
model = Measurement
alignment = {'pressure': 'right', 'temperature': 'right'}
striped = True