Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions aidialog/ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,20 @@ def from_cells(self:Dialog, cells):
return self

# %% ../nbs/02_ipynb.ipynb #6bdaa293
def reads_ipynb(txt, cls=Dialog, name='dialog'):
def reads_ipynb(txt, cls=Dialog, name='dialog', verbose=False):
"Read a dialog from notebook JSON string `txt`, constructing via `cls`"
nb = json.loads(txt)
if repairs := repair_nb(nb): print('NB repair:', '; '.join(repairs))
if (repairs := repair_nb(nb)) and verbose: print('NB repair:', '; '.join(repairs))
nb = dict2nb(nb)
return cls(name=name, meta=dict(nb.get('metadata', {}))).from_cells(nb.cells)

# %% ../nbs/02_ipynb.ipynb #7e8912e8
def read_ipynb(fname, cls=Dialog, name=None):
def read_ipynb(fname, cls=Dialog, name=None, verbose=False):
"Read a dialog from notebook file `fname` (`.ipynb` added if missing), constructing via `cls`; `name` defaults to the file stem"
f = Path(fname).expanduser()
if f.suffix != '.ipynb': f = f.with_suffix('.ipynb')
if not f.exists(): return print(f,'does not exist')
try: res = reads_ipynb(f.read_text(encoding='utf-8'), cls, name or f.stem)
try: res = reads_ipynb(f.read_text(encoding='utf-8'), cls, name or f.stem, verbose=verbose)
except (json.JSONDecodeError, PermissionError): return
res.path_ = f
res.mtime_ = safe_mtime(f)
Expand Down
8 changes: 4 additions & 4 deletions nbs/02_ipynb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,10 @@
"outputs": [],
"source": [
"#| export\n",
"def reads_ipynb(txt, cls=Dialog, name='dialog'):\n",
"def reads_ipynb(txt, cls=Dialog, name='dialog', verbose=False):\n",
" \"Read a dialog from notebook JSON string `txt`, constructing via `cls`\"\n",
" nb = json.loads(txt)\n",
" if repairs := repair_nb(nb): print('NB repair:', '; '.join(repairs))\n",
" if (repairs := repair_nb(nb)) and verbose: print('NB repair:', '; '.join(repairs))\n",
" nb = dict2nb(nb)\n",
" return cls(name=name, meta=dict(nb.get('metadata', {}))).from_cells(nb.cells)"
]
Expand All @@ -594,12 +594,12 @@
"outputs": [],
"source": [
"#| export\n",
"def read_ipynb(fname, cls=Dialog, name=None):\n",
"def read_ipynb(fname, cls=Dialog, name=None, verbose=False):\n",
" \"Read a dialog from notebook file `fname` (`.ipynb` added if missing), constructing via `cls`; `name` defaults to the file stem\"\n",
" f = Path(fname).expanduser()\n",
" if f.suffix != '.ipynb': f = f.with_suffix('.ipynb')\n",
" if not f.exists(): return print(f,'does not exist')\n",
" try: res = reads_ipynb(f.read_text(encoding='utf-8'), cls, name or f.stem)\n",
" try: res = reads_ipynb(f.read_text(encoding='utf-8'), cls, name or f.stem, verbose=verbose)\n",
" except (json.JSONDecodeError, PermissionError): return\n",
" res.path_ = f\n",
" res.mtime_ = safe_mtime(f)\n",
Expand Down