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

Saving interaction JS to file #163

Open
richardjgowers opened this issue Oct 11, 2023 · 1 comment
Open

Saving interaction JS to file #163

richardjgowers opened this issue Oct 11, 2023 · 1 comment
Labels
enhancement New feature or request visualization Visualization

Comments

@richardjgowers
Copy link

I'd really like the ipython notebook widget saved to png/svg. This is probably a pain, has it been looked into previously?

@cbouy
Copy link
Member

cbouy commented Oct 11, 2023

Did a bit of testing, this seems to work:

from prolif.plotting.network import LigNetwork
from uuid import uuid4
from html import escape
from IPython.display import HTML, Javascript

class BetterLigNetwork(LigNetwork):
    def display(self, **kwargs):
        """Prepare and display the network"""
        html = self._get_html(**kwargs)
        self.uuid = uuid4().hex
        iframe = (
            '<iframe id="{uuid}" width="{width}" height="{height}" frameborder="0" '
            'srcdoc="{doc}"></iframe>'
        )
        return HTML(
            iframe.format(width=self.width, height=self.height, doc=escape(html), uuid=self.uuid)
        )
    
    def save_png(self):
        code = """
        var iframe = document.getElementById("%(uuid)s");
        var iframe_doc = iframe.contentWindow.document;
        var canvas = iframe_doc.getElementsByTagName("canvas")[0];
        var link = document.createElement("a");
        link.href = canvas.toDataURL();
        link.download = "prolif-lignetwork.png"
        link.click();
        """ % {"uuid": self.uuid}
        return Javascript(code)
    
net = BetterLigNetwork.from_fingerprint(fp, ligand_mol)
net.display()

and when you're done drag-n-dropping things around, you'd simply run net.save_png(). You won't get the legend in the export though but that's less problematic.

Probably worth adding to the codebase at some point!

@cbouy cbouy added enhancement New feature or request visualization Visualization labels Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request visualization Visualization
Projects
None yet
Development

No branches or pull requests

2 participants