prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.1k stars 717 forks source link

3.0.40 causing interactive gui to not work from ipython console #1809

Open jansopousek opened 7 months ago

jansopousek commented 7 months ago

When starting the qt apps from ipython console, those apps are not working properly. This can be reproduced by following code in the ipython console:

import matplotlib.pyplot as plt

%matplotlib qt6

plt.plot([0,1])

The plot window is opened but frozen, the qt app is not properly started

jonathanslenders commented 7 months ago

Which Python version are you using?

jonathanslenders commented 7 months ago

Most likely this is caused by some changes that were done because asyncio.get_event_loop() is deprecated now. I don't remember when exactly this was done, but there were a few attempts at least. IPython installs an event loop which contains the inputhook for QT and prompt_toolkit is supposed to pick it up using get_event_loop(). However, this is deprecated, and get_running_loop() won't return a loop when it's not running, so on Python 3.10, 3.11 and 3.12, we create a new loop without the input hook, which causes the QT application to freeze.

We have to rethink this a bit. I think prompt_toolkit.application.Application.run() (and prompt_toolkit.prompt.PromptSession.prompt() should take an inputhook as an argument that we install ourselves in the application that we run.)

I'll see if I can come up with an PR this weekend. @jansopousek: Would you be willing to test, if I push an PR for both prompt_toolkit and IPython?

cc: @Carreau

jonathanslenders commented 7 months ago

This should fix it on the prompt_toolkit side: https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1810

This should fix it on IPython's side: https://github.com/ipython/ipython/pull/14241

Both fixes have to be published.

jansopousek commented 7 months ago

We are using python 3.11. Thanks for fixing it up. It is not that straightforward for me to test those, since I don't have any of those packages installed from source. But by the code which I provided it should be pretty easy to replicate.

jonathanslenders commented 7 months ago

As a workaround I released prompt_toolkit 3.0.41 earlier today, which detects whether it's being called from IPython and adjusts accordingly to avoid this bug. A fix on IPython's side should follow soon.

molinav commented 7 months ago

I can confirm this issue also on Windows 11 using Python 3.11.4, IPython 8.17.2, matplotlib 3.7.4, and both Tk (builtin) or PyQt5 5.15.10. A simple interactive IPython session with plt.figure() shows a plotting window that does not react and hangs. The error occurs to me for both prompt-toolkit versions 3.0.41 and 3.0.40. If I downgrade to 3.0.39, IPython works again normally.

benz0li commented 7 months ago

This should fix it on the prompt_toolkit side: #1810

This and https://github.com/prompt-toolkit/python-prompt-toolkit/commit/8c601936b11d91388edc653df80aa34bd9cb1606 does not fix it for radian.

Cross reference: https://github.com/randy3k/radian/issues/446

This should fix it on IPython's side: ipython/ipython#14241

@randy3k, please please consult with @jonathanslenders on what needs to be changed on radian's side.

Both fixes have to be published.