pyvis icon indicating copy to clipboard operation
pyvis copied to clipboard

Show function that does not write a file.

Open pfebrer opened this issue 2 years ago • 0 comments

Hi! Thanks for the package, it is very nice!

It seems odd to me that when calling show a file is written to disk. I wrote my own show function using your html generator, which doesn't write the file. Here it is in case you want to use it:

def show(net, notebook=False):
    html = net.generate_html(notebook=notebook)

    if notebook:
        # Render HTML directly on the notebook. It needs to be isolated so that the CSS does not mess
        # with the rest of the notebook.
        from IPython.display import display, HTML
        obj = HTML(f'<div style="height:{net.height}">{html}</div>', metadata={"isolated": True}, )
        display(obj)
    else:
        # Write to a temporary file and open it in the webbrowser
        import tempfile
        import webbrowser

        with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:
            f.write(html)
            name = f.name
        
            webbrowser.open(name)

pfebrer avatar Jan 08 '23 20:01 pfebrer