pyvis icon indicating copy to clipboard operation
pyvis copied to clipboard

'lib' folder - deleting previous contents?!

Open andrei-rusu opened this issue 2 years ago • 7 comments

Much to my surprise, upon updating to the latest version of pyvis, a 'lib' folder with JS files now gets created/updated in the working directory?! Incidentally, all my .py files also live in a 'lib' folder within the same working directory. As such, this latest version completely DELETED all my code. I was lucky to have made a backup recently, otherwise things would have been very bad.

Unfortunately, this 'lib' folder is not bound to the HTML file save location but rather to the working directory itself, making it impossible to maintain the codebase in a 'lib' folder. This needs to be fixed ASAP, and a Warning issued beforehand!

UPDATE: The folder only gets copied over when the 'lib' folder already exists in the working directory. I assume the following is the issue here: https://github.com/WestHealth/pyvis/blob/f4847af7307dc03939305d6e542d9e71ca7e1dda/pyvis/network.py#L511-L513

andrei-rusu avatar Sep 27 '22 19:09 andrei-rusu

Functionally unrelated, but from the same block of code that seems to have been forgotten when cleaning this up for the release: The height of the IFrame is for some reason hardcoded, so the self.height parameter gets completely ignored, cutting the network from the canvas for larger heights. https://github.com/WestHealth/pyvis/blob/f4847af7307dc03939305d6e542d9e71ca7e1dda/pyvis/network.py#L516

andrei-rusu avatar Sep 27 '22 22:09 andrei-rusu

I've taken a look into this and determined how this got in and will fix this soon. Apologies it took as long as it did to get around to this.

BerserkerGaruk avatar Nov 02 '22 22:11 BerserkerGaruk

Hi! This is not fully fixed. If notebook = False and local = True, it can remove a lib directory in the current working directory.

I would like to add the package as a visualization dependency of my package, but this is too dangerous :sweat_smile:

pfebrer avatar Jan 08 '23 18:01 pfebrer

So, on the subject of the lib folder. Is it possible that when a graph is created, the date can all be embedded into a single html file? It would make for a messy html, but it would be portable then (portable as a single file)

JohnOmernik avatar Jan 11 '23 15:01 JohnOmernik

@pfebrer https://github.com/WestHealth/pyvis/blob/write_html_rewrite/pyvis/network.py Working on a rewrite for how the write_html is handled since the change to self.cdn_resources. There were some checks in that function that are no longer relevant, and should imo be religated to that class parameter instead. The local lib folder will then only be created in the case there is no lib folder, and the files will be copied over when it exists.

def write_html(self, name, local=True, notebook=False,open_browser=False):
    """
    This method gets the data structures supporting the nodes, edges,
    and options and updates the template to write the HTML holding
    the visualization.

    To work with the old local methods local is being depricated, but not removed.
    :type name_html: str
    @param name: name of the file to save the graph as.
    @param local: Depricated parameter. Used to be used to determine how the graph needs deploy. Has been removed in favor of using the class cdn_resources instead.
    @param notebook: If true, this object will return the iframe document for use in juptyer notebook.
    @param open_browser: If true, will open a web browser with the generated graph.
    """
    check_html(name)
    self.html = self.generate_html(notebook=notebook)


    if self.cdn_resources == "local":
        if not os.path.exists("lib"):
            os.makedirs("lib")
        if not os.path.exists("lib/bindings"):
            shutil.copytree(f"{os.path.dirname(__file__)}/templates/lib/bindings", "lib/bindings")
        if not os.path.exists("lib/tom-select"):
            shutil.copytree(f"{os.path.dirname(__file__)}/templates/lib/tom-select", "lib/tom-select")
        if not os.path.exists("lib/bindings"):
            shutil.copytree(f"{os.path.dirname(__file__)}/templates/lib/vis-9.1.2", "lib/vis-9.1.2")
        with open(name, "w+") as out:
            out.write(self.html)
    elif self.cdn_resources == "in_line" or self.cdn_resources == "remote":
        with open(name, "w+") as out:
            out.write(self.html)
    else:
        assert "cdn_resources is not in ['in_line','remote','local']."
    if open_browser: # open the saved file in a new browser window.
        webbrowser.open(f"{name}")
    if notebook:
        return IFrame(name, width=self.width, height=self.height)
        

BerserkerGaruk avatar Jan 12 '23 00:01 BerserkerGaruk

This issue just borked my entire python install. Somehow the python folder was my working directory and pyvis overwrote the entire python lib folder.

jgrimard avatar Feb 18 '23 19:02 jgrimard

this issue should be closed. It was resolved by #212

norweeg avatar Apr 03 '24 16:04 norweeg