medialab / ipysigma

A Jupyter widget using sigma.js to render interactive networks.
https://medialab.github.io/ipysigma/demo.html
MIT License
206 stars 19 forks source link

How to save the html as an image from the code #221

Closed charlesdedampierre closed 10 months ago

charlesdedampierre commented 11 months ago

Is it possible save a png directly from the code ? (without clicking on the png button on the html file?)

Best,

Yomguithereal commented 11 months ago

It could be yes, but this would happen asynchronously, which means the widget must be displayed in the desired state before triggering the PNG snapshot generation and download. Unfortunately there is no convincing cross-notebook implementation to handle asynchronous workflows efficiently beyond using a sleeping time still subject to race condition, which means triggering screenshots of widgets programmatically in, e.g. a loop, would probably fail randomly at some point. But if you need to rely on the user to make sure we have waited for the widget to be in the correct state, what is preventing said user to clicking the snapshot button, you see what I mean :) ?

Can you describe your use case a little bit more to assess whether we are in the same scenario or if I am missing something?

charlesdedampierre commented 10 months ago

Thanks for the answers! The use case is when, for research purposes we create hundreds of graph and want to automatically save images that may be incorporated in research papers. When we try different parameters etc having to manually screenshot every time is not the best.

Thanks!

Yomguithereal commented 10 months ago

One would say ipysigma is not the correct tool for this use-case but I see your point. The widget already has some mechanisms to be able to retrieve the screenshot as a base64 string that could be leveraged to create a method saving the screenshot on disk from python's side. The only issue here is the asynchronous nature of the operation, that Jupyter does not handle very well at all. Using some time.sleep duct tape you could tinker and make something work but you will still be subject to race conditions if you are unlucky. Let me see if I can add something in the future.

charlesdedampierre commented 10 months ago

Alright thanks!