Skip to content

Commit e7eed91

Browse files
committed
fixes #65
1 parent 23d3b6f commit e7eed91

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

execnb/shell.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ def _out_nb(o, fmt):
130130
for x in o.display_objects: res.append(_mk_out(x.data, x.metadata))
131131
if r is not None and not o.quiet:
132132
res.append(_mk_out(*fmt.format(r), 'execute_result'))
133+
if 'execution_count' not in o: o['execution_count']=None
134+
for p in res: p['execution_count'] = o['execution_count']
133135
return res
134136

135137
# %% ../nbs/02_shell.ipynb
@@ -194,10 +196,7 @@ def cell(self:CaptureShell, cell, stdout=True, stderr=True):
194196
if cell.cell_type!='code': return
195197
self._cell_idx = cell.idx_ + 1
196198
outs = self.run(cell.source)
197-
if outs:
198-
cell.outputs = _dict2obj(outs)
199-
for o in outs:
200-
if 'execution_count' in o: cell['execution_count'] = o['execution_count']
199+
if outs: cell.outputs = _dict2obj(outs)
201200

202201
# %% ../nbs/02_shell.ipynb
203202
def find_output(outp, # Output from `run`

nbs/02_shell.ipynb

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@
707707
" for x in o.display_objects: res.append(_mk_out(x.data, x.metadata))\n",
708708
" if r is not None and not o.quiet:\n",
709709
" res.append(_mk_out(*fmt.format(r), 'execute_result'))\n",
710+
" if 'execution_count' not in o: o['execution_count']=None\n",
711+
" for p in res: p['execution_count'] = o['execution_count']\n",
710712
" return res"
711713
]
712714
},
@@ -747,7 +749,10 @@
747749
{
748750
"data": {
749751
"text/plain": [
750-
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']}]"
752+
"[{'name': 'stdout',\n",
753+
" 'output_type': 'stream',\n",
754+
" 'text': ['1\\n'],\n",
755+
" 'execution_count': None}]"
751756
]
752757
},
753758
"execution_count": null,
@@ -776,11 +781,13 @@
776781
"text/plain": [
777782
"[{'name': 'stdout',\n",
778783
" 'output_type': 'stream',\n",
779-
" 'text': ['CPU times: user 1 us, sys: 1 us, total: 2 us\\n',\n",
780-
" 'Wall time: 3.1 us\\n']},\n",
784+
" 'text': ['CPU times: user 1 us, sys: 0 ns, total: 1 us\\n',\n",
785+
" 'Wall time: 4.05 us\\n'],\n",
786+
" 'execution_count': None},\n",
781787
" {'data': {'text/plain': ['2']},\n",
782788
" 'metadata': {},\n",
783-
" 'output_type': 'execute_result'}]"
789+
" 'output_type': 'execute_result',\n",
790+
" 'execution_count': None}]"
784791
]
785792
},
786793
"execution_count": null,
@@ -862,11 +869,13 @@
862869
" 'File \\x1b[0;32m<ipython-input-1-9e1622b385b6>:1\\x1b[0m\\n',\n",
863870
" '\\x1b[0;32m----> 1\\x1b[0m \\x1b[38;5;241m1\\x1b[39m\\x1b[38;5;241m/\\x1b[39m\\x1b[38;5;241m0\\x1b[39m\\n',\n",
864871
" '\\n',\n",
865-
" '\\x1b[0;31mZeroDivisionError\\x1b[0m: division by zero\\n']},\n",
872+
" '\\x1b[0;31mZeroDivisionError\\x1b[0m: division by zero\\n'],\n",
873+
" 'execution_count': None},\n",
866874
" {'ename': 'ZeroDivisionError',\n",
867875
" 'evalue': 'division by zero',\n",
868876
" 'output_type': 'error',\n",
869-
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-9e1622b385b6>\", line 1, in <module>\\n 1/0\\n ~^~\\nZeroDivisionError: division by zero\\n'}]"
877+
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-9e1622b385b6>\", line 1, in <module>\\n 1/0\\n ~^~\\nZeroDivisionError: division by zero\\n',\n",
878+
" 'execution_count': None}]"
870879
]
871880
},
872881
"execution_count": null,
@@ -887,7 +896,10 @@
887896
{
888897
"data": {
889898
"text/plain": [
890-
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['no timeout\\n']}]"
899+
"[{'name': 'stdout',\n",
900+
" 'output_type': 'stream',\n",
901+
" 'text': ['no timeout\\n'],\n",
902+
" 'execution_count': None}]"
891903
]
892904
},
893905
"execution_count": null,
@@ -947,7 +959,8 @@
947959
"text/plain": [
948960
"[{'data': {'text/plain': ['2']},\n",
949961
" 'metadata': {},\n",
950-
" 'output_type': 'execute_result'}]"
962+
" 'output_type': 'execute_result',\n",
963+
" 'execution_count': None}]"
951964
]
952965
},
953966
"execution_count": null,
@@ -1013,11 +1026,7 @@
10131026
"----&gt; 1 import time; time.sleep(1.1)\n",
10141027
"\n",
10151028
"Cell In[6], line 7, in run_cell.&lt;locals&gt;.handler(*args)\n",
1016-
" 5 if not timeout: timeout = self.timeout\n",
1017-
" 6 if timeout:\n",
1018-
"----&gt; 7 def handler(*args): raise TimeoutError()\n",
1019-
" 8 signal.signal(signal.SIGALRM, handler)\n",
1020-
" 9 signal.alarm(timeout)\n",
1029+
"----&gt; 7 def handler(*args): raise TimeoutError()\n",
10211030
"\n",
10221031
"TimeoutError: \n",
10231032
"</code></pre>\n"
@@ -1056,11 +1065,7 @@
10561065
"<span class=\"ansi-green-fg\">----&gt; 1</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">import</span> <span class=\"ansi-bold\" style=\"color: rgb(0,0,255)\">time</span>; time<span style=\"color: rgb(98,98,98)\">.</span>sleep(<span style=\"color: rgb(98,98,98)\">1.1</span>)\n",
10571066
"\n",
10581067
"Cell <span class=\"ansi-green-fg\">In[6], line 7</span>, in <span class=\"ansi-cyan-fg\">run_cell.&lt;locals&gt;.handler</span><span class=\"ansi-blue-fg\">(*args)</span>\n",
1059-
"<span class=\"ansi-green-intense-fg ansi-bold\"> 5</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">if</span> <span class=\"ansi-bold\" style=\"color: rgb(175,0,255)\">not</span> timeout: timeout <span style=\"color: rgb(98,98,98)\">=</span> <span style=\"color: rgb(0,135,0)\">self</span><span style=\"color: rgb(98,98,98)\">.</span>timeout\n",
1060-
"<span class=\"ansi-green-intense-fg ansi-bold\"> 6</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">if</span> timeout:\n",
1061-
"<span class=\"ansi-green-fg\">----&gt; 7</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">def</span> <span style=\"color: rgb(0,0,255)\">handler</span>(<span style=\"color: rgb(98,98,98)\">*</span>args): <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">raise</span> <span class=\"ansi-bold\" style=\"color: rgb(215,95,95)\">TimeoutError</span>()\n",
1062-
"<span class=\"ansi-green-intense-fg ansi-bold\"> 8</span> signal<span style=\"color: rgb(98,98,98)\">.</span>signal(signal<span style=\"color: rgb(98,98,98)\">.</span>SIGALRM, handler)\n",
1063-
"<span class=\"ansi-green-intense-fg ansi-bold\"> 9</span> signal<span style=\"color: rgb(98,98,98)\">.</span>alarm(timeout)\n",
1068+
"<span class=\"ansi-green-fg\">----&gt; 7</span> <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">def</span> <span style=\"color: rgb(0,0,255)\">handler</span>(<span style=\"color: rgb(98,98,98)\">*</span>args): <span class=\"ansi-bold\" style=\"color: rgb(0,135,0)\">raise</span> <span class=\"ansi-bold\" style=\"color: rgb(215,95,95)\">TimeoutError</span>()\n",
10641069
"\n",
10651070
"<span class=\"ansi-red-fg\">TimeoutError</span>: \n",
10661071
"</code></pre>\n"
@@ -1134,11 +1139,13 @@
11341139
" 'File \\x1b[0;32m<ipython-input-1-01648acb07bd>:1\\x1b[0m\\n',\n",
11351140
" '\\x1b[0;32m----> 1\\x1b[0m \\x1b[38;5;28;01mraise\\x1b[39;00m \\x1b[38;5;167;01mException\\x1b[39;00m(\\x1b[38;5;124m\"\\x1b[39m\\x1b[38;5;124mOops\\x1b[39m\\x1b[38;5;124m\"\\x1b[39m)\\n',\n",
11361141
" '\\n',\n",
1137-
" '\\x1b[0;31mException\\x1b[0m: Oops\\n']},\n",
1142+
" '\\x1b[0;31mException\\x1b[0m: Oops\\n'],\n",
1143+
" 'execution_count': None},\n",
11381144
" {'ename': 'Exception',\n",
11391145
" 'evalue': 'Oops',\n",
11401146
" 'output_type': 'error',\n",
1141-
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-01648acb07bd>\", line 1, in <module>\\n raise Exception(\"Oops\")\\nException: Oops\\n'}]"
1147+
" 'traceback': 'Traceback (most recent call last):\\n File \"/Users/jhoward/miniconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\\n exec(code_obj, self.user_global_ns, self.user_ns)\\n File \"<ipython-input-1-01648acb07bd>\", line 1, in <module>\\n raise Exception(\"Oops\")\\nException: Oops\\n',\n",
1148+
" 'execution_count': None}]"
11421149
]
11431150
},
11441151
"execution_count": null,
@@ -1184,10 +1191,7 @@
11841191
" if cell.cell_type!='code': return\n",
11851192
" self._cell_idx = cell.idx_ + 1\n",
11861193
" outs = self.run(cell.source)\n",
1187-
" if outs:\n",
1188-
" cell.outputs = _dict2obj(outs)\n",
1189-
" for o in outs:\n",
1190-
" if 'execution_count' in o: cell['execution_count'] = o['execution_count']"
1194+
" if outs: cell.outputs = _dict2obj(outs)"
11911195
]
11921196
},
11931197
{
@@ -1238,10 +1242,14 @@
12381242
{
12391243
"data": {
12401244
"text/plain": [
1241-
"[{'name': 'stdout', 'output_type': 'stream', 'text': ['1\\n']},\n",
1245+
"[{'name': 'stdout',\n",
1246+
" 'output_type': 'stream',\n",
1247+
" 'text': ['1\\n'],\n",
1248+
" 'execution_count': None},\n",
12421249
" {'data': {'text/plain': ['2']},\n",
12431250
" 'metadata': {},\n",
1244-
" 'output_type': 'execute_result'}]"
1251+
" 'output_type': 'execute_result',\n",
1252+
" 'execution_count': None}]"
12451253
]
12461254
},
12471255
"execution_count": null,

0 commit comments

Comments
 (0)