filling a wxgrid

Hi,

Is there a way to fill a grid from a numpy array without having to iterate through it and issuing SetCellValue for each cell?
same for reading?

thanks,

···


Flávio Codeço Coelho
registered Linux user # 386432

“Laws are like sausages. It’s better not to see them being made.”
Otto von Bismark

Create a virtuall Grid...
Either I haven't a example at my workplace

···

-----Ursprüngliche Nachricht-----
Von: Flavio Coelho [mailto:fccoelho@gmail.com]
Gesendet: Dienstag, 17. Oktober 2006 14:52
An: wxpython-users@lists.wxwidgets.org
Betreff: [wxPython-users] filling a wxgrid

Hi,

Is there a way to fill a grid from a numpy array without having to iterate
through it and issuing SetCellValue for each cell?
same for reading?

thanks,

--
Flávio Codeço Coelho
registered Linux user # 386432
---------------------------
"Laws are like sausages. It's better not to see them being made."
Otto von Bismark

Flavio Coelho <fccoelho <at> gmail.com> writes:

Hi,Is there a way to fill a grid from a numpy array without having to iterate

through it and issuing SetCellValue for each cell?same for reading?thanks,--
Flávio Codeço Coelho

here's a minimal example:

import wx
import wx.grid
import numpy as N

class TableBase(wx.grid.PyGridTableBase):
    def __init__(self, data):
        wx.grid.PyGridTableBase.__init__(self)
        self.data = data
        
    def GetNumberRows(self):
        return self.data.shape[0]

    def GetNumberCols(self):
        return self.data.shape[1]
    
    def GetValue(self, row, col):
        return self.data[row][col]

class Grid(wx.grid.Grid):
    def __init__(self, parent, data):
        wx.grid.Grid.__init__(self, parent, -1)

        table = TableBase(data)
        self.SetTable(table, True)

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1)

        pan = wx.Panel(self, -1)
        grid = Grid(pan, N.random.rand(30,40))

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 1, wx.EXPAND)
        pan.SetSizer(sizer)
        pan.Layout()

class App(wx.App):
    def OnInit(self):
        frame = Frame()
        frame.Show()
        return True

app = App()
app.MainLoop()

Christian

Thanks for the example,

but I still have a few questions:

1)It seem almost as slow as when I looped through all cells calling “SetCellValue()”, is this right?
2) After I implemented it as sugested below, my SetColLabelValue(‘col_title’) stopped having any effect! Whats going on?

  1. Why do you subclass TableBase from wx.grid.PyGridTableBase when a wx.grid.GridTableBase exists? I acutually tried to use the latter but then the sheet came out empty, i.e., the SetTable failed silently… please enlighten me here…

thanks again,

Flávio

···

On 10/17/06, Christian Kristukat ckkart@hoc.net wrote:

Flavio Coelho <fccoelho gmail.com> writes:

Hi,Is there a way to fill a grid from a numpy array without having to iterate
through it and issuing SetCellValue for each cell?same for reading?thanks,–

Flávio Codeço Coelho

here’s a minimal example:

import wx
import wx.grid
import numpy as N

class TableBase(wx.grid.PyGridTableBase):
def init(self, data):
wx.grid.PyGridTableBase._init
_(self)
self.data = data

def GetNumberRows(self):
    return self.data.shape[0]

def GetNumberCols(self):
    return self.data.shape[1]

def GetValue(self, row, col):

    return self.data[row][col]

class Grid(wx.grid.Grid):
def init(self, parent, data):
wx.grid.Grid.init(self, parent, -1)

    table = TableBase(data)
    self.SetTable

(table, True)

class Frame(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1)

    pan = wx.Panel(self, -1)
    grid = Grid(pan, N.random.rand(30,40))

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(grid, 1, wx.EXPAND)
    pan.SetSizer(sizer)
    pan.Layout()

class App(wx.App):
def OnInit(self):
frame = Frame()
frame.Show()

    return True

app = App()
app.MainLoop()

Christian


To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org


Flávio Codeço Coelho
registered Linux user # 386432

“Laws are like sausages. It’s better not to see them being made.”
Otto von Bismark

Flavio Coelho wrote:

Thanks for the example,

but I still have a few questions:

1)It seem almost as slow as when I looped through all cells calling "SetCellValue()", is this right?

The difference is that instead of filling the grid in the loop before first use, the grid asks you for each item as it needs it for display. So just as much data is moved around with this approach, (assuming you view all items) it just redistributes it over time.

2) After I implemented it as sugested below, my SetColLabelValue('col_title') stopped having any effect! Whats going on?

Because by default the col and row labels are retrieved from the table, and since you are providing a custom table that doesn't implement the label methods then it falls back to what the base class does, which is just calculate a label based on the row or col number. You can override these methods (shown in C++ syntax here, adjust for Python) in your table class to provide the labels:

     virtual wxString GetRowLabelValue( int row );
     virtual wxString GetColLabelValue( int col );

And these if you want to be able to set the values from the grid instance:

     virtual void SetRowLabelValue( int row, const wxString& value );
     virtual void SetColLabelValue( int col, const wxString& value );

3) Why do you subclass TableBase from wx.grid.PyGridTableBase when a wx.grid.GridTableBase exists? I acutually tried to use the latter but then the sheet came out empty, i.e., the SetTable failed silently... please enlighten me here...

Because wxGridTableBase is a C++ class that doesn't know how to reflect its virtual method calls to methods of a class derived from it in Python. PyGridTableBase does know how to do that.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks Robin,
that help a lot.

···

On 10/18/06, Robin Dunn robin@alldunn.com wrote:

Flavio Coelho wrote:

Thanks for the example,

but I still have a few questions:

1)It seem almost as slow as when I looped through all cells calling
“SetCellValue()”, is this right?

The difference is that instead of filling the grid in the loop before
first use, the grid asks you for each item as it needs it for display.
So just as much data is moved around with this approach, (assuming you

view all items) it just redistributes it over time.

  1. After I implemented it as sugested below, my
    SetColLabelValue(‘col_title’) stopped having any effect! Whats going on?

Because by default the col and row labels are retrieved from the table,

and since you are providing a custom table that doesn’t implement the
label methods then it falls back to what the base class does, which is
just calculate a label based on the row or col number. You can override

these methods (shown in C++ syntax here, adjust for Python) in your
table class to provide the labels:

 virtual wxString GetRowLabelValue( int row );
 virtual wxString GetColLabelValue( int col );

And these if you want to be able to set the values from the grid instance:

 virtual void SetRowLabelValue( int row, const wxString& value );
 virtual void SetColLabelValue( int col, const wxString& value );
  1. Why do you subclass TableBase from wx.grid.PyGridTableBase when a
    wx.grid.GridTableBase exists? I acutually tried to use the latter but
    then the sheet came out empty, i.e., the SetTable failed silently…

please enlighten me here…

Because wxGridTableBase is a C++ class that doesn’t know how to reflect
its virtual method calls to methods of a class derived from it in
Python. PyGridTableBase does know how to do that.


Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org


Flávio Codeço Coelho
registered Linux user # 386432

“Laws are like sausages. It’s better not to see them being made.”

Otto von Bismark