Display log in real time with PyQt
Project description
pyqt-realtime-log-widget
Display command log in real time with PyQt widget.
This is using subprocess and psutil to execute/manuever the process, using QThread and a variety of signals defined by me to demonstrate process' log.
Requirements
-
PyQt5
-
psutil
Install
python -m pip install pyqt-realtime-log-widget --upgrade
If you want to test or modify this, clone it.
It has test code already so you can just run the logWidget.py.
Class/Method Overview
-
LogWidget(parent=None)
-
setCommand(command: str)
- set the command that you want to see the log in real time. -
started
- signal emitted after command being started. -
updated(str)
- signal emitted after log being updated. updated line as an argument. -
stopped
- signal emitted after log being stopped. -
finished
- signal emitted after command being finished -
setStartText(start_text: str)
- set the text you want to add when process begins to execute -
setStopText(stop_text: str)
- set the text you want to add when process being stopped -
setFinishText(finish_text: str)
- text when process being finished -
getStartText
,getStopText
,getFinishText
are also provided.
-
-
LogDialog()
- Simply put, dialog version of LogWidget. Currently under development, just useLogWidget
.getLogWidget()
- I believe this is self-explanatory.
Feature
-
You can pause/resume/stop the command
-
Vertical scroll bar always at the bottom while log is displaying
-
Show the warning dialog when you try to close the widget, if you give the parent widget to the constructor such as
LogWidget(self)
. Process is suspended while warning dialog is showing. If you press Yes, process will be terminated and widget will be closed. If you press no, process will be keep running until it is finished. -
You can use the signal like
started
,updated
,finished
as i mentioned before.
Example
Example 1
You need an example.py file. make it, write the code like below.
for i in range(1000):
for j in range(1000000):
pass
print(f'Log {i}')
After doing it, make main.py file or something like that and write the code below.
from PyQt5.QtWidgets import QApplication
from pyqt_realtime_log_widget import LogWidget
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = LogWidget()
comm = 'python example.py'
window.setCommand(comm)
window.show()
sys.exit(app.exec())
Run it.
Result 1
Example 2
Make example.py file.
for i in range(100):
for j in range(1000000):
pass
print(f'Log {i}')
Make the Python script and write the code below
from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
from pyqt_realtime_log_widget import LogWidget
class Widget(QWidget):
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
self.__btn = QPushButton('Start')
self.__btn.clicked.connect(self.__start)
self.__logWidget = LogWidget(self)
self.__logWidget.layout().setContentsMargins(0, 0, 0, 0)
self.__logWidget.finished.connect(self.__finished)
lay = QVBoxLayout()
lay.addWidget(self.__btn)
lay.addWidget(self.__logWidget)
self.setLayout(lay)
def __start(self):
self.__btn.setEnabled(False)
comm = 'python example.py'
self.__logWidget.setCommand(comm)
def __finished(self):
self.__btn.setEnabled(True)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
widget = Widget()
widget.show()
sys.exit(app.exec())
Run.
Result 2
Example 3 (LogDialog)
from PyQt5.QtWidgets import QApplication
from pyqt_realtime_log_widget import LogDialog
//...
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
dialog = LogDialog()
proc = 'python example.py'
dialog.setWindowTitle('Logging...')
logWidget = dialog.getLogWidget()
logWidget.setCommand(proc)
dialog.show()
sys.exit(app.exec())
Result? It's just a dialog version of LogWidget, so it is pointless to upload the image.
Note
Currently stop and finish signal is not well-distinguished. When process being stopped, finished event will be emitted as well, so it will be very confusing. I will fix it or how about you fix it for me?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pyqt-realtime-log-widget-0.0.11.tar.gz
.
File metadata
- Download URL: pyqt-realtime-log-widget-0.0.11.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/0.0.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 843b6b3a57b42bba22181ccdc2f94e749bf78ece75c77c121a2aa9f6f86716f5 |
|
MD5 | 0373ec95c6d980772bcc29ba21c6ce8c |
|
BLAKE2b-256 | cf85b29ecc19a0648ee4315b3247f7da28bd8f1241f53d294c95885a1b579bb1 |
File details
Details for the file pyqt_realtime_log_widget-0.0.11-py3-none-any.whl
.
File metadata
- Download URL: pyqt_realtime_log_widget-0.0.11-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/0.0.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2eb14dbfed9cf9be314bb1e4f50e00ce22a8f23435a4da78b59fc836f983a604 |
|
MD5 | b852c37e807cea9d316b5834b03c3892 |
|
BLAKE2b-256 | 1a2b12213850cb6256b2fbd732627eb212bb74e7741e72ecef37bfe347aa8edf |