ipycanvas icon indicating copy to clipboard operation
ipycanvas copied to clipboard

Why can not to_file

Open zhijiejia opened this issue 3 years ago • 5 comments

canvas = Canvas(width=200, height=200, sync_image_data=True)
canvas.fill_styled_rects(20, 20, 50, 50, color=[0,0,0])
canvas.to_file('test.png')
Traceback (most recent call last):
  File "/home/jzj/test.py", line 58, in <module>
    canvas.to_file('test.png')
  File "/home/jzj/miniconda3/lib/python3.9/site-packages/ipycanvas/canvas.py", line 420, in to_file
    raise RuntimeError(
RuntimeError: No image data to save, please be sure that ``sync_image_data`` is set to True

zhijiejia avatar Aug 12 '22 04:08 zhijiejia

Thanks for opening an issue.

You need to make sure that the canvas has been updated before being able to to to_file. Try executing the to_file in a separate Notebook cell.

martinRenou avatar Aug 12 '22 06:08 martinRenou

ok. Does this mean that I can't use ipycanvas in py files, if I want to export pictures (to_file) ?

zhijiejia avatar Aug 12 '22 14:08 zhijiejia

It is indeed annoying in this case.

I've been thinking of allowing to make the to_file method async:

canvas = Canvas()
# Do drawings
await canvas.to_file('test.png')

But there are limitations in ipykernel and xeus-python that prevent from doing this for now. Hopefully it will be fixed at some point if this gets in: https://github.com/ipython/ipykernel/pull/589

Though there might be other approaches. I'll try a few things when I have time.

martinRenou avatar Aug 12 '22 14:08 martinRenou

ipyvolume seems to have a solution for taking screenshots of a widget: https://github.com/widgetti/ipyvolume/blob/master/ipyvolume/pylab.py#L1260-L1271

Thanks @maartenBreddels

martinRenou avatar Aug 12 '22 14:08 martinRenou

It might be that we need a nest_asyncio solution like @mariobuikhuizen did for https://github.com/widgetti/ipyvuetify/blob/master/ipyvuetify/extra/file_input.py

maartenbreddels avatar Aug 12 '22 14:08 maartenbreddels