Skip to content

Commit

Permalink
Make it an IPython extension
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Mar 14, 2016
1 parent d18317d commit 5aa2237
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -198,6 +198,52 @@
"\n",
"print(stdout.getvalue())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## IPython extension\n",
"\n",
"You can also enable wurlitzer as an IPython extension,\n",
"so that it always forwards C-level output during execution:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%load_ext wurlitzer"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello from C, 0!\n",
"Hello from C, 1!\n",
"Hello from C, 2!\n",
"Hello from C, 3!\n",
"Hello from C, 4!\n"
]
}
],
"source": [
"for i in range(5):\n",
" time.sleep(1)\n",
" printf(\"Hello from C, %i!\" % i)"
]
}
],
"metadata": {
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ with sys_pipes():
call_some_c_function()
```

Or even simpler, enable it as an IPython extension:

```
%load_ext wurlitzer
```

To forward all C-level output to IPython during execution.

## Acknowledgments

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.
Expand Down
19 changes: 19 additions & 0 deletions wurlitzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,22 @@ def stop_sys_pipes():
_mighty_wurlitzer.__exit__(None, None, None)
_mighty_wurlitzer = None


def load_ipython_extension(ip):
"""Register me as an IPython extension
Captures all C output during execution and forwards to sys.
Use: %load_ext wurlitzer
"""
ip.events.register('pre_execute', sys_pipes_forever)
ip.events.register('post_execute', stop_sys_pipes)


def unload_ipython_extension(ip):
"""Unload me as an IPython extension
Use: %unload_ext wurlitzer
"""
ip.events.unregister('pre_execute', sys_pipes_forever)
ip.events.unregister('post_execute', stop_sys_pipes)

0 comments on commit 5aa2237

Please sign in to comment.