[docs]defexec_notebook(db:deephaven_enterprise.database.Database,path:str,globals:Dict[str,Any]=None)->None:""" Attempt to exec a Notebook :param db: Core+ Python Database object :param path: full path to Notebook, beginning with "/" :param globals: the globals to use for execution of the notebook, if you want to have the variables from your script session available use "globals()" """nr=jpy.get_type("io.deephaven.enterprise.dnd.notebook.NotebookReader")src=nr.getNotebookText(db.j_db,path)exec(compile(src,path,'exec'),globals)
[docs]defmeta_import(db:deephaven_enterprise.database.Database,root:str="notebook")->None:""" Set up a meta importer to enable using a Python import statement that references Deephaven Web console notebooks. For example, after calling 'meta_import(db, "notebook")'; you can then call 'import notebook.file' to make the "/file.py" in your Web File Explorer available as a Python module. :param db: Core+ Python Database object :param root: the module name that prefixes all web notebooks, defaults to "notebook" """sys.meta_path.append(__NotebookImporter(db,root))
class_ImportedNotebook:def__init__(self,path,source=None):self.path=pathself.source=sourcedefcreate_module(self,spec):returnNonedefexec_module(self,module):ifself.sourceisnotNone:exec(compile(self.source,'<string>','exec'),module.__dict__)class__NotebookImporter:db:deephaven_enterprise.database.Databaseroot:strdef__init__(self,db,root:str):self.db=dbifrootisNoneorroot=="":raiseException("root for notebook imports must not be empty!")if"."inroot:raiseException("root for notebook imports must not contain a period!")self.root=rootself.notebook_reader=jpy.get_type("io.deephaven.enterprise.dnd.notebook.NotebookReader")deffind_spec(self,name,path,target=None):ifname!=self.rootandnotname.startswith(self.root+"."):# this is not a notebook import; return None so that other finder/loaders can tryreturnNonesrc=Noneifname.startswith(self.root+"."):# we want to keep one dot presentnotebook_path=name[len(self.root):]data=self.notebook_reader.getFileOrFolder(self.db.j_db,notebook_path,False,".py")ifdata.isFile():src=data.getContents()elifdata.isFolder():try:init_data=self.notebook_reader.getFileOrFolder(self.db.j_db,notebook_path.replace(".","/")+"/__init__.py",True,"")src=init_data.getContents()except:passmodule_spec=ModuleSpec(name,_ImportedNotebook(path,src),is_package=True)module_spec.submodule_search_locations=name.split(".")returnmodule_spec