Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Label text content not initialized if MathJax and latexTypesetter not initialized #3961

Open
DonJayamanne opened this issue Oct 29, 2024 · 0 comments · May be fixed by #3962
Open

Label text content not initialized if MathJax and latexTypesetter not initialized #3961

DonJayamanne opened this issue Oct 29, 2024 · 0 comments · May be fixed by #3962

Comments

@DonJayamanne
Copy link
Contributor

DonJayamanne commented Oct 29, 2024

Description

In previous versions 5.0.3, when rendering a Label widget, the text would be renderer.
However in the latest version it is not renderer.

Reproduce

  1. You need to install VS Code
  2. Run the following cell in a notebook
import ipywidgets as widgets


widgets.Label(value='Label Widget')

Expected behavior

The label should be rendered

Context

Sorry, but I do not think the rest is necessary, hence skipping that.
Its pretty easy to see whats going on here, see below

  • In VS Code, window.MathJax is undefined
  • In VS Code latexTypesetter is also undefined
  • As a result, the element content is never set.
  typeset(element: HTMLElement, text?: string): void {
    this.displayed.then(() => {
      if ((window as any).MathJax?.Hub?.Queue) {
        return typeset(element, text);
      }
      const widget_manager: any = this.model.widget_manager;
      const latexTypesetter = widget_manager._rendermime?.latexTypesetter;
      if (latexTypesetter) {
        if (text !== void 0) {
          element.textContent = text;
        }
        latexTypesetter.typeset(element);
      }
    });
  }

Note:
The typeset function in utils.ts will already check whether MathJax is available.
Hence there's no need to test the availability.

export function typeset(element: HTMLElement, text?: string): void {
  if (text !== void 0) {
    element.textContent = text;
  }
  if ((window as any).MathJax !== void 0) {
    MathJax!.Hub!.Queue(['Typeset', MathJax.Hub, element]);
  }
}

I think the right fix is

  typeset(element: HTMLElement, text?: string): void {
    this.displayed.then(() => {
      const widget_manager: any = this.model.widget_manager;
      const latexTypesetter = widget_manager._rendermime?.latexTypesetter;
      if (latexTypesetter) {
        if (text !== void 0) {
          element.textContent = text;
        }
        latexTypesetter.typeset(element);
      } else {
        return typeset(element, text);
      }
    });
  }

Previously it used to be this.displayed.then(() => typeset(element, text));
The change was introduced in this commit (basically there's never a fallback if either one is not present)
388c782#diff-5cc2749aa43c522b3c6b9485d1c9f7b93df1536d43c18ca94553f3a55f70b03fL69

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant