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

JavaScript Object support #236

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

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