Search code examples
pythonweb-frontendpyscript

How to clear a terminal in pyscript (py-terminal)


The idea is to clear the output of the python REPL provided by pyscript, embedded in a website.

I have tried the regular ways used in the OS console (os.system("clear"), print("\033c") and similar), but they don't work.

I have not found anything in the documentation of the py-repl or py-terminal elements.


Solution

  • A possible approach is to implement a function in python that uses the js module provided by pyscript to modify the DOM (see How to perform DOM manipulation using pyscript).

    So a simplified version of my solution would look like this:

    <py-script>
    from js import document as _DOC
    def clear():
      ter = _DOC.getElementById("my-terminal")
      ter.innerHTML = ''
    </py-script>
    

    Put this code before your REPL and now you can execute the clear() function from it and the terminal will be cleared.