[IPython-dev] Highlight lines of code in code cells?

Greg Wilson gvwilson at third-bit.com
Mon Dec 2 18:19:05 EST 2013


On 2013-12-02 5:03 PM, Mark Voorhies wrote:
> On 12/02/2013 09:26 AM, Eric Matthes wrote:
>> I am not very familiar with the codebase yet, and I have few "good long
>> afternoons" at this time of the year. I don't want to clutter my notebooks
>> in a way that will be laborious to clean, so I think will use the
>> commenting approach I described above, but also write a quick script that
>> strips the notebooks of these formatting comments. This way anyone who is
>> using the raw notebooks for their own purposes can choose to use my hack,
>> or have a set of clean, unhighlighted notebooks to work with.
>>
>> I like the efficiency of your approach, though, so when I do get a nice
>> afternoon to hack I will play with that approach. If anyone else has a good
>> suggestion, I'd love to hear it as well.
>>
>> Thanks.
>>
>> Eric
>>
>>
>> On Mon, Dec 2, 2013 at 8:01 AM, Matthias BUSSONNIER <
>> bussonniermatthias at gmail.com> wrote:
>>
>>> Le 2 déc. 2013 à 17:37, Eric Matthes a écrit :
>>>
>>>> Hello,
>>>>
>>>> I often use IPython Notebook to write tutorials. In tutorials, I find it
>>> helpful to highlight the lines of code that are being changed in each
>>> iteration of a project. Is there a straightforward way to highlight
>>> individual lines of code within a code cell?
>>>> I came up with one approach, but it feels like an ugly hack. I write a
>>> comment before the line[s] I want to highlight, and a comment after. Then
>>> when converting to html I convert these comment lines to <b> and </b>. This
>>> works, but it clutters the raw notebook file with formatting comments.
>>>> ##bold
>>>> print("I'd like to highlight this line.")
>>>> ##/bold
>>>>
>>>> becomes:
>>>>
>>>> <b>print("I'd like to highlight this line.")</b>
>>>>
>>>> Is there a better way?
>>> One could bind code mirror gutter event (click on line number) to store
>>> metadata into the cells,
>>> and write a custom highlighter for the templates in nbconvert.
>>> This is more or less the only thing I  see. I would say that it's a good
>>> long afternoon hack if you
>>> are familiar with the codebase/codemirror and Pygment.
>>>
>>> It would have the advantage that you can easily toggle this highlight on
>>> and of with a flag.
>>>
>>> --
>>> Matthias
> It would be very nice to have this as a cell magic that would highlight lines not in a reference
> cell (or render as a diff relative to the reference cell).
>
> E.g.,
>
> [23] def Hello():
>          print "Hello, World"
>
> [24] %%diff 23
>        def Hello(s)
>          print "Hello, %s" % s
>
> No idea how to do this, though =)
>
> --Mark
>
Software Carpentry would really like this feature as well, but I'd 
prefer not to tie it to cell numbers, since they change as the notebook 
is re-executed.  Instead, could we use cell tagging to give cells names, 
then display the diff against a named cell?
Thanks,
Greg



More information about the IPython-dev mailing list