I've constructed a window in PyQt 5, which by clicking on the "optimize" button, the program reads the "Gurobi-model.lp" file (click here to get the file), and optimizes it by the help of the Gurobi software. How can I display the logs of the Gurobi on a QTextBrowser?
I found some functions in the Gurobi such as OutputFlag, LogFile, and LogToConsole. Might these functions be helpful not?
For those who are not familiar with Gurobi, the Gurobi optimizer uses Python as an interface, and produces some logs that allows you to track the progress of the optimization. These logs are printed in the console during the optimization, and somehow, responding my question doesn't need to know anything about the Gurobi.
In the below code, I've found a way to show the logs in the QTextBrowser, but the logs are represented when the optimization process is completely done. I want the logs to be represented exactly during the optimization process.
import sys
from PyQt5.QtWidgets import *
from gurobipy import *
from io import *
class MyWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.pb = QPushButton(self.tr("optimize"))
self.log_text = QTextBrowser()
layout = QVBoxLayout(self)
layout.addWidget(self.pb)
layout.addWidget(self.log_text)
self.setLayout(layout)
self.pb.clicked.connect(self.optimize)
def optimize(self):
f = StringIO()
sys.stdout = StringIO()
self.m = read('Gurobi-model.lp')
self.m.optimize()
self.log_text.append(sys.stdout.getvalue() )
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()