0

I have a list of Ordereddict as follows

list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle'), ('Grade', 22)]),
OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul'), ('Grade', 15)]),
OrderedDict([('Numbers', '25'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus'), ('Grade', 24)]),
OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John'), ('Grade', 71)])]

There are 8 unique keys and one common key in it, i would like to create a table from it in kivy with the same order, with keys being the header of the table. My expected output is as below, i am new to kivy ecosystem and i dont see anything like tableview in that, any other views could be used to get this output and how

Expected output in kivy

enter image description here

I took the simpler recycyle view example given in the comment and edited the no of columns to 9 and tried picking the values from Ordereddict and i got the below output, since i am new to kivy i am not sure to pull the values as in expected output

problem

Below are .py and .kv files

check.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from collections import OrderedDict
list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle'), ('Grade', 22)]),
OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul'), ('Grade', 15)]),
OrderedDict([('Numbers', '25'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus'), ('Grade', 24)]),
OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John'), ('Grade', 71)])]

class TextInputPopup(Popup):
    obj = ObjectProperty(None)
    obj_text = StringProperty("")

    def __init__(self, obj, **kwargs):
        super(TextInputPopup, self).__init__(**kwargs)
        self.obj = obj
        self.obj_text = obj.text


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected

    def on_press(self):
        popup = TextInputPopup(self)
        popup.open()

    def update_changes(self, txt):
        self.text = txt


class RV(BoxLayout):

    # data_items = ListProperty(newlist)

    data_items = ListProperty([])
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):            
        # create data_items
        for i in list1:
                self.data_items.append(i.values())


class TestApp(App):
    title = "Kivy RecycleView & SQLite3 Demo"

    def build(self):
        return RV()


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.10.0

<TextInputPopup>:
    title: "Popup"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        TextInput:
            id: txtinput
            text: root.obj_text
        Button:
            size_hint: 1, 0.2
            text: "Save Changes"
            on_release:
                root.obj.update_changes(txtinput.text)
                root.dismiss()
        Button:
            size_hint: 1, 0.2
            text: "Cancel Changes"
            on_release: root.dismiss()


<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    BoxLayout:
        orientation: "vertical"

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 9

            Label:
                text: "Numbers"
            Label:
                text: "FirstName"
            Label:
                text: "SecondName"
            Label:
                text: "MiddleName"
            Label:
                text: "Grade"
            Label:
                text: "Names"
            Label:
                text: "NewFirstName"
            Label:
                text: "NewSecondName"
            Label:
                text: "NewThirdName"


        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.data_items]
                SelectableRecycleGridLayout:
                    cols: 9
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'horizontal'
                    multiselect: True
                    touch_multiselect: True
ikolim
  • 15,721
  • 2
  • 19
  • 29
pythoneer
  • 9
  • 8
  • You can use [Kivy RecycleView](https://kivy.org/doc/stable/api-kivy.uix.recycleview.html) to create a table view. – ikolim May 13 '19 at 15:07
  • @ikolim can you please give me any sample code wrt to the output i expect please – pythoneer May 13 '19 at 15:10
  • Here is an example of [RecycleView](https://stackoverflow.com/questions/50219281/python-how-to-add-vertical-scroll-in-recycleview/50299444#50299444) – ikolim May 13 '19 at 15:20
  • My head is spinning in understanding kivy ecosystem :( – pythoneer May 13 '19 at 15:39
  • Here is another [simpler RecycleView](https://stackoverflow.com/questions/47013527/how-to-fetch-data-from-database-and-show-in-table-in-kivypython/47021346#47021346) – ikolim May 13 '19 at 16:05
  • i am not able to make this work to get the expected output – pythoneer May 14 '19 at 05:07
  • Please provide [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) so that we can reproduce the problem and help you. Thank you. – ikolim May 14 '19 at 15:39
  • @ikolim done, please check – pythoneer May 14 '19 at 16:58

1 Answers1

0

Question - TableView

There are 8 unique keys and one common key in it, i would like to create a table from it in kivy with the same order, with keys being the header of the table.

Solution - Using Kivy RecycleView

Modify the app to extract the keys and values from the collections.OrderedDict

  • Add a class attribute of type ListProperty e.g. column_headings = ListProperty([])
  • Implement nested for loop to extract keys or column headings
  • Implement nested for loop to extract values
  • Override None with empty string

Snippets

class RV(BoxLayout):
    column_headings = ListProperty([])
    data_items = ListProperty([])

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):

        # extract keys or column headings
        for row in list1:
            for key in row.keys():
                if key not in self.column_headings:
                    self.column_headings.append(key)

        # extract values
        values = []
        for row in list1:
            for key in self.column_headings:
                try:
                    values.append(str(row[key]))
                    del row[key]
                except KeyError:
                    values.append(None)

        # replace None with empty string
        self.data_items = ['' if x is None else x for x in values]

Output

Result

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Thank you, this works, which property i should check for resizing the cell and i want the color of a cell to change according to the data in it, where can i make those changes? – pythoneer May 15 '19 at 07:03
  • Could you please open another question for the two questions (resizing and color). Thank you very much. – ikolim May 15 '19 at 15:18