#!/usr/bin/env python # coding: utf-8 # In[1]: kernel = get_ipython().kernel # In[2]: kernel._parent_header # In[3]: import sys from contextlib import contextmanager @contextmanager def temp_parent(parent_ident, parent_header): """Context manager temporarily setting the parent header for routing output to a particular cell """ save_parent = (kernel._parent_ident, kernel._parent_header) sys.stdout.flush() sys.stderr.flush() kernel.set_parent(parent_ident, parent_header) try: yield finally: sys.stdout.flush() sys.stderr.flush() kernel.set_parent(*save_parent) # In[4]: def from_this_cell(arg, *, show=print): with temp_parent(*from_this_cell.target): show(arg) from_this_cell.target = (kernel._parent_ident, kernel._parent_header) # workaround frontend: # prevent the status:idle message from firing for this cell, # because the frontend will discard callbacks once # idle indicates that output is complete. _save_status = kernel._publish_status def _disable_once(*args, **kw): kernel._publish_status = _save_status kernel._publish_status = _disable_once # In[5]: print("I'm here") from_this_cell("Up there!") print("back")