Zubin
Zubin

Reputation: 69

Is there a way to display a pandas Dataframe in KivyMD?

Is there a way to display a pandas DataFrame in KivyMD? I tried putting the dataframe as a KivyMD DataTable but it doesn't work that way.

Upvotes: 2

Views: 1344

Answers (1)

Federico A.
Federico A.

Reputation: 266

You could use the MDDataTable and fill the row with the dataframe. You only need to process the data. Try to define a function to do that:

def get_data_table(dataframe):
    column_data = list(dataframe.columns)
    row_data = dataframe.to_records(index=False)
    return column_data, row_data

Then you can put all into the MDDataTable after setting the size of columns.

import pandas as pd
from kivy.uix.anchorlayout import AnchorLayout
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp

dataframe  = pd.read_csv(filename)
class MyApp(MDApp):
    def build(self):

        layout = AnchorLayout()
        
        
        container = my_root.ids.container
        print(container)
        column_data, row_data = get_data_table(dataframe)
        column_data = [(x, dp(60)) for x in column_data]

        table = MDDataTable(
            column_data = column_data,
            row_data = row_data,
            use_pagination=True
        )
        layout.add_widget(table)
        
        return layout
MyApp().run()

Upvotes: 3

Related Questions