PySide: Updating pandas Dataframe imports



  • In my program I try to show different .csv/.txt files with the help of pandas module. The problem that I have is that only the first import is shown, while all further imports are not shown (the first import remains) ... although the data is read from the file.

    In example, I try to first upload the first .csv file:
    @1.0 2.0 3.0 a
    4.0 5.0 6.0 b@

    which is shown and then I import the other .csv file:
    @a b c d e f g h
    1 2 3 4 5 6 7 8
    q w e r t y u i@

    but is not shown.

    I would be happy of any suggestion why this is not working.

    @from PySide import QtGui
    from PySide import QtCore
    import pandas as pd
    QVariant = lambda value=None: value

    class IdentificationWidget(QtGui.QWidget):
    def init(self, data):
    super().init()

        self.data = data
        self.dataModel = DataFrameModel()
        self.dataTable = QtGui.QTableView()
    
    def update(self):
        print("update is being made")
        self.dataModel.signalUpdate(self.data)
        self.dataModel.printShape()
        self.dataTable.setModel(self.dataModel)
        self.main_layout = QtGui.QVBoxLayout()
        self.main_layout.addWidget(self.dataTable)
        self.setLayout(self.main_layout)
    

    class DataFrameModel(QtCore.QAbstractTableModel):
    ''' data model for a DataFrame class '''
    def init(self):
    super(DataFrameModel, self).init()
    self.df = pd.DataFrame()

    def setDataFrame(self, dataFrame):
        self.df = dataFrame
    
    def signalUpdate(self, dataIn):
        self.df = dataIn
        self.layoutChanged.emit()
    
    def printShape(self):
        print(self.df.shape[0], self.df.shape[1])
    #------------- table display functions -----------------
    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QVariant()
    
        if orientation == QtCore.Qt.Horizontal:
            try:
                return self.df.columns.tolist()[section]
            except (IndexError, ):
                return QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                # return self.df.index.tolist()
                return self.df.index.tolist()[section]
            except (IndexError, ):
                return QVariant()
    
    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QVariant()
    
        if not index.isValid():
            return QVariant()
    
        return QVariant(str(self.df.ix[index.row(), index.column()]))
    
    def flags(self, index):
            flags = super(DataFrameModel, self).flags(index)
            flags |= QtCore.Qt.ItemIsEditable
            return flags
    
    def setData(self, index, value, role):
        row = self.df.index[index.row()]
        col = self.df.columns[index.column()]
    
        dtype = self.df[col].dtype
        if dtype != object:
            value = None if value == '' else dtype.type(value)
        self.df.set_value(row, col, value)
        return True
    
    def rowCount(self, index=QtCore.QModelIndex()):
        return self.df.shape[0]
    
    def columnCount(self, index=QtCore.QModelIndex()):
        return self.df.shape[1]
    

    class MainWindow(QtGui.QMainWindow):
    """Main window of the application."""
    def init(self):
    super(MainWindow, self).init()
    self.main_widget = QtGui.QWidget(self)

        # -- Stacked widget is used to -- stack widgets.
        self.stacked_widget = QtGui.QStackedWidget()
    
        # -- Menu bar.
        self.init_menu_bar()
    
        # -- Layout is put together
        grid = QtGui.QGridLayout()
        grid.addWidget(self.stacked_widget, 0, 0)
    
        self.main_widget.setLayout(grid)
    
        self.setCentralWidget(self.main_widget)
        self.setGeometry(50, 50, 800, 600)
    
    def init_menu_bar(self):
        """Initialize menu bar."""
        menu_bar = self.menuBar()
        file_menu = menu_bar.addMenu('&File')
    
        # .. Add uff import option.
        import_action = QtGui.QAction('&Open/Import', self)
        import_action.setShortcut('Ctrl+O')
        import_action.setStatusTip('Open file ...')
        file_menu.addAction(import_action)
        import_action.triggered.connect(self.getFiles)
    
    def getFiles(self):
        file_name, filtr = QtGui.QFileDialog.getOpenFileName(
            self, self.tr("Open File"), "/.", self.tr("(*.csv *.txt);;"))
        self.data = pd.read_csv(file_name, sep=' ')
        # -- Update all widgets.
    
        widgets = {0: IdentificationWidget}
        widget = widgets[0](self.data)
        widget.update()  # widget update
    
        self.stacked_widget.addWidget(widget)
    

    if name == 'main':
    import sys
    app = QtGui.QApplication(sys.argv)

    main_window = MainWindow()
    main_window.show()
    
    sys.exit(app.exec_())@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.