Skip to main content

PyQt/PySide interface to handle logging data to file particularly well suited for multithreaded programs.

Project description

https://img.shields.io/pypi/v/dvg-pyqt-filelogger https://img.shields.io/pypi/pyversions/dvg-pyqt-filelogger https://github.com/Dennis-van-Gils/python-dvg-pyqt-filelogger/actions/workflows/python-package.yml/badge.svg https://coveralls.io/repos/github/Dennis-van-Gils/python-dvg-pyqt-filelogger/badge.svg https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/badge/License-MIT-purple.svg

DvG_PyQt_FileLogger

Provides a PyQt interface to handle logging data to a file particularly well suited for multithreaded programs.

Supports PyQt5, PyQt6, PySide2 and PySide6.

Installation:

pip install dvg-pyqt-filelogger

Example usage

The following are snippets of code, not a full program.

from qtpy import QtWidgets as QtWid
from dvg_pyqt_filelogger import FileLogger

# Main/GUI thread
# ---------------

class MainWindow(QtWid.QWidget):
    def __init__(
        self,
        log: FileLogger,
        parent=None,
        **kwargs
    ):
        super().__init__(parent, **kwargs)

        # Create a record button
        self.record_button = QtWid.QPushButton("Click to start recording to file")
        self.record_button.setCheckable(True)
        self.record_button.clicked.connect(lambda state: log.record(state))

class YourDataGeneratingDevice:
    reading_1 = 0.0

device = YourDataGeneratingDevice()

def write_header_to_log():
    log.write("elapsed [s]\treading_1\n")

def write_data_to_log():
    log.write(f"{log.elapsed():.3f}\t{device.reading_1:.4f}\n")

log = FileLogger(
    write_header_function=write_header_to_log,
    write_data_function=write_data_to_log
)

log.signal_recording_started.connect(
    lambda filepath: window.record_button.setText(
        f"Recording to file: {filepath}"
    )
)
log.signal_recording_stopped.connect(
    lambda: window.record_button.setText(
        "Click to start recording to file"
    )
)

window = MainWindow(log)

# Data acquisition and/or logging thread
# --------------------------------------

# New data got acquired
device.reading_1 = 20.3

# Must be called whenever new data has become available
log.update()

API

Class FileLogger

FileLogger(
    write_header_function: Callable | None = None,
    write_data_function: Callable | None = None,
    encoding: str = "utf-8",
)

Changelog

1.4.0 (2024-06-24)

  • Using qtpy library instead of my own Qt5/6 mechanism

  • Support for Numpy 2.0

  • Extended API and docstrings

  • Added argument encoding, defaulting to “utf-8”

  • Added type hints

  • Added method get_filepath()

1.3.0 (2023-02-27)

  • Raise ImportError instead of general Exception

1.2.0 (2022-09-13)

  • Added support for PyQt5, PyQt6, PySide2 and PySide6

1.1.0 (2021-05-13)

  • Added support for numpy.savetxt()

1.0.0 (2020-08-11)

  • First release on PyPI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dvg_pyqt_filelogger-1.4.0.tar.gz (10.3 kB view hashes)

Uploaded Source

Built Distribution

dvg_pyqt_filelogger-1.4.0-py3-none-any.whl (9.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page