python access functions in ipython notebook -
this question has answer here:
i have function stored in .py file, call my_methods.
def print_text(mytext): print mytext
i'm using ipython notebook development (local server), , my_methods file changing.
i'd use runipy run other ipython notebooks via shell script reference functions within my_methods. example, 1 ipython notebook being launched shell script this:
import my_methods mm mm.print_text("print me")
how set import my_methods line can print_text function ipython notebook (.ipynb), instead of .py version? currently, i'd have download my_methods notebook .py file, causing version control issues (the .ipynb version of my_methods different downloaded .py version)
thanks help!
edit
so after reading through blog post shown answer made slight modification find_notebook function. if path ipython notebook isn't supplied,i had read through sys.path, rather current directory. i'm not running on linux machine were, made easier me maintain of added python files in separate directory, included in path variable. saved following code in module called ipyloader, , import first in other modules:
import io, os, sys, types ipython import get_ipython ipython.nbformat import current ipython.core.interactiveshell import interactiveshell def find_notebook(fullname, path=none): """find notebook, given qualified name , optional path turns "foo.bar" "foo/bar.ipynb" , tries turning "foo_bar" "foo bar" if foo_bar not exist. """ name = fullname.rsplit('.', 1)[-1] if not path: path = sys.path #edited here rather current directory d in path: nb_path = os.path.join(d, name + ".ipynb") if os.path.isfile(nb_path): return nb_path # let import notebook_name find "notebook name.ipynb" nb_path = nb_path.replace("_", " ") if os.path.isfile(nb_path): return nb_path class notebookloader(object): """module loader ipython notebooks""" def __init__(self, path=none): self.shell = interactiveshell.instance() self.path = path def load_module(self, fullname): """import notebook module""" path = find_notebook(fullname) print ("importing ipython notebook %s" % path) # load notebook object io.open(path, 'r', encoding='utf-8') f: nb = current.read(f, 'json') # create module , add 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 # work ensure magics affect user_ns # affect notebook module's ns save_user_ns = self.shell.user_ns self.shell.user_ns = mod.__dict__ try: cell in nb.worksheets[0].cells: if cell.cell_type == 'code' , cell.language == 'python': # transform input executable python code = self.shell.input_transformer_manager.transform_cell(cell.input) # run code in themodule exec(code, mod.__dict__) finally: self.shell.user_ns = save_user_ns return mod class notebookfinder(object): """module finder locates ipython notebooks""" def __init__(self): self.loaders = {} def find_module(self, fullname, path=none): nb_path = find_notebook(fullname, path) if not nb_path: return key = path if path: # lists aren't hashable key = os.path.sep.join(path) if key not in self.loaders: self.loaders[key] = notebookloader(path) return self.loaders[key] sys.meta_path.append(notebookfinder())
then sample file might run this
import ipyloader import testnotebook printer #this .ipynb file printer.myprinter("test")
and output be:
importing ipython notebook c:\python27\lib\testnotebook.ipynb test
a approximation exposed here.
easy way not exist because ipython notebook not regular plain text.
Comments
Post a Comment