Jupyter Python Script waiting keyboard input without Enter strike

Dear Friends,

My script wants to get the input from the keyboard without the need of hitting the ENTER key. The input() function works for getting the input, but it requires hitting the enter key. I have also tried several libraries for being used in .py scripts, but none of them works in Jupyterlab. Can someone suggest to me a way that works for getting keyboard input instantly for Jupyter? Thank you!

Yun

AFAIK Jupyter does not have a full replacement for stdin as used by normal programs.

The function input() is redefined in Jupyter so that it works in the Jupyter’s cells. There you probably cannot use other functions reading stdin as in normal Python text terminal programs.

Also it is not clear how exactly you want the function to behave. When you use input() then pressing Enter indicates that you finished your input. How do you want to indicate end of input in your scenario? Do you rather want to read individual keypresses?

The code in Jupyter notebook is split between multiple cells. Also the notebook has its own JupyterLab user interface so you cannot define your own interactivity of the program the same way as in the plain Python running on a text console.

IMHO most often ipywidgets are being used to implement interactivity in Jupyter. Here is a simple example of an on-screen button: Widget Events - button example

I think “stealing” the keyboard from the JupyterLab web interface would be a more complicated task than the on-screen button.

1 Like