Skip to content

Commit 5aa2237

Browse files
committed
Make it an IPython extension
1 parent d18317d commit 5aa2237

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Demo.ipynb

+47-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"cell_type": "code",
5252
"execution_count": 2,
5353
"metadata": {
54-
"collapsed": true
54+
"collapsed": false
5555
},
5656
"outputs": [],
5757
"source": [
@@ -198,6 +198,52 @@
198198
"\n",
199199
"print(stdout.getvalue())"
200200
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"## IPython extension\n",
207+
"\n",
208+
"You can also enable wurlitzer as an IPython extension,\n",
209+
"so that it always forwards C-level output during execution:"
210+
]
211+
},
212+
{
213+
"cell_type": "code",
214+
"execution_count": 8,
215+
"metadata": {
216+
"collapsed": true
217+
},
218+
"outputs": [],
219+
"source": [
220+
"%load_ext wurlitzer"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": 9,
226+
"metadata": {
227+
"collapsed": false
228+
},
229+
"outputs": [
230+
{
231+
"name": "stdout",
232+
"output_type": "stream",
233+
"text": [
234+
"Hello from C, 0!\n",
235+
"Hello from C, 1!\n",
236+
"Hello from C, 2!\n",
237+
"Hello from C, 3!\n",
238+
"Hello from C, 4!\n"
239+
]
240+
}
241+
],
242+
"source": [
243+
"for i in range(5):\n",
244+
" time.sleep(1)\n",
245+
" printf(\"Hello from C, %i!\" % i)"
246+
]
201247
}
202248
],
203249
"metadata": {

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ with sys_pipes():
4242
call_some_c_function()
4343
```
4444

45+
Or even simpler, enable it as an IPython extension:
46+
47+
```
48+
%load_ext wurlitzer
49+
```
50+
51+
To forward all C-level output to IPython during execution.
52+
4553
## Acknowledgments
4654

4755
This package is based on stuff we learned with @takluyver and @karies while working on capturing output from the [Cling Kernel](https://github.com/root-mirror/cling/tree/master/tools/Jupyter/kernel) for Jupyter.

wurlitzer.py

+19
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,22 @@ def stop_sys_pipes():
261261
_mighty_wurlitzer.__exit__(None, None, None)
262262
_mighty_wurlitzer = None
263263

264+
265+
def load_ipython_extension(ip):
266+
"""Register me as an IPython extension
267+
268+
Captures all C output during execution and forwards to sys.
269+
270+
Use: %load_ext wurlitzer
271+
"""
272+
ip.events.register('pre_execute', sys_pipes_forever)
273+
ip.events.register('post_execute', stop_sys_pipes)
274+
275+
276+
def unload_ipython_extension(ip):
277+
"""Unload me as an IPython extension
278+
279+
Use: %unload_ext wurlitzer
280+
"""
281+
ip.events.unregister('pre_execute', sys_pipes_forever)
282+
ip.events.unregister('post_execute', stop_sys_pipes)

0 commit comments

Comments
 (0)