Skip to content Skip to sidebar Skip to footer

Display List Of Ordereddict In Kivy

I have a list of Ordereddict as follows list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle'), ('Grade', 22)]), OrderedDict

Solution 1:

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

classRV(BoxLayout):
    column_headings = ListProperty([])
    data_items = ListProperty([])

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

    defget_users(self):

        # extract keys or column headingsfor row in list1:
            for key in row.keys():
                if key notin 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 isNoneelse x for x in values]

Output

Result

Post a Comment for "Display List Of Ordereddict In Kivy"