Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Python output if console.log() is called after Python code #712

Closed
3 tasks done
marimeireles opened this issue Aug 23, 2022 · 5 comments
Closed
3 tasks done

No Python output if console.log() is called after Python code #712

marimeireles opened this issue Aug 23, 2022 · 5 comments
Labels
backlog issue has been triaged but has not been earmarked for any upcoming release type: feature New feature or request

Comments

@marimeireles
Copy link
Member

marimeireles commented Aug 23, 2022

Checklist

  • I added a descriptive title
  • I searched for other issues and couldn't find a solution or duplication
  • I already searched in Google and didn't find any good information or help

What happened?

Working on #622 I found a problem that I think it's coming from upstream pyscript.py.
When you add a js print after python code that should be printing in the screen, nothing is printed. To reproduce try the following snippet:

from datetime import datetime
now = datetime.now()
now.strftime("%m/%d/%Y, %H:%M:%S")
console.log("this is coming from js")

The string "this is coming from js" shows up in the console normally, but the python output in the page remains empty.
E.g using the hello_world example, this is all I see in my screen:

Hello world!
This is the current date and time, as computed by Python:

I'm exploring this, but feel free to add thoughts if you have any!
Thanks!

What browsers are you seeing the problem on? (if applicable)

Firefox, Chrome

Console info

No response

Additional Context

No response

@marimeireles marimeireles added type: bug Something isn't working needs-triage Issue needs triage labels Aug 23, 2022
@marimeireles
Copy link
Member Author

Ok, it doesn't happen if I use:

print('🥑')
console.log('banana 🍌')

@marimeireles
Copy link
Member Author

Edit: I don't think this is coming from Pyodide :)

@JeffersGlass
Copy link
Member

JeffersGlass commented Aug 23, 2022

Hi @marimeireles! It think this is actually expected (if unintuitive) behavior that I've tripped on a couple times.

When the <py-script> tag evaluates, Pyscript calls pyodide's runPython/runPythonAsync, which are wrappers around Pyodide's eval_code() and eval_code_async() respectively.

By default, eval_code() and eval_code_async() return the value of the last expression, unless that expression has a trailing semicolon. (See the return_mode and quiet_trailing_semicolon parameters.)

This bit of code:

from datetime import datetime
now = datetime.now()
now.strftime("%m/%d/%Y, %H:%M:%S")

Therefore returns the date formatted as a string, which then gets written to the script's output element.

If you console.log(...) to the end of that script, now the script returns the return value of console.log, which is None, so nothing gets printed.

Probably the clearer way for a programmer to achieve what this code does is to write the final line as print(now.strftime("%m/%d/%Y, %H:%M:%S")), so it's obvious that it's meant to be output.


I've run into this issue when my code ends with a function that I'm using for it's side effects that also returns a value, like document.appendChild():

<!-- index.html -->
<div id="parent"></div>
# my_script.py 
from js import document
p = document.createElement('p')
p.innerText = "Hello, world!"
document.getElementById("parent").appendChild(p)

Shows up as:

image

because appendChild() returns the element it just appended. Adding a semicolon to the end solves this, but it's annoying.


Regardless, I think this is not terribly clear behavior. Maybe as part of #622, the behavior of <py-script> tags should no longer be to write the return value to the output element? It's really more of what a repl would do, printing the value of the last expression, rather than what I think of a script doing.

@marimeireles
Copy link
Member Author

Hey @JeffersGlass thanks for chiming in!
And very useful input. :)
I'm rewriting the PyScript's outputs and will take your points into consideration. Thank you!

@marimeireles marimeireles added type: feature New feature or request and removed type: bug Something isn't working needs-triage Issue needs triage labels Aug 24, 2022
@marimeireles marimeireles added the backlog issue has been triaged but has not been earmarked for any upcoming release label Oct 5, 2022
@marimeireles
Copy link
Member Author

Fixed by #749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog issue has been triaged but has not been earmarked for any upcoming release type: feature New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants