0

So, Im currently taking just limited data from a ZeroMQ Messaging system appending it all to a Dataframe and then turning the Dataframe into a pandas model with code I acquired from someone on stack. Then running that model through a PyQt5 Tableview.

Every time I run the tableview code, after converting my Dataframe to a model for Tableview the kernal just dies. I tried to at least handle a exception but it wont even raise a runtimeerror that is super general. It just dies every time....

For the purpose of the test im using a CSV with data to try to get this into a workable model. You can use any csv or data you have on your end to test this as theres no hard coded formatting.

from PyQt5 import QtCore, QtGui, QtWidgets
import pandas as pd


class PandasModel(QtCore.QAbstractTableModel):     
    def __init__(self, df = pd.DataFrame(), parent=None): 
        QtCore.QAbstractTableModel.__init__(self, parent=parent)
        self._df = df

    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if orientation == QtCore.Qt.Horizontal:
            try:
                return self._df.columns.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                # return self.df.index.tolist()
                return self._df.index.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if not index.isValid():
            return QtCore.QVariant()

        return QtCore.QVariant(str(self._df.ix[index.row(), index.column()]))

    def setData(self, index, value, role):
        row = self._df.index[index.row()]
        col = self._df.columns[index.column()]
        if hasattr(value, 'toPyObject'):
            # PyQt4 gets a QVariant
            value = value.toPyObject()
        else:
            # PySide gets an unicode
            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, parent=QtCore.QModelIndex()): 
        return len(self._df.index)

    def columnCount(self, parent=QtCore.QModelIndex()): 
        return len(self._df.columns)

    def sort(self, column, order):
        colname = self._df.columns.tolist()[column]
        self.layoutAboutToBeChanged.emit()
        self._df.sort_values(colname, ascending= order == QtCore.Qt.AscendingOrder, inplace=True)
        self._df.reset_index(inplace=True, drop=True)
        self.layoutChanged.emit()   


def createview(title, model):
    try:
        view = QtWidgets.QTableView()
        view.setWindowview
    except:
        raise RuntimeError("I know python!")


if __name__=="__main__":
    df=pd.read_csv("C:\Excel Sheets\Test_CSV_6-18-18.csv")
    model = PandasModel(df)
    createview("Model", model)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    Possible duplicate of [QWidget: Must construct a QApplication before a QPaintDevice](https://stackoverflow.com/questions/11452937/qwidget-must-construct-a-qapplication-before-a-qpaintdevice) – eyllanesc Jul 09 '18 at 13:26
  • Use: `def createview(title, model): import sys app = QtWidgets.QApplication(sys.argv) view = QtWidgets.QTableView() view.setWindowTitle(title) view.setModel(model) view.show() sys.exit(app.exec_())` – eyllanesc Jul 09 '18 at 13:27
  • Oh my goodness. You are amazing. Thank you – MontyCapital Jul 09 '18 at 14:08
  • I recommend you read a PyQt tutorial before continuing, your problem indicates that you do not know anything about PyQt. – eyllanesc Jul 09 '18 at 14:09
  • You are partially correct. Im not sure why I missed the whole view.show() but the sys.exit(app.exe_()) i didnt know. Im going to hop on that tutorial – MontyCapital Jul 09 '18 at 14:19

0 Answers0