Skip to content

Conversation

@vladko312
Copy link

This can be used to add JS code to node/edge properties.

from pyvis.JScode import JS, JSONEncoder
import json

obj = {'num': 1, 'str': 'some string', 'js': JS('alert("test")')}
js_obj = json.dumps(obj, cls=JSONEncoder)
# js_obj == '{"num": 1, "str": "some string", "js": alert("test")}'

For example, it can set ctxRenderer or apply this fix after adding the functions into the template.

Example:

from pyvis.JScode import JS

node_title = JS("""htmlTitle(
      "Go wild <'span style='display: inline-block; animation: be-tacky 5s ease-in-out alternate infinite; margin: 5px;'>!<'/span>"
    )""")
# to be used in Network.add_node()

I would also recommend adding this code somewhere in template scripts:

function preTitle(text) {
  const container = document.createElement("pre");
  container.innerText = text;
  return container;
}
function htmlTitle(html) {
  const container = document.createElement("div");
  container.innerHTML = html;
  return container;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant