From c1727e9e535904ea2451f80d63f58bb2969f8238 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Thu, 15 Jun 2023 17:20:38 +0200 Subject: [PATCH] fix: re-render latex support When a description changed, update the latex rendering. Similar to https://github.com/voila-dashboards/voila/pull/531 --- packages/solara-widget-manager/src/mathjax.ts | 20 +++++++++++++++++- tests/integration/latex_test.py | 20 ++++++++++++++++++ ...flask-chromium-linux-changed-reference.png | Bin 0 -> 1049 bytes ..._solara-flask-chromium-linux-reference.png | Bin 0 -> 1672 bytes ...lette-chromium-linux-changed-reference.png | Bin 0 -> 1049 bytes ...ara-starlette-chromium-linux-reference.png | Bin 0 -> 1672 bytes 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/integration/latex_test.py create mode 100644 tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-flask-chromium-linux-changed-reference.png create mode 100644 tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-flask-chromium-linux-reference.png create mode 100644 tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-starlette-chromium-linux-changed-reference.png create mode 100644 tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-starlette-chromium-linux-reference.png diff --git a/packages/solara-widget-manager/src/mathjax.ts b/packages/solara-widget-manager/src/mathjax.ts index c35b1b46d..24464ac6c 100644 --- a/packages/solara-widget-manager/src/mathjax.ts +++ b/packages/solara-widget-manager/src/mathjax.ts @@ -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({ @@ -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() + } + } +} diff --git a/tests/integration/latex_test.py b/tests/integration/latex_test.py new file mode 100644 index 000000000..7596a6514 --- /dev/null +++ b/tests/integration/latex_test.py @@ -0,0 +1,20 @@ +import ipywidgets as widgets +import playwright.sync_api +from IPython.display import display + + +def test_widget_button_solara(solara_test, page_session: playwright.sync_api.Page, assert_solara_snapshot): + slider = widgets.FloatSlider(description=r"$E \sim mc^2$") + slider.add_class("test-class") + display(slider) + + page_session.locator(".test-class >> .mjx-c1D438").wait_for() + page_session.evaluate("document.fonts.ready") + page_session.wait_for_timeout(100) + assert_solara_snapshot(page_session.locator(".test-class").screenshot()) + slider.description = r"$\alpha$" + slider.add_class("test-changed-class") + page_session.locator(".test-changed-class >> .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").screenshot(), postfix="-changed") diff --git a/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-flask-chromium-linux-changed-reference.png b/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-flask-chromium-linux-changed-reference.png new file mode 100644 index 0000000000000000000000000000000000000000..8aeb7874524a5b2e38d67ce78fd19bba6d7b7760 GIT binary patch literal 1049 zcmV+!1m^pRP)Px&(@8`@RCt{2+`msEZ4d|WuVSL%hsJ`il1M}gM1|7WnV`mk)`Y^uPAe1r4=ikq zmNxnyXk%ewVS>@JHk(cN8pawal}f*j&X`Ollu9MdaawaU1HWO_ zkWQyzGMUiV*H^vfa=9=zHrB?f82f*^ySq^?mw&mYsw#SVdNjvr&Cv|}hE;>rYK5w* zC>DzVfO5Hv^Ye4WVlmx$eh!H0k8$StgIiL#L;wL`1Z=w@16XyX5hBD4We{&LfKAU!9&L5(yGTQTIB=8VLr2w7I$Y z%L!j9l_Dadi;D|V6s2KYy?f2X4XcjB!$S%L0<^rmOa})CR4f+B>-Eyk&W`3Bs;W{r z9M=8pv$Hd*R4TgHG1f>t9;Z+!L=zJeG&3_pCnqOFMD+RjNmEl(ba{DMH?GmWX5z7i z7LX(fFE20f`~8~YqtPe?L4d>IXmVwY84GWcw16N8u-om3Mxza5qtPfF4hJ_Y#{Pic zuv$o#W!&E0Vt9BM7K^1ix{%N3Q79A;kH;}SKF-aGu^PBxwOFN6ss6pp&r=qg&4%9I zUJMNlp`)Xt%@s2C@8X8V7&8ps!(xmX1~)9mm|<|kVvHFEH!Q}OVQ|A@j2Xrcu?g^c TOt>Ic00000NkvXXu0mjf3?A>Z literal 0 HcmV?d00001 diff --git a/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-flask-chromium-linux-reference.png b/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-flask-chromium-linux-reference.png new file mode 100644 index 0000000000000000000000000000000000000000..237b37926a66daa7a92e50ed6d5616f83ffa24b9 GIT binary patch literal 1672 zcmV;326y?1P)Px*LPu`!raAxYEYI1d&wC+`e0_Cixiekw z?*qHp`8|IW&Uepqcs7nP#y|)mIbg+7Pk<0Yl%l{QgyakgEJ8@mpui%8p}#9HElxv8WdP*m z<2cB&3|W>D2n0}ET#TZkBCJ?JyL3Y4qe@^U5(%`mwV|V<1MzqqyLRnLUK<}DM_E}J zE?&H-#?V>O)zyWHiV9e*)|9oPC_)rP*zNW?t0=u*54~OwK@boMg%F8EP*_+S}jhWJ`KCwj(*s^ z*o=7p{ypBjd4u!k&qJ$ScyY=Y!|?DhOeRyx+8Gv$1tTLP6j(%6UauGB<>h$$_AMG3 z8gTaPSquyeq*bT1k4)p#{A$jL6DOe4>CoQZzNmgANkSkHfZ1$@$z+;yJroMTVzJD6 z@6)GGxNzYDL{Y@5Rjc6l`vCx_PMyNQzyNxBdeGO`hvUbOqoJW;R@+yvUg6H2J6OAR zEuzsV{C+xJ9x{?9pw!(nh72cyxL))sGUYz&D+0)imW){tb} zw{PE2RaJ%F-d^bSdH{gO;lB)u^bbKs+AD!-o&?vOG^O&mo8m`Mx#M@ zcemodRgxqW78WY5pOKfBhhQ*>-rinXY5fIwp2zz2>lHok%!Xhv2tg2%K?49*s};du zP;s5oKGdLkV154l8TIw`0H#+xE|*Jj+jsBYp{J(@w{G3Sh7B9g-`|gC&z|ANjT^Xp z`7$_;LuF;eJZI&u(P$vcvf?_WeW*e8z;ZYoXl`x> z0Qh{qwAwnIPTae9FZs{0*=*RmcQ2Znnh*+wU@#b9G#VE@$KQ9w;c$3Pp9KX4iyniJ za23D&zBVqM# z7K;UggM*9ihv#|3Vlkxqh)|YgBoc|VS7u~+DkvyGNlD34Pa|pd>eUz<8$%=#Nxt{} z`**Bavqo{9(mvF%BnL;^+uL#U=uyQ7FSF_J;lpTcZN-x(Pf%T5{m1?2bUMW2@wD3| z5(${iW^CNJkxsvpjI-Hn*s^5{u3o(guh)x@A3vhIyBjSnEdYSmuV2IIbS_wDwOSYK zLsd)#@pzowym^yVR#q~X%f;&I>Sn!ME*Bdb8d78EB){L!JRT3Ls;XkopFd}TK!DZP z*R#!=H?xk84mLhM&aPd%#!5;`SW8O_lVv&C_Qi`A?BKzJ%grmsF4Z+Psd}v8|1KJhA`*$fY&I)8i#0en2%Szxr`-ve2i+AaG8han7!3IN^Cu=J zCl{;@g+dfogrq<#tSpE|qX>mUFdB`?UugLK%g^C(7-qAX!ita-D6q0))}M# literal 0 HcmV?d00001 diff --git a/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-starlette-chromium-linux-changed-reference.png b/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-starlette-chromium-linux-changed-reference.png new file mode 100644 index 0000000000000000000000000000000000000000..8aeb7874524a5b2e38d67ce78fd19bba6d7b7760 GIT binary patch literal 1049 zcmV+!1m^pRP)Px&(@8`@RCt{2+`msEZ4d|WuVSL%hsJ`il1M}gM1|7WnV`mk)`Y^uPAe1r4=ikq zmNxnyXk%ewVS>@JHk(cN8pawal}f*j&X`Ollu9MdaawaU1HWO_ zkWQyzGMUiV*H^vfa=9=zHrB?f82f*^ySq^?mw&mYsw#SVdNjvr&Cv|}hE;>rYK5w* zC>DzVfO5Hv^Ye4WVlmx$eh!H0k8$StgIiL#L;wL`1Z=w@16XyX5hBD4We{&LfKAU!9&L5(yGTQTIB=8VLr2w7I$Y z%L!j9l_Dadi;D|V6s2KYy?f2X4XcjB!$S%L0<^rmOa})CR4f+B>-Eyk&W`3Bs;W{r z9M=8pv$Hd*R4TgHG1f>t9;Z+!L=zJeG&3_pCnqOFMD+RjNmEl(ba{DMH?GmWX5z7i z7LX(fFE20f`~8~YqtPe?L4d>IXmVwY84GWcw16N8u-om3Mxza5qtPfF4hJ_Y#{Pic zuv$o#W!&E0Vt9BM7K^1ix{%N3Q79A;kH;}SKF-aGu^PBxwOFN6ss6pp&r=qg&4%9I zUJMNlp`)Xt%@s2C@8X8V7&8ps!(xmX1~)9mm|<|kVvHFEH!Q}OVQ|A@j2Xrcu?g^c TOt>Ic00000NkvXXu0mjf3?A>Z literal 0 HcmV?d00001 diff --git a/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-starlette-chromium-linux-reference.png b/tests/ui/snapshots/tests/integration/latex_test.py/test_widget_button_solara-starlette-chromium-linux-reference.png new file mode 100644 index 0000000000000000000000000000000000000000..535adf126ffedc8834bd69cb167458283d142fa4 GIT binary patch literal 1672 zcmV;326y?1P)Px*LP`J1typXPxpomHkGHi6wMgItj5GtaO3PI^2n#QnV)0}^HmOIj<;sgE)wR1D=UNDZcke)2m%B_fYa%mwTdzr3@{iBFq_SYL?VdAVkjsmpwJ>@ z4yxQ4TCEnRPoIX<=|o9M30hiOf{u<3G&VM-t&PQE5Cj2Mt2OPG z&t|hBkw_pGi(ztdakQCeDxw{PE~ zzP=u3&z?npe}6`GO8Z!BoSI+FIdS3y^m;wo+S(S>Pa=^(C=`O#YK6sOnRPu9iNI#F z&3NzAr%$+W;Q|Cf!0Oej5exCPARat;fYQ=ZIGs+p&hPib>-GNcoT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