-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_label.py
85 lines (66 loc) · 2.6 KB
/
latex_label.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
from bokeh.models import Label
from bokeh.util.compiler import TypeScript
TS_CODE = """
import * as p from "core/properties"
import {Label, LabelView} from "models/annotations/label"
declare const katex: any
export class LatexLabelView extends LabelView {
model: LatexLabel
render(): void {
//--- Start of copied section from ``Label.render`` implementation
// Here because AngleSpec does units tranform and label doesn't support specs
let angle: number
switch (this.model.angle_units) {
case "rad": {
angle = -this.model.angle
break
}
case "deg": {
angle = (-this.model.angle * Math.PI) / 180.0
break
}
default:
throw new Error("unreachable code")
}
const panel = this.layout ?? this.plot_view.layout.center_panel
let sx = this.model.x_units == "data" ? this.coordinates.x_scale.compute(this.model.x) : panel.xview.compute(this.model.x)
let sy = this.model.y_units == "data" ? this.coordinates.y_scale.compute(this.model.y) : panel.yview.compute(this.model.y)
sx += this.model.x_offset
sy -= this.model.y_offset
//--- End of copied section from ``Label.render`` implementation
// Must render as superpositioned div (not on canvas) so that KaTex
// css can properly style the text
this._css_text(this.layer.ctx, "", sx, sy, angle)
// ``katex`` is loaded into the global window at runtime
// katex.renderToString returns a html ``span`` element
katex.render(this.model.text, this.el, {displayMode: true})
}
}
export namespace LatexLabel {
export type Attrs = p.AttrsOf<Props>
export type Props = Label.Props
}
export interface LatexLabel extends LatexLabel.Attrs {}
export class LatexLabel extends Label {
properties: LatexLabel.Props
__view_type__: LatexLabelView
constructor(attrs?: Partial<LatexLabel.Attrs>) {
super(attrs)
}
static init_LatexLabel() {
this.prototype.default_view = LatexLabelView
}
}
"""
class LatexLabel(Label):
"""A subclass of the Bokeh built-in `Label` that supports rendering
LaTex using the KaTex typesetting library.
Only the render method of LabelView is overloaded to perform the
text -> latex (via katex) conversion. Note: ``render_mode="canvas``
isn't supported and certain DOM manipulation happens in the Label
superclass implementation that requires explicitly setting
`render_mode='css'`).
"""
__javascript__ = ["https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.js"]
__css__ = ["https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.6.0/katex.min.css"]
__implementation__ = TypeScript(TS_CODE)