Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_file is unreliable #222

Open
DangerMouseB opened this issue Oct 6, 2021 · 10 comments · May be fixed by #301
Open

to_file is unreliable #222

DangerMouseB opened this issue Oct 6, 2021 · 10 comments · May be fixed by #301
Labels
enhancement New feature or request

Comments

@DangerMouseB
Copy link

Firstly thank you for creating this - really helpful as a quick and dirty way to plot in jupyter when matplotlib etc are too cumbersome.

I'm having problems with to_file - as in not all the drawn elements that are displaying in the browser are being saved to file. I haven't been able to figure a reliable way of saving all the elements - I suppose it's acting a bit racy.

Any ideas of how to fix? (I've set sync_image_date to true and tried the callback method as well as the direct save).

@martinRenou martinRenou added the enhancement New feature or request label Oct 6, 2021
@martinRenou
Copy link
Collaborator

martinRenou commented Oct 6, 2021

Thank you for opening an issue.

This routine is indeed a bit unreliable, the big problem is that the rendering is done on the front-end (web-page) while your Python code runs on the server, so it's difficult to get in sync.

The callback method might be more reliable than a direct save. Though, for example, you have no certainty that you'll receive back all frames of an animations.
We might be able to improve this though.

I can recommend to run your Python code in a separate Python thread, so that your Python code execution does not block any callback.

@DangerMouseB
Copy link
Author

DangerMouseB commented Oct 6, 2021

Do you happen to have some example code? - it's been 11 years since I did multithreading in python :)

A quick question to save some time, do I need to include all the rendering code, of if I have a canvas object do I just call the to_file method in a separate thread? (I'm hoping the latter).

@martinRenou
Copy link
Collaborator

I would put the rendering code in a separate thread... This example Notebook and this one both use threading so that the animation does not block any callback.

It might be possible to change the ipycanvas implementation so that we manually request image data, and making sure the Python callback gets called.

@profhuster
Copy link

I cannot get to_file to work at all.
Can you give an example notebook that uses it?

@martinRenou
Copy link
Collaborator

@profhuster what code did you try?

Did you try something similar to here? https://ipycanvas.readthedocs.io/en/latest/retrieve_images.html#save-canvas-to-a-file

from ipycanvas import Canvas

canvas = Canvas(width=200, height=200, sync_image_data=True)


def save_to_file(*args, **kwargs):
    canvas.to_file("my_file.png")


# Listen to changes on the ``image_data`` trait and call ``save_to_file`` when it changes.
canvas.observe(save_to_file, "image_data")

# Perform some drawings...

@profhuster
Copy link

Yes. Parts of the image are missing in the file. It is weird because maybe on in ten runs it does have the full drawing. I cannot find any pattern

@profhuster
Copy link

I added canvas.flush() after drawing. That does not help.

@DangerMouseB
Copy link
Author

That was my experience too - which is a shame because the api was nice and simple and I got some nice plots done using it but it was very hard to get them reliable into a png.

@profhuster
Copy link

I will look at pycairo. It is a more mature, and probably complex, package.

@profhuster
Copy link

Also Cairo and pycairo use vector drawing, so it scales without degradation.

@martinRenou martinRenou linked a pull request Oct 24, 2022 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants