Closed blakeex closed 2 years ago
You could try Notebook(lazy=True)... it should stop loading when it hits the desired imported name... Though things like completion, etc might cause a full import.
But typically we gate the "demo" cells with:
if __name__ == "main":
UnwantedSideEffect()
You can hoist that check to a variable as well, so it might only by 4+len(varname) keys to type.
I guess we could consider a magic importnb.Stop exception, notebook metadata, or the like, which would work if you control both documents.
This didn't work for me, the whole notebook got executed :c
Maybe the solution from ipynb
could be somehow adapted?
https://github.com/ipython/ipynb/blob/master/ipynb/fs/defs/__init__.py
The ast
manipulation sounds... interesting. Related, there's the importnb.Paramterize
loader, where one could also hoist the stuff to a constant, and potentially do other things with it.
i don't know if i'm going to implement this feature in importnb
just yet, but definitely going to consider it.
it sounds cool and could alleviate some overhead in some use cases. typing up this example raised some questions about what filters should be applied to the ast nodes and what shouldn't.
for the meantime, the importnb.Notebook.visit
method can be overloaded to support
this feature.
import ast, importnb
you_ll_never_see = None
class ClassOnly(ast.NodeTransformer):
def visit_Module(self, node):
return ast.Module([
x for x in node.body
# what is the right set of nodes to include
if isinstance(x, (ast.Import, ast.ImportFrom, ast.ClassDef))]
, node.type_ignores)
class ClassOnlyImporter(importnb.Notebook):
def visit(self, nodes):
return ClassOnly().visit(nodes)
with ClassOnlyImporter():
import issue_115 as nb
[x for x in dir(nb) if not x.startswith("_")]`
this feature definitely has some thorns, but its added in #127. hopefully is works for yalls
When I run the codes above in another notebook, the entire cells in the
my 1st notebook.ipynb
runs, which is not ideal.How do I load the class definitions & method definitions without running all the cells from another notebook? Is it possible?
I am running both notebooks in Google Colab. Thanks