[IPython-dev] Loop over a set of notebook cells?

MinRK benjaminrk at gmail.com
Mon Mar 17 14:44:07 EDT 2014


You can execute a cell range with a little javascript (e.g. in a
%%javascript cell):

var start = 2;
var stop = 4;

for (var i = start; i < stop; i++) {
    var cell = IPython.notebook.get_cell(i);
    cell.execute();
}




On Mon, Mar 17, 2014 at 9:05 AM, Patrick Surry <patrick.surry at gmail.com>wrote:

> Yes, I'd seen that, but it seemed to fall into the external/batch category
> (write a script that post-processes a notebook) rather than something
> that you could do interactively within the notebook workflow.
>
> Cheers,
> Patrick
>
>
> From: Clyde Fare <clyde.fare at gmail.com>
>
>
> I think MinRK did something like this here:
>
> http://nbviewer.ipython.org/gist/minrk/6011986
>
> His NotebookLoader class does loop over the cells.
>
> Clyde
>
>
>
> On 15 March 2014 11:18, Patrick Surry <patrick.surry at gmail.com> wrote:
>
> > What about a "loop delimiter" magic where you insert a matching pair of
> > %magics bracketing a sequence of regular cells, and then it basically
> > unrolls the loop for you?
> >
> > i.e. when you run the magic, on the first loop iteration it executes each
> > cell in place, with the first loop value, and for each subsequent
> > iterations, it appends a copied sequence of new cells after the %end,
> > evaluated with the corresponding loop element.   So if you looped over a
> > list of two items, you'd end up with a second copy of all your looped
> cells
> > that have been executed with the second item in the list. (see example
> > sketch below)
> >
> > So it's doing a kind of repeated block copy & paste (useful in it's own
> > right), unrolling the loop, and leaving you the new results to explore
> and
> > further tweak.  Maybe it would also mark itself as executed somehow
> > (comment itself out?!) so the notebook is stable if you re-execute all
> > cells?
> >
> > You'd rapidly want multi-cell delete I guess :)  Perhaps other bracketing
> > magics like %block cut / %endblock / %block paste ??
> >
> > Patrick
> >
> > Example with cells a, b, c:
> >
> >     %begin for item in [1,2]
> >     ---
> >     a
> >     ---
> >     b
> >     ---
> >     c
> >     ---
> >     %end
> >
> > when executed becomes:
> >
> >     # %begin for item in [1, 2]
> >     ---
> >     a [item=1]
> >     ---
> >     b [item=1]
> >     ---
> >     c [item=1]
> >     ---
> >     # %end
> >     ---
> >     a [item=2]
> >     ---
> >     b [item=2]
> >     ---
> >     c [item=2]
> >     ---
> >
> >
> > From: Thomas Kluyver <takowl at gmail.com>
> >
> > >On 14 March 2014 09:46, Nikolas Tezak <ntezak at stanford.edu> wrote:
> >
> > > I've been thinking about this for a while as well. Although this
> doesn't
> > > quite address looping, I think one idea might be to use cell tags to
> > create
> > > certain "run sets" of cells and then have a little widget/ui component
> > that
> > > allows for selecting and running all cells of a certain tag. So, in
> that
> > > respect a good interface for marking/selecting multiple cells and then
> > > tagging them or running them or copying them would be great.
> >
> >
> > Tags on cells is something that we want to do - there are a lot of useful
> > things that could be done with it.
> >
> > Thomas
> >
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140317/6b6b151c/attachment.html>


More information about the IPython-dev mailing list