TUI
Terminal user interface (TUI) helper functions and components.
ImgFmt (Enum)
An enumeration.
diff2rich(diff, *, console=<console width=80 None>)
Show rich representation of notebook diff in terminal.
Source code in databooks/tui.py
def diff2rich(
diff: DiffContents,
*,
console: Console = Console(),
) -> None:
"""Show rich representation of notebook diff in terminal."""
a_nb, b_nb = (
JupyterNotebook.parse_raw(c)
if c is not None
else JupyterNotebook(
nbformat=0, nbformat_minor=0, metadata=NotebookMetadata(), cells=[]
)
for c in (diff.a.contents, diff.b.contents)
)
cols = Columns(
[
Rule(
f"{ab}/{c['path'].resolve().name if c['path'] is not None else 'null'}"
)
for ab, c in asdict(diff).items()
if ab in ("a", "b")
],
width=console.width // 2,
padding=(0, 0),
)
console.print(cols, a_nb - b_nb)
diffs2rich(diffs, *, context=False, export_kwargs=None, **console_kwargs)
Show rich representation of notebook diff in terminal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
diffs |
List[databooks.git_utils.DiffContents] |
|
required |
context |
Union[databooks.tui.ImgFmt, bool] |
specify context - |
False |
export_kwargs |
Optional[Dict[str, Any]] |
keyword arguments for exporting prints (as a dictionary) |
None |
console_kwargs |
Any |
keyword arguments to be passed to |
{} |
Returns:
Type | Description |
---|---|
Optional[str] |
console output if |
Source code in databooks/tui.py
def diffs2rich(
diffs: List[DiffContents],
*,
context: Union[ImgFmt, bool] = False,
export_kwargs: Optional[Dict[str, Any]] = None,
**console_kwargs: Any,
) -> Optional[str]:
"""
Show rich representation of notebook diff in terminal.
:param diffs: `databooks.git_utils.DiffContents` for rendering
:param context: specify context - `ImgFmt` to export outputs, `True` for `pager`
:param export_kwargs: keyword arguments for exporting prints (as a dictionary)
:param console_kwargs: keyword arguments to be passed to `Console`
:return: console output if `context` is `ImgFmt`, else `None`
"""
theme = console_kwargs.pop("theme", DATABOOKS_TUI)
console = Console(record=isinstance(context, ImgFmt), theme=theme, **console_kwargs)
ctx_map: Dict[Union[ImgFmt, bool], AbstractContextManager] = {
True: console.pager(styles=True),
False: nullcontext(),
}
with ctx_map.get(context, console.capture()):
for diff in diffs:
diff2rich(diff, console=console)
if isinstance(context, ImgFmt):
return getattr(console, f"export_{context.name}")(**(export_kwargs or {}))
nb2rich(path, console=<console width=80 None>)
Show rich representation of notebook in terminal.
Source code in databooks/tui.py
def nb2rich(
path: Path,
console: Console = Console(),
) -> None:
"""Show rich representation of notebook in terminal."""
notebook = JupyterNotebook.parse_file(path)
console.print(Rule(path.resolve().name), notebook)
nbs2rich(paths, *, context=False, export_kwargs=None, **console_kwargs)
Show rich representation of notebooks in terminal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths |
List[pathlib.Path] |
notebook paths to print |
required |
context |
Union[databooks.tui.ImgFmt, bool] |
specify context - |
False |
export_kwargs |
Optional[Dict[str, Any]] |
keyword arguments for exporting prints (as a dictionary) |
None |
console_kwargs |
Any |
keyword arguments to be passed to |
{} |
Returns:
Type | Description |
---|---|
Optional[str] |
console output if |
Source code in databooks/tui.py
def nbs2rich(
paths: List[Path],
*,
context: Union[ImgFmt, bool] = False,
export_kwargs: Optional[Dict[str, Any]] = None,
**console_kwargs: Any,
) -> Optional[str]:
"""
Show rich representation of notebooks in terminal.
:param paths: notebook paths to print
:param context: specify context - `ImgFmt` to export outputs, `True` for `pager`
:param export_kwargs: keyword arguments for exporting prints (as a dictionary)
:param console_kwargs: keyword arguments to be passed to `Console`
:return: console output if `context` is `ImgFmt`, else `None`
"""
if "record" in console_kwargs:
raise ValueError(
"Specify `record` parameter of console via `context` argument."
)
theme = console_kwargs.pop("theme", DATABOOKS_TUI)
console = Console(record=isinstance(context, ImgFmt), theme=theme, **console_kwargs)
ctx_map: Dict[Union[ImgFmt, bool], AbstractContextManager] = {
True: console.pager(styles=True),
False: nullcontext(),
}
with ctx_map.get(context, console.capture()):
for path in paths:
nb2rich(path, console=console)
if isinstance(context, ImgFmt):
return getattr(console, f"export_{context.name}")(**(export_kwargs or {}))