I am developing a library management system in python
with the help of sqlite3
and flet
modules. I have used the DataTable
controls as follows:
class BooksStatusControl(ft.UserControl):
def __init__(self, page):
super().__init__()
self.page = page
def build(self):
books = self.page.db.fetch_all_books()
self.data_table = ft.DataTable(
columns=[
ft.DataColumn(ft.Text("Id")),
ft.DataColumn(ft.Text("Title")),
ft.DataColumn(ft.Text("Author")),
ft.DataColumn(ft.Text("Issued to")),
],
rows=[
ft.DataRow(
cells=[
ft.DataCell(ft.Text(id)),
ft.DataCell(ft.Text(title)),
ft.DataCell(ft.Text(author)),
ft.DataCell(ft.Text(issued_to if issued_to is not None else "-")),
],
) for id, title, author, issued_to in books
]
)
return ft.Container(
height=500,
content=ft.Column(
scroll="always",
controls=[
self.data_table
]
)
)
self.page.db.fetch_all_books
implementation
class Database:
def __init__(self, file):
self.file = file
...
def fetch_all_books(self):
with sqlite3.connect(self.file) as conn:
cursor = conn.cursor()
query = "SELECT * FROM BOOKS"
cursor.execute(query)
return cursor.fetchall()
It works fine when there are 10-15 books but starts lagging when there are near 200 books and does not even load at higher numbers.
How can I make it efficient like the ListView
control?