[IPython-dev] metadata attribute for streams

Jason Grout jason-sage at creativetrax.com
Thu Jun 28 15:12:54 EDT 2012


As you probably know from our many messages, we're implementing a sage 
frontend/backend using the IPython framework.  We'd like to attach some 
metadata to stdout and stderr messages, for example, indicating where 
the frontend should put the output.  It would be really convenient if 
sys.stdout and sys.stderr (which I think is 
IPython.zmq.iostream.OutStream, right?) had a 'metadata' attribute that 
could be set and would be added to each outgoing message.  This would 
also change the messaging spec to add a 'metadata' attribute to the 
content dict for stream messages (just like display_data messages 
already have).

We'd use it something like this (not tested...):

from contexlib import contexmanager
@contexmanager
def stream_metadata(metadata):
     old_metadata = (sys.stdout.metadata.copy(), sys.stderr.metadata.copy())
     sys.stdout.update(metadata)
     sys.stderr.update(metadata)
     try:
         yield None
     finally:
         sys.stdout.metadata, sys.stderr.metadata = old_metadata

with stream_metadata({'location': 4}):
     print 'hello world' # goes to location 4


I can submit a pull request, but thought I'd ask for comments first.

Thanks,

Jason



More information about the IPython-dev mailing list