Welcome | Get started | Dive | Contribute | Topics | Reference | Changes | More

Virtual tables

The rows of a virtual table are defined by a method get_data_rows. In data tables this method has a default implementation based on the model attribute.

The columns of a virtual table must be defined using virtual fields.

Here is an example of a virtual table (taken from the lino_book.projects.vtables demo project):

from lino.api import dd

DATA = [
    ["Belgium", "Eupen", 17000],
    ["Belgium", "Liege", 400000],
    ["Belgium", "Raeren", 5000],
    ["Estonia", "Tallinn", 400000],
    ["Estonia", "Vigala", 1500],
]


class MyBase(dd.VirtualTable):

    @classmethod
    def get_data_rows(cls, ar):
        return DATA

    @dd.displayfield("Country")
    def country(cls, row, ar):
        return row[0]


class Cities(MyBase):

    column_names = "country city"

    @dd.displayfield("City")
    def city(cls, row, ar):
        return row[1]


class CitiesAndInhabitants(Cities):

    column_names = "country city population"

    @dd.displayfield("Population")
    def population(cls, row, ar):
        return row[2]

We can show this table in a shell session:

>>> from lino import startup
>>> startup('lino_book.projects.vtables.settings')
>>> from lino.api.doctest import *
>>> rt.show(vtables.Cities)
... 
========= =========
 Country   City
--------- ---------
 Belgium   Eupen
 Belgium   Liege
 Belgium   Raeren
 Estonia   Tallinn
 Estonia   Vigala
========= =========
>>> rt.show(vtables.CitiesAndInhabitants)
... 
========= ========= ============
 Country   City      Population
--------- --------- ------------
 Belgium   Eupen     17000
 Belgium   Liege     400000
 Belgium   Raeren    5000
 Estonia   Tallinn   400000
 Estonia   Vigala    1500
========= ========= ============

Usage examples of virtual tables in real applications: