Skip to content

Commit 882b286

Browse files
committed
add to pythonpath
1 parent 53b3bf9 commit 882b286

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

execnb/_modidx.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
'min_python': '3.7',
2222
'nbs_path': 'nbs',
2323
'recursive': 'False',
24-
'requirements': 'fastcore>=1.3.27',
24+
'requirements': 'fastcore>=1.3.27 ipython',
2525
'status': '2',
2626
'title': 'execnb',
2727
'tst_flags': 'notest',
2828
'user': 'fastai',
29-
'version': '0.0.2'},
29+
'version': '0.0.3'},
3030
'syms': { 'execnb.fastshell': { 'execnb.fastshell.FastInteractiveShell': 'https://fastai.github.io/execnb/fastshell#FastInteractiveShell',
3131
'execnb.fastshell.FastInteractiveShell.ask_exit': 'https://fastai.github.io/execnb/fastshell#FastInteractiveShell.ask_exit',
3232
'execnb.fastshell.FastInteractiveShell.data_pub': 'https://fastai.github.io/execnb/fastshell#FastInteractiveShell.data_pub',
@@ -57,4 +57,5 @@
5757
'execnb.shell.CaptureShell.execute': 'https://fastai.github.io/execnb/shell#CaptureShell.execute',
5858
'execnb.shell.CaptureShell.run': 'https://fastai.github.io/execnb/shell#CaptureShell.run',
5959
'execnb.shell.CaptureShell.run_all': 'https://fastai.github.io/execnb/shell#CaptureShell.run_all',
60+
'execnb.shell.CaptureShell.set_path': 'https://fastai.github.io/execnb/shell#CaptureShell.set_path',
6061
'execnb.shell.exec_nb': 'https://fastai.github.io/execnb/shell#exec_nb'}}}

execnb/shell.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ def __init__(self):
4747
self.exc,self.result,self.out,self.count = None,None,[],1
4848
self.run_cell('%matplotlib inline')
4949

50-
def enable_gui(self, gui=None): pass
50+
def set_path(self, path):
51+
"Add `path` to python path"
52+
self.run_cell(f"import sys; sys.path.insert(0, {str(path)})")
53+
54+
def enable_gui(self, gui=None):
55+
"Disable GUI (over-ridden; called by IPython)"
56+
pass
5157

5258
def _showtraceback(self, etype, evalue, stb: str):
5359
self.out.append(_out_exc(etype, evalue, stb))
@@ -116,7 +122,7 @@ def run_all(self:CaptureShell,
116122
postproc(cell)
117123
if self.exc and exc_stop: raise self.exc[1] from None
118124

119-
# %% ../nbs/02_shell.ipynb 37
125+
# %% ../nbs/02_shell.ipynb 38
120126
@patch
121127
def execute(self:CaptureShell,
122128
src:str|Path, # Notebook path to read from
@@ -130,12 +136,13 @@ def execute(self:CaptureShell,
130136
):
131137
"Execute notebook from `src` and save with outputs to `dest"
132138
nb = read_nb(src)
139+
self.set_path(Path(src).parent.resolve())
133140
if inject_path is not None: inject_code = Path(inject_path).read_text()
134141
self.run_all(nb, exc_stop=exc_stop, preproc=preproc, postproc=postproc,
135142
inject_code=inject_code, inject_idx=inject_idx)
136143
if dest: write_nb(nb, dest)
137144

138-
# %% ../nbs/02_shell.ipynb 47
145+
# %% ../nbs/02_shell.ipynb 49
139146
@call_parse
140147
def exec_nb(
141148
src:str, # Notebook path to read from

nbs/02_shell.ipynb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@
9797
" self.exc,self.result,self.out,self.count = None,None,[],1\n",
9898
" self.run_cell('%matplotlib inline')\n",
9999
"\n",
100-
" def enable_gui(self, gui=None): pass\n",
100+
" def set_path(self, path):\n",
101+
" \"Add `path` to python path\"\n",
102+
" self.run_cell(f\"import sys; sys.path.insert(0, {str(path)})\")\n",
103+
"\n",
104+
" def enable_gui(self, gui=None):\n",
105+
" \"Disable GUI (over-ridden; called by IPython)\"\n",
106+
" pass\n",
101107
"\n",
102108
" def _showtraceback(self, etype, evalue, stb: str):\n",
103109
" self.out.append(_out_exc(etype, evalue, stb))\n",
@@ -200,8 +206,8 @@
200206
" 'execution_count': 1},\n",
201207
" {'name': 'stdout',\n",
202208
" 'output_type': 'stream',\n",
203-
" 'text': ['CPU times: user 2 us, sys: 0 ns, total: 2 us',\n",
204-
" 'Wall time: 3.81 us']}]"
209+
" 'text': ['CPU times: user 3 us, sys: 1 us, total: 4 us',\n",
210+
" 'Wall time: 5.25 us']}]"
205211
]
206212
},
207213
"execution_count": null,
@@ -531,6 +537,15 @@
531537
"except Exception as e: print(f\"got exception: {e}\")"
532538
]
533539
},
540+
{
541+
"cell_type": "code",
542+
"execution_count": null,
543+
"metadata": {},
544+
"outputs": [],
545+
"source": [
546+
"nb = read_nb('../../testw/index.ipynb')"
547+
]
548+
},
534549
{
535550
"cell_type": "markdown",
536551
"metadata": {},
@@ -626,12 +641,22 @@
626641
"):\n",
627642
" \"Execute notebook from `src` and save with outputs to `dest\"\n",
628643
" nb = read_nb(src)\n",
644+
" self.set_path(Path(src).parent.resolve())\n",
629645
" if inject_path is not None: inject_code = Path(inject_path).read_text()\n",
630646
" self.run_all(nb, exc_stop=exc_stop, preproc=preproc, postproc=postproc,\n",
631647
" inject_code=inject_code, inject_idx=inject_idx)\n",
632648
" if dest: write_nb(nb, dest)"
633649
]
634650
},
651+
{
652+
"cell_type": "code",
653+
"execution_count": null,
654+
"metadata": {},
655+
"outputs": [],
656+
"source": [
657+
"s.execute(clean, '../../testw/index.ipynb')"
658+
]
659+
},
635660
{
636661
"cell_type": "markdown",
637662
"metadata": {},

0 commit comments

Comments
 (0)