Home > OS >  "TypeError: 'int' object is not callable when calling function" in MultipartEnco
"TypeError: 'int' object is not callable when calling function" in MultipartEnco

Time:10-02

I'm getting error "TypeError: 'int' object is not callable" at line 80 in <lambda> e, lambda monitor: self.progressBar.setValue(int(100*monitor.bytes_read/total_size)) and I can't figure why. Please, any insights will be greatly appreciated.

Basically, I want to extract the percentage from the progress bar from tqdm and pass its value to a QProgressBar from PyQt5 so the POST request (file upload) can be tracked via the GUI. I'm open to alternatives to this approach.

Here's the full code:

import sys
import requests
from PyQt5 import QtCore, QtWidgets
from pathlib import Path
from tqdm import tqdm
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor

class MenuWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(MenuWidget, self).__init__(parent)
        self.init_ui()

    def init_ui(self):

        self.filepath_lineedit = QtWidgets.QLineEdit()
        self.progressBar = QtWidgets.QProgressBar() # progress bar
        select_button = QtWidgets.QPushButton(
            text="Choose File",
            clicked=self.select_file
        )
        self.name_lineedit = QtWidgets.QLineEdit()
        self.log_textedit = QtWidgets.QTextBrowser()
        upload_button = QtWidgets.QPushButton(
            text="Upload",
            clicked=self.upload_file
        )
        hlay = QtWidgets.QHBoxLayout()
        hlay.addWidget(self.filepath_lineedit)
        hlay.addWidget(select_button)
        lay = QtWidgets.QFormLayout(self)
        lay.addRow("File:", hlay)
        hlay.addWidget(self.progressBar)
        lay.addRow(self.log_textedit)
        lay.addRow(upload_button)

    @QtCore.pyqtSlot()
    def select_file(self):
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(
            self, 
            "Open file", 
            QtCore.QDir.currentPath(), 
        )
        if filename:
            self.filepath_lineedit.setText(filename)

    @QtCore.pyqtSlot()
    def upload(self):
        url = "http://localhost:8000/api/upload/"
        path = self.filepath_lineedit.text()
        data = {'dir':'/uploads/', 'submit':'Submit'}
        files = {'upload': open(path, 'rb')}
        r = requests.post(url, data=data, files=files)
        response = r.json()
        self.log_textedit.setText(str(response))

    def upload_file(self):
        self.progressBar.setValue = 0
        upload_url = "http://localhost:8000/api/upload/"
        filepath = self.filepath_lineedit.text()
        fields = {'upload': open(filepath, 'rb')}

        path = Path(filepath)
        total_size = path.stat().st_size
        filename = path.name

        with tqdm(
            desc=filename,
            total=total_size,
            unit="B",
            unit_scale=True,
            unit_divisor=1024,
        ) as bar:
            with open(filepath, "rb") as f:
                fields["upload"] = (filename, f)
                e = MultipartEncoder(fields=fields)
                #m = MultipartEncoderMonitor(
                #    e, lambda monitor: bar.update(monitor.bytes_read - bar.n),
                #)
                m = MultipartEncoderMonitor(
                    e, lambda monitor: self.progressBar.setValue(int(100*monitor.bytes_read/total_size)),
                ) # Line which is giving TypeError
                headers = {"Content-Type": m.content_type}
                r = requests.post(upload_url, data=m, headers=headers)
                response = r.json()
                self.log_textedit.setText(str(response))

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = MenuWidget()
    w.show()
    sys.exit(app.exec_())

CodePudding user response:

You're trying to call a function named self.progressBar.setValue():

self.progressBar.setValue(int(100*monitor.bytes_read/total_size))

But earlier you set that same variable to an integer:

self.progressBar.setValue = 0

You can't do that.

  • Related