ipycanvas
ipycanvas copied to clipboard
Why can not to_file
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
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.
ok. Does this mean that I can't use ipycanvas in py files, if I want to export pictures (to_file) ?
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.
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
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