Zulko / gizeh

Simple Vector Graphics for Python
Other
702 stars 70 forks source link

surface.ipython_display() fails in python 3 #31

Open ghost opened 7 years ago

ghost commented 7 years ago

The following snippet produces no output in jupyter with python3.

import gizeh as gz
W, H = 500, 300
r, gray, t = 25., .3, 1.

surface = gz.Surface(W,H, bg_color=(1, 1, 1)) # white background
gradient = gz.ColorGradient(type="radial", stops_colors=[(0,(gray, gray, gray)), (1, (1, 1, 1))],
                            xy1=[0, 0], xy2=[0, 0], xy3=[0, r])
rf = gz.circle(r=r, xy=(W/2., H/2.), fill=gradient)
#rect = gz.rectangle(lx=.3*H, ly=.02*H, xy=(W*t/duration, H/2.), fill=(0,1,0), angle=np.pi/2)

rf.draw(surface)
surface.ipython_display()

The problem is in Surface._repr_png_(). Python 3 doesn't have the cStringIO module, so the line data = StringIO() fails (silently). If I instead use the io module as suggested here, the snippet above generates an error from cairo.

YPares commented 6 years ago

I found that extension for jupyter https://github.com/fomightez/cairo-jupyter

Is there a way to access the underlying cairo surface/context of a gizeh surface?

EDIT: Yes, with _cairo_surface. And it seems to work alright :)