-
-
Notifications
You must be signed in to change notification settings - Fork 471
/
equation.py
92 lines (72 loc) · 2.78 KB
/
equation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Renders objects representing equations including LaTeX strings and
SymPy objects.
"""
from __future__ import annotations
import sys
from typing import Any, ClassVar, Mapping
import param
from pyviz_comms import JupyterComm
from ..util import lazy_load
from .markup import DivPaneBase
def is_sympy_expr(obj):
"""Test for sympy.Expr types without usually needing to import sympy"""
if 'sympy' in sys.modules and 'sympy' in str(type(obj).__class__):
import sympy
if isinstance(obj, sympy.Expr):
return True
return False
class LaTeX(DivPaneBase):
"""
The `LaTeX` pane allows rendering LaTeX equations. It uses either
`MathJax` or `KaTeX` depending on the defined renderer.
By default it will use the renderer loaded in the extension
(e.g. `pn.extension('katex')`), defaulting to `KaTeX`.
Reference: https://panel.holoviz.org/reference/panes/LaTeX.html
:Example:
>>> pn.extension('katex')
>>> LaTeX(
... 'The LaTeX pane supports two delimiters: $LaTeX$ and \(LaTeX\)',
... style={'font-size': '18pt'}, width=800
... )
"""
renderer = param.ObjectSelector(default=None, allow_None=True,
objects=['katex', 'mathjax'], doc="""
The JS renderer used to render the LaTeX expression.""")
# Priority is dependent on the data type
priority: ClassVar[float | bool | None] = None
_rename: ClassVar[Mapping[str, str | None]] = {"renderer": None}
@classmethod
def applies(cls, obj: Any) -> float | bool | None:
if is_sympy_expr(obj) or hasattr(obj, '_repr_latex_'):
return 0.05
elif isinstance(obj, str):
return None
else:
return False
def _get_model_type(self, root, comm):
module = self.renderer
if module is None:
if 'panel.models.mathjax' in sys.modules and 'panel.models.katex' not in sys.modules:
module = 'mathjax'
else:
module = 'katex'
model = 'KaTeX' if module == 'katex' else 'MathJax'
return lazy_load(f'panel.models.{module}', model, isinstance(comm, JupyterComm), root)
def _get_model(self, doc, root=None, parent=None, comm=None):
model = self._get_model_type(root, comm)(**self._get_properties())
if root is None:
root = model
self._models[root.ref['id']] = (model, parent)
return model
def _get_properties(self):
properties = super()._get_properties()
obj = self.object
if obj is None:
obj = ''
elif hasattr(obj, '_repr_latex_'):
obj = obj._repr_latex_()
elif is_sympy_expr(obj):
import sympy
obj = r'$'+sympy.latex(obj)+'$'
return dict(properties, text=obj)