Skip to content

Commit 8efd81f

Browse files
committed
IDs
1 parent 4891466 commit 8efd81f

3 files changed

Lines changed: 46 additions & 34 deletions

File tree

execnb/nbio.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/01_nbio.ipynb.
44

5-
# %% auto 0
5+
# %% auto #0
66
__all__ = ['NbCell', 'dict2nb', 'read_nb', 'mk_cell', 'new_nb', 'nb2dict', 'nb2str', 'write_nb']
77

8-
# %% ../nbs/01_nbio.ipynb
8+
# %% ../nbs/01_nbio.ipynb #5faca748
99
from fastcore.basics import *
1010
from fastcore.xtras import rtoken_hex
1111
from fastcore.imports import *
@@ -14,11 +14,11 @@
1414
from pprint import pformat,pprint
1515
from json import loads,dumps
1616

17-
# %% ../nbs/01_nbio.ipynb
17+
# %% ../nbs/01_nbio.ipynb #9d2e7add
1818
def _read_json(self, encoding=None, errors=None):
1919
return loads(Path(self).read_text(encoding=encoding, errors=errors))
2020

21-
# %% ../nbs/01_nbio.ipynb
21+
# %% ../nbs/01_nbio.ipynb #49d7bf89
2222
class NbCell(AttrDict):
2323
def __init__(self, idx, cell):
2424
super().__init__(cell)
@@ -42,7 +42,7 @@ def parsed_(self):
4242
def __hash__(self): return hash(self.source) + hash(self.cell_type)
4343
def __eq__(self,o): return self.source==o.source and self.cell_type==o.cell_type
4444

45-
# %% ../nbs/01_nbio.ipynb
45+
# %% ../nbs/01_nbio.ipynb #6c742618
4646
def _dict2obj(d, list_func=list, dict_func=AttrDict):
4747
"Convert (possibly nested) dicts (or lists of dicts) to `AttrDict`"
4848
if isinstance(d, list): return list(map(_dict2obj, d))
@@ -55,14 +55,14 @@ def dict2nb(js=None, **kwargs):
5555
nb.cells = [NbCell(*o) for o in enumerate(nb.cells)]
5656
return nb
5757

58-
# %% ../nbs/01_nbio.ipynb
58+
# %% ../nbs/01_nbio.ipynb #0626f06e
5959
def read_nb(path):
6060
"Return notebook at `path`"
6161
res = dict2nb(_read_json(path, encoding='utf-8'))
6262
res['path_'] = str(path)
6363
return res
6464

65-
# %% ../nbs/01_nbio.ipynb
65+
# %% ../nbs/01_nbio.ipynb #c86fe311
6666
def mk_cell(text, # `source` attr in cell
6767
cell_type='code', # `cell_type` attr in cell
6868
**kwargs): # any other attrs to add to cell
@@ -74,27 +74,27 @@ def mk_cell(text, # `source` attr in cell
7474
kwargs['execution_count']=0
7575
return NbCell(0, dict(cell_type=cell_type, source=text, directives_={}, **kwargs))
7676

77-
# %% ../nbs/01_nbio.ipynb
77+
# %% ../nbs/01_nbio.ipynb #e7af3290
7878
def new_nb(cells=None, meta=None, nbformat=4, nbformat_minor=5):
7979
"Returns an empty new notebook"
8080
cells = [o if isinstance(o,dict) else mk_cell(o) for o in cells]
8181
return dict2nb(cells=cells or [],metadata=meta or {},nbformat=nbformat,nbformat_minor=nbformat_minor)
8282

83-
# %% ../nbs/01_nbio.ipynb
83+
# %% ../nbs/01_nbio.ipynb #7c7bab22
8484
def nb2dict(d, k=None):
8585
"Convert parsed notebook to `dict`"
8686
if k=='source': return d.splitlines(keepends=True)
8787
if isinstance(d, list): return list(map(nb2dict,d))
8888
if not isinstance(d, dict): return d
8989
return dict(**{k:nb2dict(v,k) for k,v in d.items() if k[-1] != '_'})
9090

91-
# %% ../nbs/01_nbio.ipynb
91+
# %% ../nbs/01_nbio.ipynb #21ffe533
9292
def nb2str(nb):
9393
"Convert `nb` to a `str`"
9494
if isinstance(nb, (AttrDict,list)): nb = nb2dict(nb)
9595
return dumps(nb, sort_keys=True, indent=1, ensure_ascii=False) + "\n"
9696

97-
# %% ../nbs/01_nbio.ipynb
97+
# %% ../nbs/01_nbio.ipynb #d979e25a
9898
def write_nb(nb, path):
9999
"Write `nb` to `path`"
100100
new = nb2str(nb)

execnb/shell.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_shell.ipynb.
44

5-
# %% ../nbs/02_shell.ipynb 2
5+
# %% ../nbs/02_shell.ipynb #535003cf
66
from __future__ import annotations
77

88
from fastcore.utils import *
@@ -33,11 +33,11 @@
3333
from .nbio import _dict2obj
3434

3535

36-
# %% auto 0
36+
# %% auto #0
3737
__all__ = ['CaptureShell', 'format_exc', 'NbResult', 'render_outputs', 'find_output', 'out_exec', 'out_stream', 'out_error',
3838
'exec_nb', 'SmartCompleter']
3939

40-
# %% ../nbs/02_shell.ipynb
40+
# %% ../nbs/02_shell.ipynb #6913382c
4141
class _CustDisplayHook(DisplayHook):
4242
def write_output_prompt(self): pass
4343
def write_format_data(self, data, md_dict): pass
@@ -49,7 +49,7 @@ def __repr__(self: ExecutionInfo): return f'cell: {self.raw_cell}; id: {self.cel
4949
@patch
5050
def __repr__(self: ExecutionResult): return f'result: {self.result}; err: {self.error_in_exec}; info: <{self.info}>'
5151

52-
# %% ../nbs/02_shell.ipynb
52+
# %% ../nbs/02_shell.ipynb #e3fb2bee
5353
class CaptureShell(InteractiveShell):
5454
displayhook_class = _CustDisplayHook
5555

@@ -84,7 +84,7 @@ def set_path(self, path):
8484

8585
def enable_gui(self, gui=None): pass
8686

87-
# %% ../nbs/02_shell.ipynb
87+
# %% ../nbs/02_shell.ipynb #93adf867
8888
@patch
8989
def run_cell(self:CaptureShell, raw_cell, store_history=False, silent=False, shell_futures=True, cell_id=None,
9090
stdout=True, stderr=True, display=True, timeout=None, verbose=False):
@@ -98,15 +98,15 @@ def handler(*args): raise TimeoutError()
9898
finally:
9999
if timeout: signal.alarm(0)
100100

101-
# %% ../nbs/02_shell.ipynb
101+
# %% ../nbs/02_shell.ipynb #a1eb7703
102102
def format_exc(e):
103103
"Format exception `e` as a string list"
104104
return traceback.format_exception(type(e), e, e.__traceback__)
105105

106-
# %% ../nbs/02_shell.ipynb
106+
# %% ../nbs/02_shell.ipynb #8ba0ae59
107107
class NbResult(list): pass
108108

109-
# %% ../nbs/02_shell.ipynb
109+
# %% ../nbs/02_shell.ipynb #26bf9161
110110
def _out_stream(text, name): return dict(name=name, output_type='stream', text=text.splitlines(True))
111111
def _out_exc(e):
112112
ename = type(e).__name__
@@ -139,7 +139,7 @@ def _out_nb(o, fmt):
139139
if p["output_type"]=="execute_result": p['execution_count'] = o['execution_count']
140140
return res
141141

142-
# %% ../nbs/02_shell.ipynb
142+
# %% ../nbs/02_shell.ipynb #242f732f
143143
@patch
144144
def run(self:CaptureShell,
145145
code:str, # Python/IPython code to run
@@ -153,7 +153,7 @@ def run(self:CaptureShell,
153153
self.exc = res.exception
154154
return _out_nb(res, self.display_formatter)
155155

156-
# %% ../nbs/02_shell.ipynb
156+
# %% ../nbs/02_shell.ipynb #eaa11df9
157157
@patch
158158
async def run_async(self:CaptureShell,
159159
code: str, # Python/IPython code to run
@@ -163,7 +163,7 @@ async def run_async(self:CaptureShell,
163163
verbose:bool=False): # Show stdout/stderr during execution
164164
return self.run(code, stdout=stdout, stderr=stderr, timeout=timeout, verbose=verbose)
165165

166-
# %% ../nbs/02_shell.ipynb
166+
# %% ../nbs/02_shell.ipynb #f698a432
167167
def _pre(s, xtra=''): return f"<pre {xtra}><code>{escape(s)}</code></pre>"
168168
def _strip(s): return strip_ansi(escape(s))
169169

@@ -198,7 +198,7 @@ def render_output(out):
198198

199199
return '\n'.join(map(render_output, outputs))
200200

201-
# %% ../nbs/02_shell.ipynb
201+
# %% ../nbs/02_shell.ipynb #c6176b9e
202202
@patch
203203
def cell(self:CaptureShell, cell, stdout=True, stderr=True, verbose=False):
204204
"Run `cell`, skipping if not code, and store outputs back in cell"
@@ -207,32 +207,32 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True, verbose=False):
207207
outs = self.run(cell.source, verbose=verbose)
208208
if outs: cell.outputs = _dict2obj(outs)
209209

210-
# %% ../nbs/02_shell.ipynb
210+
# %% ../nbs/02_shell.ipynb #008c0cef
211211
def find_output(outp, # Output from `run`
212212
ot='execute_result' # Output_type to find
213213
):
214214
"Find first output of type `ot` in `CaptureShell.run` output"
215215
return first(o for o in outp if o['output_type']==ot)
216216

217-
# %% ../nbs/02_shell.ipynb
217+
# %% ../nbs/02_shell.ipynb #6e677414
218218
def out_exec(outp):
219219
"Get data from execution result in `outp`."
220220
out = find_output(outp)
221221
if out: return '\n'.join(first(out['data'].values()))
222222

223-
# %% ../nbs/02_shell.ipynb
223+
# %% ../nbs/02_shell.ipynb #c7352a2b
224224
def out_stream(outp):
225225
"Get text from stream in `outp`."
226226
out = find_output(outp, 'stream')
227227
if out: return ('\n'.join(out['text'])).strip()
228228

229-
# %% ../nbs/02_shell.ipynb
229+
# %% ../nbs/02_shell.ipynb #7d52326c
230230
def out_error(outp):
231231
"Get traceback from error in `outp`."
232232
out = find_output(outp, 'error')
233233
if out: return '\n'.join(out['traceback'])
234234

235-
# %% ../nbs/02_shell.ipynb
235+
# %% ../nbs/02_shell.ipynb #60a25f27
236236
def _false(o): return False
237237

238238
@patch
@@ -253,7 +253,7 @@ def run_all(self:CaptureShell,
253253
postproc(cell)
254254
if self.exc and exc_stop: raise self.exc from None
255255

256-
# %% ../nbs/02_shell.ipynb
256+
# %% ../nbs/02_shell.ipynb #70808010
257257
@patch
258258
def execute(self:CaptureShell,
259259
src:str|Path, # Notebook path to read from
@@ -275,7 +275,7 @@ def execute(self:CaptureShell,
275275
inject_code=inject_code, inject_idx=inject_idx, verbose=verbose)
276276
if dest: write_nb(nb, dest)
277277

278-
# %% ../nbs/02_shell.ipynb
278+
# %% ../nbs/02_shell.ipynb #694161cb
279279
@patch
280280
def prettytb(self:CaptureShell,
281281
fname:str|Path=None): # filename to print alongside the traceback
@@ -287,7 +287,7 @@ def prettytb(self:CaptureShell,
287287
fname_str = f' in {fname}' if fname else ''
288288
return f"{type(self.exc).__name__}{fname_str}:\n{_fence}\n{cell_str}\n"
289289

290-
# %% ../nbs/02_shell.ipynb
290+
# %% ../nbs/02_shell.ipynb #1227c8b1
291291
@call_parse
292292
def exec_nb(
293293
src:str, # Notebook path to read from
@@ -302,7 +302,7 @@ def exec_nb(
302302
CaptureShell().execute(src, dest, exc_stop=exc_stop, inject_code=inject_code,
303303
inject_path=inject_path, inject_idx=inject_idx, verbose=verbose)
304304

305-
# %% ../nbs/02_shell.ipynb
305+
# %% ../nbs/02_shell.ipynb #c44963a0
306306
class SmartCompleter(IPCompleter):
307307
def __init__(self, shell, namespace=None, jedi=False):
308308
if namespace is None: namespace = shell.user_ns
@@ -322,7 +322,7 @@ def __call__(self, c):
322322
if res and res[0][-1]=='=': res = [o for o in res if o[-1]=='=']
323323
return res
324324

325-
# %% ../nbs/02_shell.ipynb
325+
# %% ../nbs/02_shell.ipynb #7d7c852c
326326
@patch
327327
def complete(self:CaptureShell, c):
328328
if not hasattr(self, '_completer'): self._completer = SmartCompleter(self)

nbs/index.ipynb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5+
"id": "a6ece3e1",
56
"metadata": {},
67
"source": [
78
"[![CI](https://github.com/fastai/execnb/actions/workflows/test.yaml/badge.svg)](https://github.com/fastai/execnb/actions/workflows/test.yaml) [![Deploy to GitHub Pages](https://github.com/fastai/execnb/actions/workflows/deploy.yaml/badge.svg)](https://github.com/fastai/execnb/actions/workflows/deploy.yaml)"
89
]
910
},
1011
{
1112
"cell_type": "markdown",
13+
"id": "cebbd70a",
1214
"metadata": {},
1315
"source": [
1416
"# execnb\n",
@@ -18,13 +20,15 @@
1820
},
1921
{
2022
"cell_type": "markdown",
23+
"id": "19fba324",
2124
"metadata": {},
2225
"source": [
2326
"## Install"
2427
]
2528
},
2629
{
2730
"cell_type": "markdown",
31+
"id": "9235036f",
2832
"metadata": {},
2933
"source": [
3034
"Either:\n",
@@ -40,13 +44,15 @@
4044
},
4145
{
4246
"cell_type": "markdown",
47+
"id": "ba2d91de",
4348
"metadata": {},
4449
"source": [
4550
"## How to use"
4651
]
4752
},
4853
{
4954
"cell_type": "markdown",
55+
"id": "323dd8ea",
5056
"metadata": {},
5157
"source": [
5258
"Use `CaptureShell` to run Jupyter code and capture notebook outputs, without running a Jupyter server (or even having it installed):"
@@ -55,6 +61,7 @@
5561
{
5662
"cell_type": "code",
5763
"execution_count": null,
64+
"id": "55f62b7c",
5865
"metadata": {},
5966
"outputs": [],
6067
"source": [
@@ -66,6 +73,7 @@
6673
{
6774
"cell_type": "code",
6875
"execution_count": null,
76+
"id": "481c4034",
6977
"metadata": {},
7078
"outputs": [
7179
{
@@ -89,6 +97,7 @@
8997
},
9098
{
9199
"cell_type": "markdown",
100+
"id": "125143bc",
92101
"metadata": {},
93102
"source": [
94103
"To execute a notebook and save it with outputs filled in, use `CaptureShell.execute`:"
@@ -97,6 +106,7 @@
97106
{
98107
"cell_type": "code",
99108
"execution_count": null,
109+
"id": "457df56e",
100110
"metadata": {},
101111
"outputs": [
102112
{
@@ -116,6 +126,7 @@
116126
},
117127
{
118128
"cell_type": "markdown",
129+
"id": "d787e3a7",
119130
"metadata": {},
120131
"source": [
121132
"You can also execute notebooks from the command line with `exec_nb`:"
@@ -124,6 +135,7 @@
124135
{
125136
"cell_type": "code",
126137
"execution_count": null,
138+
"id": "3a1c97b8",
127139
"metadata": {},
128140
"outputs": [
129141
{
@@ -164,5 +176,5 @@
164176
}
165177
},
166178
"nbformat": 4,
167-
"nbformat_minor": 4
179+
"nbformat_minor": 5
168180
}

0 commit comments

Comments
 (0)