-
-
Notifications
You must be signed in to change notification settings - Fork 863
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
Interactive console in pyodide-py #1125
Conversation
Can you temporarily make this into a Stream redirection should be handled with a context manager e.g. from contextlib import redirect_stdout, redirect_stderr
import pyodide
def run_code(code):
with redirect_stdout(mystdoutstream), redirect_stderr(mystderrstream):
pyodide.eval_code(code) Exactly what you don't want is for the existence of a terminal object somewhere to funnel all writes to stdout into your terminal. |
Thanks @hoodmane for looking at this.
Can't understand why you need me to do this.
At first, I planed to do it like this. But ...
I think that it could be exactly what you want! You can stop the redirection with |
I am constantly opening up the browser console and typing Effectively there are two separate use cases: normal people and developers. I am okay if you make this stream change to
I would try to solve this in another way before going to the "alter global state" approach which I think is a last resort. Also, if the |
One other noteworthy thing: normally if you write to |
Thanks @hoodmane for looking at this.
Can't get your point: (after the first command is run,) stdout were already constantly redirected to terminal. I am not changing the behavior here. If you want an html page that loads Pyodide so that you can do some
It could. When implementing p5.js support in Basthon, I can have a
Can't get any trace from |
The current |
What do you mean by this? I am having trouble finding where in this code this is taken care of. |
Actually now that I'm looking into it, the behavior is kind of funny: printing to stderr works correctly until you execute the first command in the repl. After the first command in the repl is executed, all writes to stdout and stderr are swallowed. I can fix it to use context manager in |
Just do something like
This is what I was saying by:
This is because |
The point is that I want |
@casatir The point is that you don't have great reading comprehension. I said three times I wanted a tool called
|
Ouch! I was just saying that to go your way and support the fact that we need both. You start the review with a dev tool in mind but I was advertising for a showcase in #875:
This was just to make it clear that, at first, we have different purpose in mind. Moreover, please be kind. With this kind of answer, you will ruin the motivation of volunteers that takes on their (work and family) time for working on Pyodide and that are not fluent in english. |
Thanks for this improvement @casatir and for the review @hoodmane !
Yes, absolutely. It's OK to have a technical disagreement, and a PR author can refuse changes suggested in a PR, while still having a pleasant conversation. As far as I understand the implied meaning was "I won't change it, unless you have better arguments" rather than literately "I don't understand". I know it's easy to misunderstand each other. In this case, I would also prefer to keep a single
likely in a different PR, unless the changes in this PR prevent that? |
After my repeated attempts at compromise, @casatir's tone comes across as accusatory and lacking of acknowledgement of the compromises.
"at first" is a qualification that you didn't give in your original message. It also makes your comment rather stale information, since I suggested other positions based on the stuff you've already said. It is of course true that the user I am thinking of first is myself, but I am looking for some option that can make us both happy (of course making us both happy is different than doing the best thing or even from doing a reasonable thing, but hopefully it is at least vaguely related).
This is an accusation that I don't care about end users? I'm thinking: He isn't happy with the compromise options I offered? He just doesn't want my feedback? Or did he just not read my posts very carefully and didn't notice that I changed my position at all? It sounds like a hard bargain to me. |
Yes, I am now convinced by that. The current behavior is weird, this preserves the weird current behavior while making unrelated improvements. |
The main source of my confusion in this discussion is that I've been taking the ground truth state of the system to be what happens in a fresh copy of |
Thanks @rth.
Totally agree but it's not OK to have personal attack like this:
Wait wait wait.
Does this suggest I want to do wrong or unreasonable things?
There is no accusation here, I promise. I have no doubt you are really concern by end users. I had just not understand you proposes this compromise for this PR.
Totally agree that this stupid bug makes a lot of confusion in our discussion. Let's forget all this. I have a proposition that will certainly makes all of us happy. I am not really interested by the class InteractiveConsole(code.InteractiveConsole):
def __init__(
self,
locals: Optional[dict] = None,
stdout_callback: Optional[Callable[[str], None]] = None,
stderr_callback: Optional[Callable[[str], None]] = None,
persistent_stream_redirection: bool = False,
): The WDYT? If you are OK, I'll add this to this PR. |
Sounds perfect!
I don't have a super strong opinion about what the default value should be, I still think it could be a good idea to have a |
@casatir Sorry for the misunderstanding, I really appreciate your work! I will try not to read your tone as so confrontational in the future. This has been a really rough year and I am not exactly a pillar of emotional stability.
Right, I meant to imply that you could do nothing in this PR and then I would do something to deal with it in a future PR. I guess I never explicitly said that, but I was thinking I would just copy one of the old
That's definitely my bad!
I will try.
No sorry don't mean to suggest that. I mean that theoretically "contributors are happy" is independent from "a good decision was made", you can have either one without the other, both, or neither. |
Great!
Me neither.
Could be nice to have @rth though on this. And what about an URL parameter like |
|
src/pyodide-py/pyodide/console.py
Outdated
""" A banner similar to the one printed by the real Python interpreter. """ | ||
# copyied from https://github.com/python/cpython/blob/799f8489d418b7f9207d333eac38214931bd7dcc/Lib/code.py#L214 | ||
cprt = 'Type "help", "copyright", "credits" or "license" for more information.' | ||
return f"Python {sys.version} on {sys.platform}\n{cprt}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would show "Python 3.8 on emscripten". Technically it's true but it might be confusing to users. I'm not sure what else we could put there, WebAssembly VM, pyodide, browser? Also keeping the snake emoji we had before would be a nice touch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shows
Python 3.8.2 (default, Jan 4 2021, 12:17:49)
[Clang 6.0.1 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com on emscripten
Type "help", "copyright", "credits" or "license" for more information.
>>>
which is not nice, you are right...
I see something like
Python 3.8.2 (default, Jan 4 2021, 12:17:49) on WebAssembly VM
Type "help", "copyright", "credits" or "license" for more information.
for the default banner and
Python 3.8.2 (default, Jan 4 2021, 12:17:49) on WebAssembly VM
Welcome to the Pyodide terminal emulator 🐍
Type "help", "copyright", "credits" or "license" for more information.
for the one customized in console.html
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe:
Welcome to the Pyodide terminal emulator 🐍
Python 3.8.2 (default, Jan 4 2021, 12:17:49) on WebAssembly VM
Type "help", "copyright", "credits" or "license" for more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer the last version (with the welcome message at the beginning)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too, this is the one I commited.
|
||
Parameters | ||
---------- | ||
stdout_callback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locals also need a docstring entry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Corrected.
assert my_stderr == "bar\n" | ||
|
||
# redirections disabled at destruction | ||
shell.restore_stdstreams() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this test fails before this line does it mean that we get monkeypatched stdout/stderr globally?
Maybe it would be good add a pytest fixure that stores and restores the stdout just in case. Something like,
@pytest.fixture
def restore_stdstreams():
# save streams to local variables
yield
# restore streams after the test function exits
def test_interactive_console_streams(restore_stdstreams):
...
BTW, it's interesting that pytest also captures stderr/stdout but does it at the level of file system descriptors https://docs.pytest.org/en/stable/capture.html#setting-capturing-methods-or-disabling-capturing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, it could be a bad issue!
Minor comments above but otherwise LGTM. Thanks!
Also +1 for it (not in this PR). |
Also please add a changelog entry. |
5c4939e
to
a68c4cb
Compare
Sorry for the force push but there were conflicts with #979 so I rebased on master. |
term.handlePythonResult = function(result) { | ||
c.restore_stdstreams(); | ||
term.resume(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.html
is not really clean with this restore_stdstreams()
in the JS code but they will disappear in the part 2/3 of the console improvement, I promise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Please merge upstream master into this PR to resolve new conflicts.
Actually I fixed conflicts via UI. Will merge on green CI. |
src/templates/console.html
Outdated
} | ||
|
||
let term = $('body').terminal( | ||
var term = $('body').terminal( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var term = $('body').terminal( | |
let term = $('body').terminal( |
unless it was an intentional change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, probably forgotten when merging with #979.
ca08d2e
to
808888c
Compare
Conflicts should be fixed. |
Merging master should be fine
|
Thanks @dalcde. You mean |
It's working very nicely. But now we definitely need to fix #877 |
""" A banner similar to the one printed by the real Python interpreter. """ | ||
# copyied from https://github.com/python/cpython/blob/799f8489d418b7f9207d333eac38214931bd7dcc/Lib/code.py#L214 | ||
cprt = 'Type "help", "copyright", "credits" or "license" for more information.' | ||
return f"Python {platform.python_version()} {platform.python_build()} on WebAssembly VM\n{cprt}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently prints,
Python 3.8.2 ('default', 'Jan 15 2021 09:54:23') on WebAssembly VM
while the classical Python REPL displays,
Python 3.8.6 (default, Sep 25 2020, 09:36:53)
It would be nice to remove the quotes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arf, sorry for this. Good catch! It will be corrected in #1141. Is that OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sure, thanks!
This is the 1/3 part of the #875 splitting. It addresses:
console.html
)In a separate PR, I will address:
And in a last PR, I will propose a completion system.