Repository: ipython/ipython

Issue:
Jupyter shebang cells won't show partially written lines
When a shebang cell (i.e., %%sh, %%bash, etc.) in a Jupyter notebook writes/flushes output, the output doesn't show up until the script ends or a newline is written. This hurts the usability of programs that indicate progress by writing periodically on the same line.

Changing this ipython logic to read whatever text is available (rather than waiting for a newline) would fix the issue: https://github.com/ipython/ipython/blob/18e45295c06eb9/IPython/core/magics/script.py#L215. (I'm not familiar with asyncio's streams, so it might also be necessary to ensure the fd is in non-blocking mode.)

Below is a GIF demonstrating the issue. Observe that:

1. The output regular Python cell updates in realtime, even if it doesn't include a newline.
2. The output an equivalent shebang cell does not update in realtime.
3. The output of a shebang cell that includes newlines updates in realtime.

![Image](https://github.com/user-attachments/assets/fd25968f-0400-4722-af4a-63e190413c27)


Discussion:
To reproduce:

```python
%%sh
python -c '
import time
for _ in range(10):
    time.sleep(0.25)
    print(".", flush=True, end="")
'
```

Simplified:

```
%%script python
import time
for _ in range(10):
    time.sleep(0.25)
    print(".", flush=True, end="")
```


Even more:

```
%%python
import time
for _ in range(10):
    time.sleep(0.25)
    print(".", flush=True, end="")
```



Patch:
diff --git a/IPython/core/magics/script.py b/IPython/core/magics/script.py
index 8abc22efe6..3bfc4d8d67 100644
--- a/IPython/core/magics/script.py
+++ b/IPython/core/magics/script.py
@@ -220,7 +220,7 @@ def in_thread(coro):
 
         async def _readchunk(stream):
             try:
-                return await stream.readuntil(b"\n")
+                return await stream.read(100)
             except asyncio.exceptions.IncompleteReadError as e:
                 return e.partial
             except asyncio.exceptions.LimitOverrunError as e:


Test patch:
diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
index a107da0c6c..4e167007c5 100644
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -12,6 +12,8 @@
 from importlib import invalidate_caches
 from io import StringIO
 from pathlib import Path
+from time import sleep
+from threading import Thread
 from subprocess import CalledProcessError
 from textwrap import dedent
 from time import sleep
@@ -1259,6 +1261,37 @@ def test_script_defaults():
             assert cmd in ip.magics_manager.magics["cell"]
 
 
+async def test_script_streams_continiously(capsys):
+    ip = get_ipython()
+    # Windows is slow to start up a thread on CI
+    is_windows = os.name == "nt"
+    step = 3 if is_windows else 1
+    code = dedent(
+        f"""\
+    import time
+    for _ in range(3):
+        time.sleep({step})
+        print(".", flush=True, end="")
+    """
+    )
+
+    def print_numbers():
+        for i in range(3):
+            sleep(step)
+            print(i, flush=True, end="")
+
+    thread = Thread(target=print_numbers)
+    thread.start()
+    sleep(step / 2)
+    ip.run_cell_magic("script", f"{sys.executable}", code)
+    thread.join()
+
+    captured = capsys.readouterr()
+    # If the streaming was line-wise or broken
+    # we would get `012...`
+    assert captured.out == "0.1.2."
+
+
 @magics_class
 class FooFoo(Magics):
     """class with both %foo and %%foo magics"""