0

I am wondering if there is a way to to interactively send text from a python script to qtconsole. The closest possible approach I found is through subprocess, but it does not work. It follows the code that I tried:

import subprocess

QtConsole = subprocess.Popen(["jupyter", "qtconsole"], stdin=subprocess.PIPE)
QtConsole.wait()
QtConsole.stdin.write(("ls" + "\n").encode())
QtConsole.stdin.flush()

The qtconsole open, but nothing is sent.

mypy signal an Item "None" of "Optional[IO[bytes]]" has no attribute "write" [union-attr] Error in the last two lines, which suggest me that no instances of Popen are returned, and I am suspicious that the error may be there (but I don't know how to fix it).

If I remove the line QtConsole.wait() nothing seems to happen.

I am using Python 3.10.

I would like to have an interactive session with qtconsole, therefore I am using Popen instead of subprocess.run().

Any suggestions?

Barzi2001
  • 989
  • 8
  • 24
  • 1
    `jupyter` is a UI process, and the QtConsole is a GUI control. It isn't going to have a stdin. You can't, for example, do `echo ls | jupyter qtconsole`, can you? – Tim Roberts May 04 '23 at 21:04
  • Nope. It just open jupyter qtconsole. Ok then, is there any way to send text to jupyter console? I am going to update my question. – Barzi2001 May 04 '23 at 21:06
  • Not easily, no. I suppose you could use the `pywinauto` module to find the proper window and simulate keystrokes. – Tim Roberts May 04 '23 at 21:10
  • Auch! But if jupyter is a UI process, once it started qtconsole, can't I send text to jupyter and jupyter to forward to qtconsole? (I am aware that this may be a very stupid question though) – Barzi2001 May 04 '23 at 21:14
  • 1
    No. QtConsole is a GUI control that's running inside of Jupyter, which is itself a GUI app. Jupyter does not have a stdin/stdout. – Tim Roberts May 04 '23 at 21:19
  • Fair enough. Thanks. – Barzi2001 May 04 '23 at 21:26
  • You may be asking the wrong question, QtConsole is a frontend to jupyter, that's what you want to communicate with. See https://stackoverflow.com/questions/9977446/connecting-to-a-remote-ipython-instance and https://github.com/WillForan/wf-utils/blob/master/jpsnd (taken from https://stackoverflow.com/questions/52616522/how-to-send-commands-to-ipython-console-session) – tevemadar May 06 '23 at 12:27

0 Answers0