Fix handling of set_options() function#307
Fix handling of set_options() function#307OS-of-S wants to merge 2 commits intoWestHealth:masterfrom
Conversation
|
Now I pushed a second commit with changes in network.py. There, as I sugested a few hours before, a just hardcoded check Also, it's really ugly solution, and I just didn't find any better, but now it solving bug completely. And I have to draw attention to some particularly ugly place in my code: I totaly copied a function The reason why I done it — the necessary solution includes merging new options with current options, what is done in my first commit. But after reassigning JSON to options variable, options not an Options class anymore, it's dictionary. Therefore we can't use So... The code could be improved, but I don't know if it should be... The main thing is that it works now. I checked my changes with the next script by commiting different commands in it, so I hope everything works allright: |
|
Instead of mutating self.options into a dict and then wrapping every call with isinstance(self.options, dict), just create an OptionsWrapper class that transparently handles both dict and Options types behind the scenes. Then inside Network: Just wanted to share in case it's helpful! You've clearly put a lot of effort into untangling this |
|
Thanks! That's definitely better. But it seems that library is not maintained at all, so... Probably people will use my workaround from here. And it's far from ideal, but helps to use set_options() at least somehow.
Not really, it took two weekends to find out the reasons of that bug. But thanks for suggestion! Maybe someone would implement it in some better version of pyvis. UPD: Nice spider-project also! |


Documentation says that
Network.set_options()should get JSON options, generated by browser, and set it to the Network, but didn't work at all. Now it's fixed inside of options.py file and work perfectly, but with one small peculiarity. It works only after calling all other functions that related to Options class and not after it. Details of my solution you can find in commentary on this topic: #81 (comment)The root reason for the bug is that
Network.set_options()is settingoptionsvariable todict, but notOptionsclass! That's why all functions fromOptionsclass became unavalieble after callingNetwork.set_options().Also there were some smaller issues like:
del_nulls()subfunction.Network.set_options()rewrote the values of options that were seted before using functions (Network.set_edge_smooth(),Network.show_buttons()etc.) That's whydeep_merge(dict1, dict2)function needed. This merge is "deep", becouse of recursive representation of options in JSON form:"edges": {
"color": {
"inherit": true
}
}
Therefore, I see two ways to solve this bug complitly, so that users could use set_options() few times and another functions from
Optionsclass in any order.First way — is to construnt an
Optionsclass from JSON instead of reasigning it as dictionary. It's truly problematic becouse of JSON file with options consists not only ofOptionsclass' members, but of "nodes" and "edges" too, which, as I can see, not represented in python part of pyvis library. Those go to the input of other imported libraries and set defoult color to Nodes and so on(?) I mean — when Node's color is not clarified, it became the color from those options.And the second way looks easier for me — to check if options is the dict in every function wich operate with it and choose suitable command. It does not require rewriting Options as a class, and similar exception check already exists in pyvis code. But only for one function inside network.py:
I'll try to implement the second solution in the next commit.