Skip to content

Instantly share code, notes, and snippets.

@gottadiveintopython
Last active September 19, 2021 22:26
Show Gist options
  • Save gottadiveintopython/e7d6159868b2874bec74a67245aca0ca to your computer and use it in GitHub Desktop.
Save gottadiveintopython/e7d6159868b2874bec74a67245aca0ca to your computer and use it in GitHub Desktop.
sqlite3 database as RecycleView data source
import sqlite3
from kivy.app import App
from kivy.lang import Builder
def init_db():
import itertools
from string import ascii_uppercase
conn = sqlite3.connect(":memory:")
cur = conn.cursor()
cur.execute("""
CREATE TABLE LabelAttrs(
'text' TEXT,
font_size INT
);
""")
cur.executemany(
"INSERT INTO LabelAttrs(text, font_size) VALUES (?, ?)",
zip(
map(''.join, itertools.product(ascii_uppercase, repeat=3)),
itertools.cycle((14, 20, 30, 40))
)
)
conn.commit()
return conn
KV_CODE = '''
RecycleView:
viewclass: 'Label'
RecycleBoxLayout:
orientation: 'vertical'
size_hint_y: None
default_size_hint: 1, None
default_size: 100, 40
height: self.minimum_height
'''
class RVCompatibleRow(sqlite3.Row):
def get(self, key, default_value):
return default_value
def items(self):
return zip(self.keys(), self)
# def items(self):
# for key in self.keys():
# yield (key, self[key])
class SampleApp(App):
def build(self):
return Builder.load_string(KV_CODE)
def on_start(self):
rv = self.root
conn = init_db()
conn.row_factory = RVCompatibleRow
rv.data = conn.execute("SELECT text, font_size FROM LabelAttrs")
if __name__ == '__main__':
SampleApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment