Experimenting with IPython

Added by J. Simmons about 9 years ago

So, a couple of weeks ago Jeremy posted a link on G+ that talked about optimizing Python code. The article itself was interesting, but what really drew my eye was the way in which the page wove discussion with Python source code and results of running the Python code. It was strikingly similar to what I am trying to achieve with MTK. So, I did some digging, and found out the blog post was written in IPython. IPython is another Python based math analysis package (similar to Sage). I get the feeling it includes fewer libraries than Sage, but unlike Sage IPython runs on Windows (as well as Mac and Linux). And clearly, it is already capable of generating clean and professionally formatted documents mixing math and discussion. So, before we get locked into a particular architecture, I am going to take some time to evaluate IPython in case it would make a better foundation for MTK than Sage. Here are a couple of quick notes about this evaluation before I get started.

  • The effort to deliver an initial cut of MTK using Sage was well spent even if we switch to another math package under the hood
    • We worked fast and efficiently to get a first draft of MTK to evaluate (think agile development)
    • That first cut of MTK generated far better requirements in the form of example documents and helped refine what MTK should be when it is done (more on that in another post)
  • On first blanche, IPython is not exactly what we want for the totality of MTK (though it may be far closer in some areas than Sage)

I will use this thread to capture some notes and running commentary about my evaluation of MTK. I plan to work in both Mac and Windows to get a sense of just how well the Windows port works.


Replies (23)

RE: Experimenting with IPython - Added by J. Simmons about 9 years ago

Some notes on setting up IPython on Mac.

  1. Install pip if you don't already have it (forgot to capture link to this step)
  2. Install IPython with Notebook support (notebooks are the web based UI in IPython that provides an interface for working in text and math)
    pip install "ipython[notebook]"
  3. Install Pandoc (Pandoc provides the PDF export capability for the IPython notebook)
  4. Install Pint in IPython (Pint Installation using pip and Using pip in IPython) - be sure to do this inside an IPython session in the terminal
    !pip install pint

Other general notes:

RE: Experimenting with IPython - Added by J. Simmons about 9 years ago

Getting IPython set up on Win7.

  1. Install pip if you need it.
    So, I had Python 3 and pip installed on my Win7 computer at one point, but some later install broke the path. So, the first thing I had to do was restore the path for Python 3.4.1 by placing "C:\Python34;C:\Python34\scripts" in my system path (note, I had to put it in before other Python installs).
  2. Install IPython with Notebook support
    pip3 install "ipython[notebook]"
  3. Install Pandoc
    Downloaded and ran pandoc-1.13.2-windows.msi. Note, I already had MiKTeX installed due to previous LaTeX installation
  4. Install Pint
    ipython
    In [1]: !pip3 install pint

Smoke Testing

  1. Testing Pint, run the following commands before exiting ipython from Pint installation
    In [2]: from pint import UnitRegistry
    In [3]: ureg = UnitRegistry()
    In [4]: distance = 24.0 * ureg.meter
    In [5]: print(distance)
    24.0 meter
  2. Testing PDF export from notebook
    1. Save sample notebook to a scratch directory
      c:\test\NUFFT.ipynb
    2. Open a command prompt and change to the scratch directory
      cd \test\
    3. Launch IPython Notebook server
      ipython notebook
    4. Select the NUFFT.ipynb in the web browser when it opens, dismissing the dialog about the file being converted to the current format
    5. Select File>Download As>PDF
      I got an error at this step: "nbconvert failed: you need to have pywin32 installed for this to work"

Correcting Installation

  1. Clean up steps
    1. Close export notebook tab
    2. Save notebook
    3. Exit IPython tabs
    4. Exit IPython Notebook server
  2. Install pywin32
    1. Download pywin32 for correct version of Python (3.4.x) and Windows (x32 to match Python version)
    2. Run pywin32 installer
  3. Repeat Notebook export test (after closing the command prompt and opening a new one), received an error that Pandoc was not installed
    1. Repeated clean up steps
    2. Unistalled Pandoc
    3. Installed Pandoc
    4. Repeated notebook test again, same error
    5. Fix for cannot find Pandoc error on Windows
    6. Repeated notebook test again, took a really long time to run (as of the last save the export was still running, looked like it might be stalled)
    7. Rebooted, reran test again, this time it worked (after miktek asked to install several packages... will need to capture what packages IPython is using if we proceed to use it)

RE: Experimenting with IPython - Added by J. Simmons about 9 years ago

Started working on implementing the test cases tonight. I started with the first test case. I ran into a missing feature right off the bat: including values computed in code inside of text commentary (see the end of Section 1 of the test file). The good news is it looks like there is a package that already adds in this functionality, IPython Notebook Extensions.

Attempting to Install IPython Notebook Extensions

Referencing installation instructions for IPython 3.

  1. start IPython CLI
    ipython
  2. In IPython, run Step 1 instructions
    In [1]: import IPython.html.nbextensions as nb
    In [2]: ext= 'https://github.com/ipython-contrib/IPython-notebook-extensions/archive/master.zip'
    In [3]: nb.install_nbextension(ext)
  3. Exit IPython

Testing Notebook Extensions

Referencing Loading an Extension

  1. Launch IPython notebook
    ipython notebook
  2. Add a code block to activate the markdown extension
    %%javascript
    IPython.load_extensions('IPython-notebook-extensions-master/usability/python-markdown');
  3. Include output from Python in a markdown cell {{x1}} + {{x2}} = {{y}}

Initial results did not render the values. Upon inspection, I cannot find the installed files. So, need to investigate the installation process. Also, note that the PDF export needs to be updated to support this feature (see the last paragraph of installation process).

Found the installation files under C:\Python34\share\jupyter\nbextensions\. Looking at this page for help trying to get the extension to load. Getting an error in the JS console that looks like this:

Failed to load extension(s): ["nbextensions/IPython-notebook-extensions-master/usability/python-markdown"]

I tried copying the install files to my profile directory, but no luck there. Will have to try more later...

RE: Experimenting with IPython - Added by J. Simmons about 9 years ago

And, figured out the Notebook Extensions issue. There were 4 problems I had to resolve.

  1. The installation instructions put the extension files in the wrong place. I had to copy the files manually to my IPython profile. Given my other issues, I elected to get the files from the zip download of the IPython 3 branch.
    C:\Users\<username>\.ipython\nbextensions\IPython-notebook-extensions-master\
  2. The include path was wrong for the python-markdown extension
    %%javascript
    IPython.load_extensions('IPython-notebook-extensions-master/usability/python-markdown/main');
  3. I had to trust the notebook (only trusted notebooks would render the desired output)
    File>Trust in the IPython UI
  4. I had to remember to run the markdown cell, not the code cell above it. Contrary to a comment I saw somewhere, the values do refresh even when the markdown is not dirty.

RE: Experimenting with IPython - Added by Christopher Sigman almost 9 years ago

We had discussed an OS to use as our basis for working on iPython, but I don't think that was ever quite resolved. I propose we go with Ubuntu 14.04. I also propose that we stick with the latest version of Python on there, which is 3.4. What is everyone's thoughts on that?

RE: Experimenting with IPython - Added by Jeremy Wright almost 9 years ago

Ubuntu 14.04 is what I would suggest as well. The Python version is most often dictated by what version is supported by the libraries we need to use. I would check into Pint, Minted, IPython, etc to see what they need.

RE: Experimenting with IPython - Added by J. Simmons almost 9 years ago

Jeremy, I was browsing through my many and varied notes above and IPython itself definitely runs in Python 3. It looks like Pint works from my early tests. And I don't think we need to worry about Minted (that works with Python 3 and is mostly a hack for rendering code nicely in plain LaTeX). I think the bigger question is do all of the core math/science libraries run in Python 3 now. I get the feeling they do, but I would feel better seeing it in writing.

RE: Experimenting with IPython - Added by Jeremy Wright almost 9 years ago

CadQuery is still Python 2.7.x, but we can decouple that somewhat by writing the output libraries in a 2.x/3.x compatible way.

RE: Experimenting with IPython - Added by Jeremy Wright almost 9 years ago

One more thing, here is an interesting post about setting up IPython.

Do we want all of those scientific packages that are being installed in the post? I'm not sure if IPython requires them all, but even if it doesn't, it may not hurt for MTK to have all of those accessible out of the box.

RE: Experimenting with IPython - Added by Christopher Sigman almost 9 years ago

Looking at the list of iPython extensions, some that we'll probably want to use:

RE: Experimenting with IPython - Added by Jeremy Wright almost 9 years ago

The instructions to get IPython working on Ubuntu 14.04 are missing some things. First, you have to install the jsonschema package as well. However, jsonschema seems to be invisible to IPython even after it's installed, so I'll need to figure that out.

Also, there is no ~/.ipython/profile\_[name]/ipython\_notebook\_config.py file created by default with IPython anymore. There's a long conversation about it here that ended with the comment that it will be fixed sometime in the future as part of another upgrade. I'm still investigating whether the ipython_config.py file will do all that we need.

Chris did something similar to pip3 install "ipython[all]" which worked for him, and I'm wondering if that would be a better starting point for the script than what's in the blog post above.

RE: Experimenting with IPython - Added by J. Simmons almost 9 years ago

That is very interesting news, Jeremy and Chris. I was talking to Chris on the drive down about this, but I wonder if we should also investigate Anaconda.

RE: Experimenting with IPython - Added by Jeremy Wright almost 9 years ago

Jason D. has much more experience with Anaconda than I do. He might be someone to ask about it.

RE: Experimenting with IPython - Added by Christopher Sigman almost 9 years ago

So, here's the 3 cells of my notebook right now:

code:
"%%javascript\n",
"IPython.load_extensions('IPython-notebook-extensions-3.x/usability/python-markdown/main')"

code:
a = 2
b = 5
c = a * b

markdown: {{a}} * {{b}} = {{c}}

and it shows 2 * 5 = 10 as expected.

RE: Experimenting with IPython - Added by Jeremy Wright over 8 years ago

I just loaded IPython via Anaconda, and was able to get the python-markdown extension to load properly.

%%javascript
IPython.load_extensions('IPython-notebook-extensions-3.x/usability/python-markdown/main');

Gave me console output of...
"Loaded extension: IPython-notebook-extensions-3.x/usability/python-markdown/main" 

So now here's where I'm at.
%%javascript
IPython.load_extensions('IPython-notebook-extensions-3.x/usability/python-markdown/main');
a = 2
b = 5
c = a * b
{{a}} * {{b}} = {{c}}
File "<ipython-input-12-d8535d30375b>", line 1
    {{a}} * {{b}} = {{c}}
                         ^
SyntaxError: can't assign to operator

Which would suggest to me that the markdown still isn't working. Thoughts?

RE: Experimenting with IPython - Added by Jeremy Wright over 8 years ago

Just remembered you guys saying that a cell has to be set as markdown. It works now.

RE: Experimenting with IPython - Added by J. Simmons over 8 years ago

Awesome! I can't wait to restore my VM to a previous snapshot and give it a try. This plus Pint should get test case 3 basically working.

RE: Experimenting with IPython - Added by Jeremy Wright over 8 years ago

The install script includes Pint.

RE: Experimenting with IPython - Added by J. Simmons over 8 years ago

I have a good news/bad news sort of situation.

  • Good News 1: I don't think we will need to mess with the formatting. The first attachment shows that the document title and the section headers get report style formatting when exported to PDF.
  • Good News 2: I got a pretty close copy of the units example working locally (complete with short units formatting).
  • Bad News 1: Exporting a PDF of a NB with Python Markdown extension turned on does not render the embedded python values in the exported PDF (see second attachment).
  • Bad News 2: Once I got the Python Markdown loading into my NB, I had to manually trust it (I thought this was no longer an issue, but it looks like it is).

RE: Experimenting with IPython - Added by Jeremy Wright over 8 years ago

With newer version of IPython the older form of IPython.load_extensions may not work. Instead, try the following.

require(['base/js/utils'],
function(utils) {
    utils.load_extensions('IPython-notebook-extensions-3.x/usability/python-markdown/main');
});

RE: Experimenting with IPython - Added by Jeremy Wright over 8 years ago

Here's an updated notebook with the new nbexternsion load method from above done correctly for the installer and Jupyter 4.0.4.

(1-23/23)