Skip to content

Commit

Permalink
fix: re-render latex support
Browse files Browse the repository at this point in the history
When a description changed, update the latex rendering.

Similar to voila-dashboards/voila#531
  • Loading branch information
maartenbreddels committed Jun 16, 2023
1 parent fb8884d commit e0b1720
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/solara-widget-manager/src/mathjax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ RegisterHTMLHandler(browserAdaptor());

// Override dynamically generated fonts in favor
// of our font css that is picked up by webpack.
// @ts-ignore
class emptyFont extends TeXFont {
readonly defaultFonts = {};
// @ts-ignore
static defaultFonts = {};
}

const chtml = new CHTML({
Expand Down Expand Up @@ -57,3 +59,19 @@ export function renderMathJax(): void {
.updateDocument()
.reset();
}
// this makes it compatible with the old MathJax
// see https://github.com/voila-dashboards/voila/pull/531
// @ts-ignore
window.MathJax = {
Hub: {
// @ts-ignore
Queue: ([_ignore, _ignore2, node]) => {
html.findMath({ elements: [node] })
.compile()
.getMetrics()
.typeset()
.updateDocument()
.reset()
}
}
}
21 changes: 21 additions & 0 deletions tests/integration/latex_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ipywidgets as widgets
import playwright.sync_api
from IPython.display import display


def test_widget_latex_solara(solara_test, page_session: playwright.sync_api.Page, assert_solara_snapshot):
# we tried a FloatSlider before, but that seems to give unstable images (handle moves around randomly)
label = widgets.Label(value=r"$E \sim mc^2$")
label.add_class("test-class-latex")
display(label)

page_session.locator(".test-class-latex >> .mjx-c1D438").wait_for()
page_session.evaluate("document.fonts.ready")
page_session.wait_for_timeout(100)
assert_solara_snapshot(page_session.locator(".test-class-latex >> .MathJax").screenshot())
label.value = r"$\alpha$"
label.add_class("test-changed-class-latex")
page_session.locator(".test-changed-class-latex >> .mjx-c1D6FC").wait_for()
page_session.evaluate("document.fonts.ready")
page_session.wait_for_timeout(100)
assert_solara_snapshot(page_session.locator(".test-changed-class-latex >> .MathJax").screenshot(), postfix="-changed")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e0b1720

Please sign in to comment.