Hi Matthias ipynb is what I was looking for. I noticed two small problems. (I am not sure where/how to report them)
1) You can not import functions that have doc strings in them. Here is the error msg File "<unknown>", line 13 """uses Gauss's method for summing integers http://wmueller.com/precalculus/advanced/hint4_3_6.html""" ^IndentationError: unexpected indent 2) I needed to use import 'as'. I think the https://github.com/ipython/ipynb readme.md needs to be update. Here is an example import ipynb.fs.defs.myMathFunctions as mmf mmf.quickSum(5) many thanks On Thursday, April 13, 2017 at 7:17:45 PM UTC-7, Matthias Bussonnier wrote: > > Hi Andy, > > I would suggest to look at https://github.com/ipython/ipynb , which is > supposed to do what you want by walking the AST. > We need to polish it and advertise it more, feedback (and > contributions) would be welcome. > -- > Matthias > > On Thu, Apr 13, 2017 at 5:28 PM, Andy Davidson > <[email protected] <javascript:>> wrote: > > Hi > > > > I have a couple of python functions defined in one notebook that I would > > like to reuse in another. I found the following direction. > > > > > http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html > > > > > > https://github.com/jupyter/notebook/blob/master/docs/source/examples/Notebook/Importing%20Notebooks.ipynb > > > > > My challenge is I have a lot of cells that define straight python code. > > These cells often load large data files and are very slow. Ideally I > would > > like to to only import functions. It looks like the heavy lifting is > done by > > NotebookLoader() bellow. Maybe there is a clever way to only execute > > function definitions? Is there a way to get the abstract syntax tree for > the > > code in a cell and pick out the function definitions? > > > > As a newbie my hack would be to require functions be defined in their > own > > cell. Next before execute check the cell code for lines beginning with > ‘def' > > > > class NotebookLoader(object): > > """Module Loader for Jupyter Notebooks""" > > def __init__(self, path=None): > > self.shell = InteractiveShell.instance() > > self.path = path > > > > def load_module(self, fullname): > > """import a notebook as a module""" > > path = find_notebook(fullname, self.path) > > > > print ("importing Jupyter notebook from %s" % path) > > > > # load the notebook object > > with io.open(path, 'r', encoding='utf-8') as f: > > nb = read(f, 4) > > > > > > # create the module and add it to sys.modules > > # if name in sys.modules: > > # return sys.modules[name] > > mod = types.ModuleType(fullname) > > mod.__file__ = path > > mod.__loader__ = self > > mod.__dict__['get_ipython'] = get_ipython > > sys.modules[fullname] = mod > > > > # extra work to ensure that magics that would affect the user_ns > > # actually affect the notebook module's ns > > save_user_ns = self.shell.user_ns > > self.shell.user_ns = mod.__dict__ > > > > try: > > for cell in nb.cells: > > if cell.cell_type == 'code': > > # transform the input to executable Python > > code = > > self.shell.input_transformer_manager.transform_cell(cell.source) > > # run the code in themodule > > exec(code, mod.__dict__) > > finally: > > self.shell.user_ns = save_user_ns > > return mod > > > > > > > > P.s. I found Importing%20Notebooks.html hard to use. It might be easier > of > > the code as split out and this notebook and put in the standard > distribution > > of jupyter. “how to import notebooks.ipynb” then becomes 2 lines > > > > Import importNotebookHelper > > Import myNotebook > > > > Kind regards > > > > Andy > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "Project Jupyter" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to [email protected] <javascript:>. > > To post to this group, send email to [email protected] > <javascript:>. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/jupyter/D51565AA.571A2%25Andy%40SantaCruzIntegration.com. > > > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Project Jupyter" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/5e2fc660-31b1-47bf-ada5-49e20b12c896%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
