11# GUI created with PyQT/PySide
2- import re
2+ # import re
33import sys
4+ from io import StringIO
45import os
56import subprocess
67from PySide6 import QtCore , QtWidgets
78from PySide6 .QtCore import QSize
89pypef_root = os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' ))
10+ #sys.path.append(pypef_root)
11+ from pypef import __version__
912from pypef .main import run_main
1013
1114
1215EXEC_API_OR_CLI = ['api' , 'cli' ][0 ]
1316
1417
18+ class Capturing (list ):
19+ """https://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call"""
20+ def __enter__ (self ):
21+ self ._stdout = sys .stdout
22+ sys .stdout = self ._stringio = StringIO ()
23+ return self
24+ def __exit__ (self , * args ):
25+ self .extend (self ._stringio .getvalue ().splitlines ())
26+ del self ._stringio # free up some memory
27+ sys .stdout = self ._stdout
28+
29+
1530def capture (command ):
1631 proc = subprocess .Popen (
1732 command ,
@@ -22,8 +37,8 @@ def capture(command):
2237 return out , err , proc .returncode
2338
2439
25- out , err , exitcode = capture ([f'python' , f'{ pypef_root } /run.py' , '--version' ])
26- version = re .findall (r"[-+]?(?:\d*\.*\d.*\d+)" , str (out ))[0 ]
40+ # out, err, exitcode = capture([f'python', f'{pypef_root}/run.py', '--version'])
41+ # version = re.findall(r"[-+]?(?:\d*\.*\d.*\d+)", str(out))[0]
2742
2843button_style = """
2944QPushButton {
@@ -71,7 +86,7 @@ def __init__(
7186
7287 # Texts #############################################################################
7388 layout = QtWidgets .QGridLayout (self )
74- self .version_text = QtWidgets .QLabel (f"PyPEF v. { version } " , alignment = QtCore .Qt .AlignRight )
89+ self .version_text = QtWidgets .QLabel (f"PyPEF v. { __version__ } " , alignment = QtCore .Qt .AlignRight )
7590 self .ncores_text = QtWidgets .QLabel ("Single-/multiprocessing" )
7691 self .llm_text = QtWidgets .QLabel ("LLM" )
7792 self .regression_model_text = QtWidgets .QLabel ("Regression model" )
@@ -266,8 +281,8 @@ def __init__(
266281 layout .addWidget (self .textedit_out , 8 , 0 , 1 , - 1 )
267282
268283 self .process = QtCore .QProcess (self )
269- self .process .setProcessChannelMode (QtCore .QProcess .MergedChannels )
270- self .process .readyReadStandardOutput .connect (self .on_readyReadStandardOutput )
284+ # self.process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
285+ # self.process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
271286 self .process .started .connect (lambda : self .button_help .setEnabled (False ))
272287 self .process .finished .connect (lambda : self .button_help .setEnabled (True ))
273288 self .process .started .connect (lambda : self .button_mklsts .setEnabled (False ))
@@ -463,8 +478,20 @@ def exec_pypef_cli(self, cmd):
463478 self .textedit_out .append ("=" * 104 + "\n " )
464479
465480 def exec_pypef_api (self , cmd ):
466- run_main (argv = cmd )
467- pass
481+ self .textedit_out .append (f'Executing command:{ cmd } ' )
482+ try :
483+ with Capturing () as captured_output :
484+ run_main (argv = cmd )
485+ print (captured_output )
486+ for cap_out_text in captured_output :
487+ self .textedit_out .append (cap_out_text )
488+ except Exception as e : # anything
489+ self .textedit_out .append (f"Provided wrong inputs! Error: { e } " )
490+ finally :
491+ self .process .finished .connect (self .process_finished )
492+ if self .c > 0 :
493+ self .textedit_out .append ("=" * 104 + "\n " )
494+
468495
469496 def process_finished (self ):
470497 self .version_text .setText ("Finished..." )
0 commit comments