support latex better
This commit is contained in:
+2
-2
@@ -11,7 +11,7 @@ from uuid import uuid4
|
|||||||
|
|
||||||
from ipython_shell.jupyter import InputRequest, JupyterApp, ParsedJupyterMessage
|
from ipython_shell.jupyter import InputRequest, JupyterApp, ParsedJupyterMessage
|
||||||
|
|
||||||
from st_demo.ui import event_to_record, preferred_mime
|
from st_demo.ui import event_to_record, latex_body, preferred_mime
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -174,7 +174,7 @@ def _render_mime(data: dict[str, object]) -> None:
|
|||||||
elif mime == "application/javascript":
|
elif mime == "application/javascript":
|
||||||
st.code(str(value), language="javascript")
|
st.code(str(value), language="javascript")
|
||||||
elif mime == "text/latex":
|
elif mime == "text/latex":
|
||||||
st.latex(str(value))
|
st.latex(latex_body(value))
|
||||||
else:
|
else:
|
||||||
st.code(str(value), language="text")
|
st.code(str(value), language="text")
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,21 @@ def preferred_mime(data: Mapping[str, object]) -> tuple[str, object] | None:
|
|||||||
return next(iter(data.items()), None)
|
return next(iter(data.items()), None)
|
||||||
|
|
||||||
|
|
||||||
|
def latex_body(value: object) -> str:
|
||||||
|
"""Adapt common Jupyter LaTeX payloads to Streamlit's ``st.latex`` API.
|
||||||
|
|
||||||
|
IPython's LaTeX formatter commonly returns ``$...$`` while Streamlit
|
||||||
|
supplies its own delimiters around the value. Remove only the outer
|
||||||
|
delimiters so they are not rendered as literal dollar signs.
|
||||||
|
"""
|
||||||
|
body = str(value).strip()
|
||||||
|
delimiters = (("$$", "$$"), ("$", "$"), (r"\[", r"\]"), (r"\(", r"\)"))
|
||||||
|
for opening, closing in delimiters:
|
||||||
|
if body.startswith(opening) and body.endswith(closing):
|
||||||
|
return body[len(opening) : -len(closing)].strip()
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
def _content_record(content: object) -> dict[str, object]:
|
def _content_record(content: object) -> dict[str, object]:
|
||||||
if isinstance(content, BaseModel):
|
if isinstance(content, BaseModel):
|
||||||
return content.model_dump(mode="json")
|
return content.model_dump(mode="json")
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ from fastapi.testclient import TestClient
|
|||||||
from ipython_mcp.app import app as mcp_app
|
from ipython_mcp.app import app as mcp_app
|
||||||
from ipython_shell import App, AppInfo
|
from ipython_shell import App, AppInfo
|
||||||
from ipython_webapp.app import app as web_app
|
from ipython_webapp.app import app as web_app
|
||||||
from langchain_demo.ipython_wrapper import get_app_info
|
from ipython_agent.ipython_wrapper import get_app_info
|
||||||
|
|
||||||
|
|
||||||
class AppInfoTests(unittest.IsolatedAsyncioTestCase):
|
class AppInfoTests(unittest.IsolatedAsyncioTestCase):
|
||||||
|
|||||||
+19
-1
@@ -8,7 +8,7 @@ from ipython_shell.jupyter.messages import (
|
|||||||
UnknownJupyterMessage,
|
UnknownJupyterMessage,
|
||||||
)
|
)
|
||||||
from st_demo.app import visible_records
|
from st_demo.app import visible_records
|
||||||
from st_demo.ui import event_to_record, preferred_mime
|
from st_demo.ui import event_to_record, latex_body, preferred_mime
|
||||||
|
|
||||||
|
|
||||||
class UIDisplayTests(unittest.TestCase):
|
class UIDisplayTests(unittest.TestCase):
|
||||||
@@ -44,6 +44,24 @@ class UIDisplayTests(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual((mime, value), ("text/html", "<b>rich</b>"))
|
self.assertEqual((mime, value), ("text/html", "<b>rich</b>"))
|
||||||
|
|
||||||
|
def test_preferred_mime_chooses_latex_before_plain_text(self):
|
||||||
|
latex = r"\\int_0^1 x^2\\,dx = \\frac{1}{3}"
|
||||||
|
|
||||||
|
mime, value = preferred_mime(
|
||||||
|
{
|
||||||
|
"text/plain": "integral",
|
||||||
|
"text/latex": latex,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual((mime, value), ("text/latex", latex))
|
||||||
|
|
||||||
|
def test_latex_body_unwraps_ipython_math_delimiters(self):
|
||||||
|
self.assertEqual(
|
||||||
|
latex_body(r"$\displaystyle x^{2} - x - 6$"),
|
||||||
|
r"\displaystyle x^{2} - x - 6",
|
||||||
|
)
|
||||||
|
|
||||||
def test_execute_result_record_keeps_mime_and_repr(self):
|
def test_execute_result_record_keeps_mime_and_repr(self):
|
||||||
event = ExecuteResultMessage(
|
event = ExecuteResultMessage(
|
||||||
message_id="message-1",
|
message_id="message-1",
|
||||||
|
|||||||
Reference in New Issue
Block a user