Printing output to a component

This is a fun little module that allows you to log all print statements to a label component of your choosing.

# component_logger.py
import sys

class ComponentLogger:
    def __init__(self, component):
        self.component = component

    def write(self, text):
        self.component.text += text
        sys.__stdout__.write(text)

    def flush(self):
        sys.__stdout__.flush()

    def close(self):
        pass


def reset():
    sys.stdout = sys.__stdout__


def log_to(component):
    sys.stdout = ComponentLogger(component)

Then use it in a form like:

from component_logger import log_to

class Form1(Form1Template):
    def __init__(self, **properties):
        log_to(self.label_1)
        print("logging test")

Clone:


linked post: [updated] Webpush Notifications using ONLY Anvil and Python
@tobias.carlbom you might find this useful

4 Likes

Ohhhhhhh, that is just awesome! I will be using this module A LOT :smiley:

1 Like