1

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.

Miguel
  • 2,130
  • 1
  • 11
  • 26
  • Can you just grab the containing dom element, and set it to `""`? – Gerrat Jan 04 '23 at 12:56
  • Do you mean by using the inspector? That would work for me as a single user, but not for a product I want to offer to others (specially people that don't necessarily know how to use the inspector) – Miguel Jan 04 '23 at 16:08
  • 1
    I meant by using javascript/pyscript and something like `document.getElementByID`, to grab the actual DOM node that pyscript is in, then clearing it. You could actually delete the node, then re-insert it at the same point. – Gerrat Jan 04 '23 at 18:30
  • I guess that could make an answer – Miguel Jan 05 '23 at 09:58

1 Answers1

2

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.

Miguel
  • 2,130
  • 1
  • 11
  • 26