1

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.

8
  • This isn't reproducible. I can show you your shared code works fine in both JupyterLab 4.2.5 and Jupyter Notebook 7.2.2 using ipywidgets 8.1.5. Click here and be patient while a session on a remote machine served by MyBinder spins up. It will start off in JupyterLab, but you can switch to Jupyter Notebook by .... Commented Nov 26, 2024 at 17:35
  • <continued> editing the URL in the address bar. In case something goes wrong, copy the current URL to a new browser window first. Then edit the URL back so it looks something like this, https://hub.binder.curvenote.dev/user/fomightez-3dsca-ayground-binder-ym6m70fu/, and then add tree (essentially trimming off the part at the end of the starting URL that is lab/tree/48a6b7f76c7734527a058087a06d8658/mathjax_label_test.ipynb and replacing it with tree) and hit enter. Now click into the directory 48a6b7f76c7734527a058087a06d8658 and open the notebook mathjax_label_test.ipynb. Commented Nov 26, 2024 at 17:37
  • Thank you for verifying that problem is not reproducible. After seeing your answer, I checked a different browser (safari --> chrome). The problem persisted, so I will now check with cocalc. Thank you very much Commented Nov 26, 2024 at 21:21
  • Hopefully using the link I posted, you could see on a remote machine it works though? Commented Nov 26, 2024 at 21:39
  • 1
    The error described above was an error in the Jupyter notebook implementation in CoCalc. The bug has been squashed by the CoCalc team almost immediately after the ticket on the error was submitted. Thank you to the CoCalc team for very speedy response and for @Wayne to help me determine where the bug resided. I am not sure how to indicate this is a closed issue, but it is a closed issue. Commented Dec 3, 2024 at 6:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.