Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow c printf to print in ipython notebook in cython cell?

%%cython
from libc.stdio cimport printf
def test():
    printf('abc')

If I run test(), it doesn't print anything.

Currently I am doing something stupid as:

cdef char s[80]
sprintf(s, 'something')
print s

What's a better way to use printf in cython? Why doesn't it print?

like image 396
colinfang Avatar asked Sep 28 '22 11:09

colinfang


1 Answers

You can use the wurlitzer package to capture C-level stdout / stderr and redirect it to IPython.

For example, include the following code blocks in your Jupyter notebook:

%load_ext Cython
%load_ext wurlitzer
%%cython
from libc.stdio cimport printf
def test():
    printf('abc')
test()
# prints "abc"
like image 111
ostrokach Avatar answered Nov 15 '22 09:11

ostrokach