You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, I obviously realize that rich is intended to be used in a terminal, not a GUI. That said, I have a CLI application that is built using rich and works great. Now, in a separate project, I'm trying to use the main function of that CLI (which uses a rich progress bar) in a GUI. Essentially I'm adding a GUI wrapper around the CLI. What I'd like to do is capture the progress bar to update the GUI. Below is a simplified example of what I'd like to do.
In the run method below I can call function_from_another_module just fine, which, of course, outputs to the terminal and doesn't update the GUI progress bar. I need a way to get the progress from this function --- is there a way to add a callback or something to the rich progress bar? I didn't see a way in the documentation.
I'd be great to also get all information from the progress bar, not only the % complete. For example, description, estimated time remaining, etc. That way I could add that stuff to the GUI as well.
importosimportsysimportthreadingimporttimefromPySide6.QtGuiimportQIcon, QtfromPySide6.QtWidgetsimport (
QApplication,
QLabel,
QProgressBar,
QVBoxLayout,
QWidget,
)
fromrich.progressimporttrack# Ideally I would make no changes to this function --- in the actual use case,# this function is imported from another library that uses rich to display a# progress bar in a CLI application. Effectively, I'm trying to write a simple# GUI wrapper to this CLI application, hopefully without making any change to# the CLI application, so that it doesn't need to be aware of whether it is # running in the CLI or a GUI. However, I did write the CLI, so if I need to# add an optional keyword argument to provide a callback function or something,# I can do that. Or switch from using track to an actual Progress object.deffunction_from_another_module(n=50):
fornintrack(range(n), description='Processing...'):
time.sleep(0.1)
classWorkerThread(threading.Thread):
def__init__(self, n, progress):
threading.Thread.__init__(self)
self.n=nself.progress=progressdefrun(self):
function_from_another_module()
# for i in range(self.n):# self.progress.setValue((i + 1) / self.n * 100.0)# time.sleep(0.1)classMainWindow(QWidget):
def__init__(self):
QWidget.__init__(self)
vbox=QVBoxLayout()
label=QLabel('Processing..')
label.setAlignment(Qt.AlignCenter)
vbox.addWidget(label)
self.progress_bar=QProgressBar()
self.progress_bar.setAlignment(Qt.AlignCenter)
vbox.addWidget(self.progress_bar)
self.setLayout(vbox)
self.setGeometry(300, 300, 300, 50)
defcloseEvent(self, event):
os._exit(0)
if__name__=='__main__':
app=QApplication(sys.argv)
window=MainWindow()
window.show()
WorkerThread(n=50, progress=window.progress_bar).start()
app.exec_()
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
First, I obviously realize that rich is intended to be used in a terminal, not a GUI. That said, I have a CLI application that is built using rich and works great. Now, in a separate project, I'm trying to use the main function of that CLI (which uses a rich progress bar) in a GUI. Essentially I'm adding a GUI wrapper around the CLI. What I'd like to do is capture the progress bar to update the GUI. Below is a simplified example of what I'd like to do.
In the
run
method below I can callfunction_from_another_module
just fine, which, of course, outputs to the terminal and doesn't update the GUI progress bar. I need a way to get the progress from this function --- is there a way to add a callback or something to the rich progress bar? I didn't see a way in the documentation.I'd be great to also get all information from the progress bar, not only the % complete. For example, description, estimated time remaining, etc. That way I could add that stuff to the GUI as well.
Beta Was this translation helpful? Give feedback.
All reactions