Skip to content

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

Closed
@DonJayamanne

Description

@DonJayamanne

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions