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 7a6dce1
Show file tree
Hide file tree
Showing 6 changed files with 49 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()
}
}
}
30 changes: 30 additions & 0 deletions tests/integration/latex_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ipywidgets as widgets
import playwright.sync_api
import pytest
from IPython.display import display

from .conftest import SERVERS

widgets_version_major = int(widgets.__version__.split(".")[0])
widgets_postfix = f"-ipywidgets-{widgets_version_major}"


def test_widget_latex_solara(solara_test, page_session: playwright.sync_api.Page, assert_solara_snapshot, request):
if request.node.callspec.params["solara_server"] != SERVERS[0]:
pytest.skip("No need to run this test for all servers.")
# 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")
container = widgets.VBox([label], layout={"width": "200px", "height": "100px"})
display(container)

page_session.locator(".test-class-latex >> .mjx-c1D438").wait_for()
page_session.evaluate("document.fonts.ready")
page_session.wait_for_timeout(1000)
assert_solara_snapshot(page_session.locator(".test-class-latex").screenshot(), postfix=widgets_postfix)
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(1000)
assert_solara_snapshot(page_session.locator(".test-changed-class-latex").screenshot(), postfix=widgets_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 7a6dce1

Please sign in to comment.