grst / nbimporter

Import ipython notebooks as modules
BSD 3-Clause "New" or "Revised" License
59 stars 9 forks source link

Celldeleter #2

Closed mdbenito closed 7 years ago

mdbenito commented 7 years ago

This PR uses python's AST module to discard any code which is not a definition of a function or a class when importing a notebook. This is particularly useful when importing from notebooks containing long computations, tests, etc. The behaviour can be disabled by setting a variable.

However, it is sometimes useful to run some code in the notebook for initialisation, hence a facility for this purpose is provided: if a function __init__() is found in the module, it is executed.

Finally, I added some documentation and made a couple of cosmetic changes here and there.

Best

PS: Maybe I should clarify that "celldeleter" is a misnomer, since it is not full cells which can be ignored, but all code not defining functions or classes.

grst commented 7 years ago

Hi Miguel,

thanks for your contribution.

Using the minimal working example from the README, I get the following error with your version:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-ede54ba8b013> in <module>()
----> 1 from libnb import *

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load(name, import_)

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _load_unlocked(spec)

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _load_backward_compatible(spec)

/pstore/home/sturmg/repos/nbimporter/nbimporter.py in load_module(self, fullname)
    113 
    114         # Run any initialisation if available, but only once
--> 115         if options['run_init'] and not mod.__dict__.has_key('__init_done__'):
    116             try:
    117                 mod.__init__()

AttributeError: 'dict' object has no attribute 'has_key'

If I simply re-run the import it works. Can you please fix that?

mdbenito commented 7 years ago

Oops, sorry! Old python2 code... Fixed now.

grst commented 7 years ago

There still seems to be another (python3?) problem:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-ede54ba8b013> in <module>()
----> 1 from libnb import *

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load(name, import_)

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _load_unlocked(spec)

/pstore/apps/Anaconda3/2.5.0/lib/python3.5/importlib/_bootstrap.py in _load_backward_compatible(spec)

/pstore/home/sturmg/repos/nbimporter/nbimporter.py in load_module(self, fullname)
    115         if options['run_init'] and '__init_done__' not in mod.__dict__:
    116             try:
--> 117                 mod.__init__()
    118                 mod.__init_done__ = True
    119             except (KeyError, AttributeError) as _:

TypeError: Required argument 'name' (pos 1) not found
mdbenito commented 7 years ago

Sorry again. It was silly to use __init__ for custom notebook initialisation. I switched that to __nbinit__ which btw sounds better. I've tested now with py3.5 so there shouldn't be any more issues.

grst commented 7 years ago

Thanks, now it works! I will make sure to upload the revised version on PyPI the next days.