0

When I run the following code using python test.py the code works. Another console window appears, writes hello world and waits to be closed. But when I use Jupyter and execute the whole code in one cell, then "hello world" never appears on the screen of the new console window.

I though that there could be some buffering problems, but the documentation states that stdin is not affected by any buffering. From the Jupyter side I use flush() so the message is supposed to be sent immediately. stdin of the process is connected to the PIPE so the program is supposed to see the message.

Could someone help me send the message into the new window using stdin?

The original idea of how to open a new console window comes from this post: How can I open two consoles from a single script

from subprocess import Popen, PIPE, CREATE_NEW_CONSOLE
import sys

command = [
    sys.executable,
    "-u",
    "-c",
    """import sys
for line in sys.stdin:
    sys.stdout.write(line)
    sys.stdout.flush()
input()
""",
]
proc = Popen(
    command,
    stdin=PIPE,
    bufsize=1,
    universal_newlines=True,
    creationflags=CREATE_NEW_CONSOLE,
)
proc.stdin.write("hello world\n")
proc.stdin.flush()
proc.wait()
keiv.fly
  • 2,753
  • 4
  • 20
  • 38
  • I'm trying to figure out something similar... This might help https://stackoverflow.com/questions/50069402/how-to-change-stdin-in-jupyter-notebook –  Aug 23 '21 at 09:08

0 Answers0