0

I'm a beginner in python and pyqt5. I'm trying to populate a tableView on clicking the button. On clicking the pushbutton I want to call another class which can generate the table when passing the dataframe. So far code shows nothing but empty tableview.

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5.QtCore import QAbstractTableModel, Qt

df=pd.read_csv("testing_tweets.csv" )

Dataframe Class:

class pandasModel(QAbstractTableModel):

  def __init__(self, data):
    QAbstractTableModel.__init__(self)
    self._data = data
    print("in")

  def rowCount(self, parent=None):
    return self._data.shape[0]
    print("in")


  def columnCount(self, parent=None):
    return self._data.shape[1]
    print("in")

  def data(self, index, role=Qt.DisplayRole):
    if index.isValid():
        if role == Qt.DisplayRole:
            return str(self._data.iloc[index.row(), index.column()])
    return None

  def headerData(self, col, orientation, role):
    if orientation == Qt.Horizontal and role == Qt.DisplayRole:
        return self._data.columns[col]
    return None

Main Class:

class Ui_MainWindow(object):
  def extract(self):
    df=pd.read_csv("testing_tweets.csv" )
    app = QApplication(sys.argv)
    model = pandasModel(df)
    view = QTableView()
    view.setModel(model)
    view.resize(800, 600)
    view.show()
    sys.exit(app.exec_())  

  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(640, 480)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.tableView = QtWidgets.QTableView(self.centralwidget)
    self.tableView.setGeometry(QtCore.QRect(10, 10, 621, 331))
    self.tableView.setObjectName("tableView")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(20, 350, 75, 23))
    self.pushButton.setObjectName("pushButton")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 21))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
    self.pushButton.clicked.connect(self.extract)

  def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
  import sys
  app = QtWidgets.QApplication(sys.argv)
  MainWindow = QtWidgets.QMainWindow()
  ui = Ui_MainWindow()
  ui.setupUi(MainWindow)
  MainWindow.show()

  sys.exit(app.exec_())

Any help will be highly appreciated!

Kashan
  • 348
  • 3
  • 19
  • I recommend you to read the answers of the duplicate answers so that you understand the errors, for this one time you will provide a direct solution: https://gist.github.com/eyllanesc/e56520f8d9f881706edae56554fb841b, in other cases I hope you will carry out a deeper investigation and not just excuse yourself that you are a beginner – eyllanesc Oct 22 '19 at 20:21
  • Thanks for the help. however the code from the given link does not show any output. . – Kashan Oct 22 '19 at 20:36
  • change `testing_tweets.csv` to fullpath – eyllanesc Oct 22 '19 at 20:37
  • the file is in the same directory – Kashan Oct 22 '19 at 20:38
  • But the relative path of the file is not with respect to the .py but to the path from where you launch the application so to rule out the error of the path, if the error persists then I will analyze where the problem is, otherwise I will indicate the necessary code to create the absolute path. – eyllanesc Oct 22 '19 at 20:39
  • Unfortunately, the code still does not show any output. – Kashan Oct 22 '19 at 20:44
  • I have tested with a .csv and it works correctly so that for me the .csv may have problems, share the file – eyllanesc Oct 22 '19 at 20:46
  • Thanks again for your help. https://drive.google.com/open?id=1p2i3De3fzaynWggUQfVVGCTsIfDnGymb – Kashan Oct 22 '19 at 20:53
  • I have updated the code by adding code to get the file path assuming it is next to the .py https://gist.github.com/eyllanesc/e56520f8d9f881706edae56554fb841b – eyllanesc Oct 22 '19 at 21:23
  • Yes its working. appreciate your efforts – Kashan Oct 23 '19 at 05:03

0 Answers0