daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 56 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. self.data = pd.read_csv('transactions.txt', sep='t', header=None)
  2.     self.data.columns = ["Name", "Price", "Action"]
  3.     self.model = PandasModel(self.data)
  4.     self.tableViewTransaction.setModel(self.model)
  5.    
  6. class PandasModel(QtCore.QAbstractTableModel):
  7.     def __init__(self, df = pd.DataFrame(), parent=None):
  8.         QtCore.QAbstractTableModel.__init__(self, parent=parent)
  9.         self._df = df
  10.  
  11.     def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
  12.         if role != QtCore.Qt.DisplayRole:
  13.             return QtCore.QVariant()
  14.  
  15.         if orientation == QtCore.Qt.Horizontal:
  16.             try:
  17.                 return self._df.columns.tolist()[section]
  18.             except (IndexError, ):
  19.                 return QtCore.QVariant()
  20.         elif orientation == QtCore.Qt.Vertical:
  21.             try:
  22.                 # return self.df.index.tolist()
  23.                 return self._df.index.tolist()[section]
  24.             except (IndexError, ):
  25.                 return QtCore.QVariant()
  26.  
  27.     def data(self, index, role=QtCore.Qt.DisplayRole):
  28.         if role != QtCore.Qt.DisplayRole:
  29.             return QtCore.QVariant()
  30.  
  31.         if not index.isValid():
  32.             return QtCore.QVariant()
  33.  
  34.         return QtCore.QVariant(str(self._df.ix[index.row(), index.column()]))
  35.  
  36.     def setData(self, index, value, role):
  37.         row = self._df.index[index.row()]
  38.         col = self._df.columns[index.column()]
  39.         if hasattr(value, 'toPyObject'):
  40.             # PyQt4 gets a QVariant
  41.             value = value.toPyObject()
  42.         else:
  43.             # PySide gets an unicode
  44.             dtype = self._df[col].dtype
  45.             if dtype != object:
  46.                 value = None if value == '' else dtype.type(value)
  47.         self._df.set_value(row, col, value)
  48.         return True
  49.  
  50.     def rowCount(self, parent=QtCore.QModelIndex()):
  51.         return len(self._df.index)
  52.  
  53.     def columnCount(self, parent=QtCore.QModelIndex()):
  54.         return len(self._df.columns)
  55.  
  56.     def sort(self, column, order):
  57.         colname = self._df.columns.tolist()[column]
  58.         self.layoutChanged.emit()
  59.         self._df.sort_values(colname, ascending= order == QtCore.Qt.AscendingOrder, inplace=True)
  60.         self._df.reset_index(inplace=True, drop=True)
  61.         self.layoutChanged.emit()
  62.    
  63. def recordTransaction(self):
  64.         name = self.comboBoxStock.currentText()
  65.         price = self.lineEditMoney.text()
  66.         action = self.comboBoxAction.currentText()
  67.  
  68.         self.data.loc[len(self.data.index)] = [name, price, action]
  69.         self.tableViewTransaction.model().layoutChanged.emit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top