Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are several ways to send a variable value to the body of an Ipython display javascript function that is subject to change:

  1. Using format() method: In this approach, you can create a string with curly braces {} where you want the variable value to be inserted, and then use the format() method to replace those braces with the desired value.

Example:

from IPython.display import Javascript
var = 5
js_code = """
var x = {};
console.log(x);
""".format(var)
Javascript(js_code)
  1. Using string concatenation: In this approach, you can concatenate the variable value with the string that contains the javascript code.

Example:

from IPython.display import Javascript
var = 5
js_code = """
var x = """ + str(var) + """;
console.log(x);
"""
Javascript(js_code)
  1. Using f-strings: In this approach, you can create a string with a variable value using f-strings (formatted string literals) and then use that string in your javascript code.

Example:

from IPython.display import Javascript
var = 5
js_code = f"""
var x = {var};
console.log(x);
"""
Javascript(js_code)

All of these approaches allow you to send a variable value to the body of an Ipython display javascript function that is subject to change.