Search on blog:

Python: How to redirect output from print() to textarea in Brython

Original sys.stdout has method write() and function print() uses this sys.stdout.write() to send text to console.

To redirect print() to element on page or in GUI you have to create class with method write() which gets text and puts on page or in GUI and assign this class to sys.stdout. You may also assign original sys.stdout to some variable to assign it back to sys.stdout to send text again to console.

---

Minimal working example for Brython.

Page has <textarea> with id="console"` and I create class MyOutput which uses write to put text in this document["console"]. When I assing instance of this class to sys.stdout then print() sends text to <textarea>.

<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js"></script>
    </head>
    <body onload="brython(1)">
        <textarea id="console"></textarea>
        <script type="text/python3">
            import sys
            from browser import document

            class MyOutput:
                def __init__(self):
                    self.console = document["console"]

                def write(self, text):
                    self.console.text += text

            my_output = MyOutput()

            original_stdout = sys.stdout

            sys.stdout = my_output

            print("Hello World 1")
            print("Hello World 2")
            print("Hello World 3")

            sys.stdout = original_stdout

        </script>
    </body>
</html>

You can also assign instance to variable my_output = MyOutput() and use with print(..., file=my_output) to display only selected text.

<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js"></script>
    </head>
    <body onload="brython(1)">
        <textarea id="console"></textarea>
        <script type="text/python3">
            import sys
            from browser import document

            class MyOutput:
                def __init__(self):
                    self.console = document["console"]

                def write(self, text):
                    self.console.text += text

            my_output = MyOutput()

            print("Hello World 1", file=my_output)
            print("Hello World 2", file=my_output)
            print("Hello World 3", file=my_output)

        </script>
    </body>
</html>

You can also use directly my_output.write() but then you have to add '\n' to text.

<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js"></script>
    </head>
    <body onload="brython(1)">
        <textarea id="console"></textarea>
        <script type="text/python3">
            import sys
            from browser import document

            class MyOutput:
                def __init__(self):
                    self.console = document["console"]

                def write(self, text):
                    self.console.text += text

            my_output = MyOutput()

            my_output.write("Hello World 1" + "\n")
            my_output.write("Hello World 2" + "\n")
            my_output.write("Hello World 3" + "\n")

        </script>
    </body>
</html>

Interesujące linki [2021.01.09]

  • toscrape.com

    Web Scraping Sandbox stworzony przez Scrapinghub

    Fikcyjna księgarnia, która desperacko chce być zeskrapowana. To bezpieczne miejsce dla początkujących uczących się we scrapingu, jak również dla deweloperów sprawdzających swoje umiejętności scrapingu

    Dostępne na: books.toscrape.com

    Strona, która wyświetla cytaty znanych ludzi. Ma wiele odmian strony, które pokazują cytaty …

« Page: 1 / 58 »