Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: add mathjax lib using useScript (#2839)

Shahmir Varqha53744aa1e67c
diff --git a/frontend/src/hooks/useScript.ts b/frontend/src/hooks/useScript.ts
new file mode 100644
index 0000000000..1ce1845f6b
--- /dev/null
+++ b/frontend/src/hooks/useScript.ts
@@ -0,0 +1,91 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import React from "react";
+
+type ScriptStatus = "loading" | "ready" | "error" | "unknown";
+interface ScriptOptions {
+ removeOnUnmount?: boolean;
+}
+
+const defaultOptions = { removeOnUnmount: false };
+
+export function useScript(
+ src: string,
+ options: ScriptOptions = defaultOptions,
+) {
+ const [status, setStatus] = React.useState<ScriptStatus>(() => {
+ const existingScript = document.querySelector<HTMLScriptElement>(
+ `script[src="${src}"]`,
+ );
+ return (existingScript?.dataset.status as ScriptStatus) || "unknown";
+ });
+
+ React.useEffect(() => {
+ let script = document.querySelector<HTMLScriptElement>(
+ `script[src="${src}"]`,
+ );
+
+ const updateStatus = (newStatus: ScriptStatus) => {
+ if (script) {
+ script.dataset.status = newStatus;
+ setStatus(newStatus);
+ }
+ };
+
+ const handleScriptLoad = () => updateStatus("ready");
+ const handleScriptError = () => updateStatus("error");
+
+ // observes DOM changes and updates the status
+ const observeScript = (script: HTMLScriptElement) => {
+ const observer = new MutationObserver(() => {
+ const newStatus = script.dataset.status;
+ if (newStatus) {
+ setStatus(newStatus as ScriptStatus);
+ }
+ });
+ observer.observe(script, {
+ attributes: true,
+ attributeFilter: ["data-status"],
+ });
+ return observer;
+ };
+
+ if (!script) {
+ script = document.createElement("script");
+ script.src = src;
+ script.async = true;
+ updateStatus("loading");
+ document.body.append(script);
+
+ script.addEventListener("load", handleScriptLoad);
+ script.addEventListener("error", handleScriptError);
+
+ const observer = observeScript(script);
+
+ return () => {
+ observer.disconnect();
+ if (script) {
+ script.removeEventListener("load", handleScriptLoad);
+ script.removeEventListener("error", handleScriptError);
+
+ if (options.removeOnUnmount) {
+ script.remove();
+ }
+ }
+ };
+ }
+
+ const domStatus = script.dataset.status;
+ if (domStatus) {
+ if (domStatus === "ready" || domStatus === "error") {
+ return;
+ }
+ const observer = observeScript(script);
+ return () => observer.disconnect();
+ }
+
+ setStatus("unknown");
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [src]);
+
+ return status;
+}
diff --git a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
index 47d00bfc9f..f1af33b4d5 100644
--- a/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
+++ b/frontend/src/plugins/impl/plotly/PlotlyPlugin.tsx
@@ -16,6 +16,7 @@ import { isEqual, pick, set } from "lodash-es";
import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
import { usePrevious } from "@uidotdev/usehooks";
import { Arrays } from "@/utils/arrays";
+import { useScript } from "@/hooks/useScript";
interface Data {
figure: Figure;
@@ -109,6 +110,12 @@ export const PlotlyComponent = memo(
return structuredClone(originalFigure);
});
+ // Used for rendering LaTeX. TODO: Serve this library from Marimo
+ const scriptStatus = useScript(
+ "https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-MML-AM_CHTML",
+ );
+ const isScriptLoaded = scriptStatus === "ready";
+
useEffect(() => {
const nextFigure = structuredClone(originalFigure);
setFigure(nextFigure);
@@ -117,7 +124,7 @@ export const PlotlyComponent = memo(
...value,
});
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [originalFigure]);
+ }, [originalFigure, isScriptLoaded]);
const [layout, setLayout] = useState<Partial<Plotly.Layout>>(() => {
return {