Saturday, 24 November 2012

Hide ipython notebook input-boxes

Last week I analyzed data for a customer, I used pandas to extract the information from a large collection of XMLs, I converted the result to html using nbconvert. I sent the html file to the customer without explaining what ipython notebook is.

The answer from my customer was: "That's a great analysis-document, nice tables, but the stuff between the tables is distracting."

On the Toggle Input Test page, you see how I removed that distracting stuff ;-). Click Toggle Input. (Will only work on blogger.com)

html_settings() extended_styles() creates the Toggle Input link and is part of my ipython notebook database display tools. It loads jQuery from CDN if not available, so in the converted html Toggle Input will only work when you're online.




21 comments:

  1. Hi Jean-Louis,

    Thanks for posting that `toggle input' code; it did something I've been looking for, for some time. Unfortunately for me, I saved my document after toggling and lost all the original code and python output. Does this behaviour occur for you or have I broken something?

    Cheers, Mark.

    ReplyDelete
    Replies
    1. Hi Mark,

      'Toggle input' only hides input fields, it doesn't remove anything. This shouldn't happen.

      1. What version of ipython do you use?
      2. If your notebook doesn't contain confidential data, you could send it and I will check it. It would be great to get a version before and after.

      Best,
      Jean-Louis

      Delete
    2. This comment has been removed by the author.

      Delete
  2. Hi Jean-Louis, thanks for responding. The two deletions were due to code formatting errors on my part. I still can't get the code formatting and indentation to work!

    In the notebook I see:

    In [1]: print __version__
    1.6.2

    In an IPython window at the terminal I see,

    Python 2.6.5 (r265:79063, Oct 1 2012, 22:04:36)
    Type "copyright", "credits" or "license" for more information.

    IPython 0.13.1 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.

    In [1]:

    I believe 0.13.1 is the current stable version, though I'm not sure of what the notebook is reporting!

    I get this behaviour with,

    In [1]: from pretty import *
    html_settings()
    Out[1]: Toggle Input
    In [2]: print __version__
    1.6.2


    Best regards,

    Mark.

    ReplyDelete
    Replies
    1. Hi Mark

      1. Have you remembered saving? Sorry, for that, but I often forget it.
      2. What browser are you using?

      I have to say I never tested it with python 2.6, only python 2.7 and 3.x.

      Best,
      Jean-Louis

      Delete
    2. Hi Jean-Louis,

      Don't be sorry! Yes, it's the point of saving that overwrites the ok file. If save with all code displayed, exit, and reload: all is well. If I save with code hidden, exit, and reload, all the code is gone, including the toggle command and all generated output apart from markdown text.

      I'm using Chromium 18.0.1025.168 (dev build) for Ubuntu 10.04.
      I'll test with firefox and report back. Thanks again for your time.

      BR,

      Mark.

      Delete
    3. Hi again, Jean-Louis,

      I can confirm that firefox shows the same behaviour.

      Best regards,

      Mark.

      Delete
    4. Can you open the "emtpy" notebook in text editor and see if the input/output actually isn't there or it is just not displayed.

      Delete
    5. I added a small improvement: https://gist.github.com/3772679

      I tested this with notebook 0.13.1 and python 2.6, 2.7.

      Delete
    6. Maybe you can upload example to dropbox or something.

      Delete
    7. Hi Jean-Louis. I'll get to your suggestions in an hour: I'm at work and being pulled away from my desk. Best regards, Mark.

      Delete
    8. I inspected the .ipynb file for the script,

      from pretty import *
      html_settings()
      print 'Hello'

      after saving and before hitting, `toggle input'. It was 121 lines long.
      After hitting `toggle input' so the inputs were hidden, and saving again,
      the .ipynb file became,

      {
      "metadata": {
      "name": "Untitled5"
      },
      "nbformat": 3,
      "nbformat_minor": 0,
      "worksheets": [
      {
      "cells": [],
      "metadata": {}
      }
      ]
      }

      so the information *is* being lost. Have you an email address I can send them to? They're very small files.

      BR, Mark.

      Delete
    9. More information: I downloaded your update above. imported, reloaded to make sure. The same thing occurs: if I view the .ipynb file with the code non-collapsed and saved, the file is large. If I collapse the code and save, the saved file becomes very small. I can re-expand the code and save again to restore the file but if I exit after saving the notebook in its collapsed form, the saved file is close to empty.

      BR, Mark.

      Delete
    10. Hello Mark

      Thanks a lot. Now I know what the problem is. I'll try to fix that tonight.

      In the meantime you can just save when the input is not collapsed. If you convert that to html it will collapse on load.

      To you need to save the collapse state? I haven't implemented that.

      Best,
      Jean-Louis

      Delete
    11. Hi Jean-Louis,

      Great! Thanks for your time and thanks for the code in the first place. It's a real help to me. I don't need to save the collapsed state; I was just printing with it.

      Best regards,

      Mark.

      Delete
    12. I cleaned quite a bit code up. Saving should work now. Please let me know if it now works for you.

      Be aware that I renamed the file and the function is now called extended_styles() instead of html_settings()

      Delete
    13. Brilliant! It works. Thank you. You've helped speed up my work.
      Best regards, Mark.

      Delete
  3. That is pretty cool. I am also often distracted by my own code when cross reading old notebooks. (At least when they are about plotting/data extraction)

    Thanks for this usefull little tool !

    ReplyDelete
  4. sorry but it seems that the github link is broken, could you please repost it or post a link to another repository where I could find this wonderful code?
    thanks

    ReplyDelete
    Replies
    1. I updated the post:

      https://github.com/adfinis-sygroup/adsy-python/blob/master/lib/adsy/display.py

      Delete
  5. Congratulations, you hit the nail on the head with this tool.
    I already included it in my notebooks.
    A must.

    ReplyDelete