[IPython-dev] messaging protocol

Robert Kern robert.kern at gmail.com
Fri Apr 8 17:05:34 EDT 2011


On 4/8/11 3:32 PM, Fernando Perez wrote:
> On Fri, Apr 8, 2011 at 1:21 PM, Thomas Kluyver<takowl at gmail.com>  wrote:
>>
>> In fact, now that we're using AST instead of code blocks, we could actually
>> do what you suggest. We could check the last node, and only run it
>> interactively if it was a single expression. Whether or not that's what we
>> want to do, I don't know: any views?
>
> That's a very good point.  The fragile heuristics we had were
> precisely because we lacked this information.  But I really do like
> this suggestion, because I think it provides the most intuitive
> semantics.  Things like:
>
> for i in range(10):
>    plot(foo[i])
>
> won't produce 10 different Out[] outputs, and yet any last-block
> expression, even if it contains some multline string or other complex
> formatting that makes it be more than one *line of text* will still be
> executed interactively, yielding just one result.
>
> I'm very much +1 on this idea.  Big benefit of your recent tackling
> inputsplitter!! Awesome.

I don't think we need to solve this in the splitter. You can do everything you 
need to do in the display trap. The logic is very simple. The display trap sets 
the displayhook before the code In[N] is executed. Every time the displayhook 
gets called during this execution, the display trap records the object and its 
formatted representation, overwriting whatever was there. Once the execution is 
done, *then* the formatted representation is given to the reply message (or 
printed if in the terminal frontend) and the history DB (if it is storing 
outputs), and the object is shoved into Out[N]. Then the display trap is cleared 
before In[N+1] is executed.

This allows code like "x; y = x+1" to put x into Out[N] even though the last AST 
node is not an expression. It's not *entirely* clear to me that this is a real 
use case, but I think it would be less surprising behavior.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the IPython-dev mailing list