In Jupyter notebook (using with a python or sagemath kernel), when updating the value of a widgets.Label with a string that includes LateX code does not display properly when the update occurs in an interactive environment. Updating the Label value works as expected outside of the interactive environment.
I haven't programed in 20 years and not in python. I hope I have done my due deligence to make sure this is not a question asked before. I am trying to do a user interface for a large demonstration for calculus I. As part of that interface I use widgets.Labels to display some LaTeX code. There is no problem with the LaTeX code being displayed properly when used to set the value of the Label at creation time. Also setting Label.value to a string works as expected, as seen below (test widget created with a simple string and then test.value set to a string with LaTeX code.) The problem occurs when test.value is updated within a response to an interactive change in some widget. The clean code showing this is below:
import ipywidgets as widgets
slider = widgets.IntSlider(min=0, max=1)
test = widgets.Label('test')
test.value= "$\\alpha$"
display(slider, test)
def on_value_change(change):
if (slider.value == 0):
test.value = '$\\alpha$'
else:
test.value = '$\\beta$'
slider.observe(on_value_change, names='value')
I have checked this code using Jupyter notebooks using a python and sagemath kernels in a cocalc environment.
If the code is executed, the value of the test widget is updated on the command line resulting in a very nice alpha. If the slider is moved from 0 to 1, the test widget is updated and displays the LaTeX code, not a nice beta. It does not matter if you use a f-string or a r-string. Behavior is the same.
https://hub.binder.curvenote.dev/user/fomightez-3dsca-ayground-binder-ym6m70fu/, and then addtree(essentially trimming off the part at the end of the starting URL that islab/tree/48a6b7f76c7734527a058087a06d8658/mathjax_label_test.ipynband replacing it withtree) and hit enter. Now click into the directory48a6b7f76c7734527a058087a06d8658and open the notebookmathjax_label_test.ipynb.