nbautoexport.utils
¶
JupyterNotebook (BaseModel)
pydantic-model
¶
__hash__(self)
special
¶
Return hash(self).
Source code in nbautoexport/utils.py
def __hash__(self):
return hash(self.json())
cleared_argv()
¶
Context manager that temporarily clears sys.argv. Useful for wrapping nbconvert so unexpected arguments from outer program (e.g., nbautoexport) aren't passed to nbconvert.
Source code in nbautoexport/utils.py
@contextmanager
def cleared_argv():
"""Context manager that temporarily clears sys.argv. Useful for wrapping nbconvert so
unexpected arguments from outer program (e.g., nbautoexport) aren't passed to nbconvert.
"""
prev_argv = [arg for arg in sys.argv]
sys.argv = [sys.argv[0]]
try:
yield
finally:
sys.argv = prev_argv
find_notebooks(directory)
¶
Finds Jupyter notebooks in a directory. Not recursive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory |
Path |
directory to search for notebook files |
required |
Returns:
Type | Description |
---|---|
List[JupyterNotebook] |
notebooks found |
Source code in nbautoexport/utils.py
def find_notebooks(directory: Path) -> List[JupyterNotebook]:
"""Finds Jupyter notebooks in a directory. Not recursive.
Args:
directory (Path): directory to search for notebook files
Returns:
List[JupyterNotebook]: notebooks found
"""
notebooks = []
for subfile in directory.iterdir():
if subfile.is_file() and subfile.name:
try:
notebook = nbformat.read(str(subfile), as_version=nbformat.NO_CONVERT)
nbformat.validate(notebook)
notebooks.append(JupyterNotebook(path=subfile, metadata=notebook.metadata))
except Exception as e:
if subfile.suffix.lower() == ".ipynb":
warn(
f"Error reading {subfile.resolve()} as Jupyter Notebook: "
+ f"[{type(e).__name__}] {e}"
)
return notebooks
working_directory(directory)
¶
Changes working directory and returns to previous on exit.
Source code in nbautoexport/utils.py
@contextmanager
def working_directory(directory: Path):
"""Changes working directory and returns to previous on exit."""
prev_cwd = Path.cwd()
os.chdir(directory)
try:
yield
finally:
os.chdir(prev_cwd)