How to access the output of a script via JS?

Hello,

I’d like to ask how can one access the output of a script (example: user runs custom code on a py-repl tag and it provides the output on screen). How is it possible to grab this output as a JS variable?

Thanks!

1 Like

Interesting, with pyscript to use js var we can

from js import foo

if its the otherway around maybe we could use pyodide proxy.toJS?
Still learning pyscript ways…
Maybe others know how?

Could you please provide an example of passing a js variable onto a pyscript tag?

<h1 id="example"></h1>
<script>
    window.foo = 'Hello There';
</script>
<py-script output="example">
from js import foo
foo
</py-script>

Thank you! And what if I only want the script to run after the user has pressed a button for instance?

well just add event-listener or put it under py-button
(Correct me if im wrong though, this is based on the examples provided in the repo)
e.g

<py-button>
Do this()
</py-button>

Update 1:
Please declare the function before calling the event from py-button

e.g.

<h2 id="bipbop"></h2>
<br>
<py-script>
bipped = Element("bipbop")

def click_event(*ags, **kwgs):
    bipped.element.innerText += "Biopsy"
</py-script>

<py-button id="new-task-btn" label="Add Biopsy!">
          def on_click(evt):
            click_event()
        </py-button>
<br>

Thanks! However, in case I want to add just a print statement inside click_event, it doesn’t appear on the screen. Do you know what may be a solution for that?

Well I suggest for now dont use print.

The function that i gave above is basically the same intention to print and appear on screen.
We have to grab the Element, and add a “string” or variable to that element
hence:

foo.element.innerText += “Text”

This will add the Text and appear it on screen

1651924551833