[IPython-dev] Highlight lines of code in code cells?
Mark Voorhies
mark.voorhies at ucsf.edu
Mon Dec 2 23:23:01 EST 2013
On 12/02/2013 05:41 PM, Gabriel Becker wrote:
> Unless I completely miss my guess, shell.history_manager is going to look
> at the code that has been executed. This is related to what is in the
> cells, of course, but it isn't the same.
>
> Also, the concept of "cell number" if I am correct in assuming you are
> referring to the numbers that appear next code cells after the run, is that
> they are not persistently connected to current cell content and don't have
> a 1-to-1 correspondence to the actual cells during non-headless execution.
>
> i.e. if a code cell is executed, then edited and executed again, but the
> cell number targeted for diffing isn't changed, a magic using
> shell.history_manager will grab the contents of the cell *when it was run
> the first time*
All of this is correct.
> as opposed to what I think is conceptually intended, which
> is what is in the cell now.
It is true that my approach won't work for anything that the kernel hasn't seen,
so it can't do live updates of the diff as cells are edited (and JS _would_ be
the right approach for that sort of thing). Outside of that, I think most
behaviors can be achieved given the right interface.
Here's what the current iteration (https://github.com/markvoorhies/ipython/commit/216ea233413a20061e6c77863798d449be6200c9)
can handle:
%diff 6 7
--> gives diff of cells [6] and [7]. If [7] is edited (e.g., becoming [9]), the %diff output does not change,
even if re-run. %diff 6 9 _does_ diff the new content.
%diff 6
--> gives diff of the last cell (e.g., [7]) with cell [6]. If cell [7] is edited and re-run (e.g., becoming [9]),
re-running the %diff _does_ diff [6] and [9].
%diff
--> gives diff of the last two cells (e.g. [6] and [7]). This is not useful for re-running (because the %diff cell
itself becomes part of the history), but is a nice shorthand for the most common use case.
%diff A B
--> diffs the most recent cells with first lines "##label:A" and "##label:B" respectively. If either cell is
edited and re-run, then re-running the %diff _does_ diff the latest content.
(This is a place-holder, pending an IPythonic mechanism for labeling cells)
The last example allows this work flow:
1) Create cell "##label:A"
2) Create cell "##label:B" (via M-C,M-V in cell A and editing)
3) %diff A B
4) Fix up A and B with little edits as needed, re-running the edited cell and "%diff A B" with each edit.
5) Create cell "##label:C" (via M-C,M-V in cell B and editing)
6) %diff B C
7) Fix up C with little edits as needed, re-running the edited cell and "%diff B C" with each edit.
8) etc.
For teaching, I would tend to skip the "little edits" steps, in which case (3) can become "%diff A"
and (6) can become "%diff B".
--Mark
More information about the IPython-dev
mailing list