我正在尝试在标签集的 jupyter notebook 中复制https://docs.bokeh.org/en/latest/docs/user_guide/extensions_gallery/latex.html#userguide-extensions-examples-latex中提到的散景乳胶示例。我可以看到 katex.min.js 正在从 Web 控制台加载。但是,当 LatexLabel 呈现时,它会声明katex not defined。Katex JS 文档说,一旦加载了 js,它就应该在全球范围内可用。
import * as p from "core/properties"
import {LabelSet, LabelSetView} from "models/annotations/label_set"
declare const katex: any
export class LatexLabelSetView extends LabelSetView {
model: LatexLabelSet
render(): void {
const draw = this._v_css_text.bind(this)
const {ctx} = this.plot_view.canvas_view
const [sx, sy] = this._map_data()
for (let i = 0, end = this._text.length; i < end; i++) {
try {
draw(ctx, i, this._text[i], sx[i] + this._x_offset[i], sy[i] - this._y_offset[i], this._angle[i])
katex.render(this._text[i], this.el, {displayMode: true})
}
catch(e) {
console.log( 'Error: ' + e);
}
}
}
}
class LatexLabelSet(LabelSet):
__javascript__ = ["https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js"]
__css__ = ["https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css"]
__implementation__ = TypeScript(TS_CODE)
还尝试将脚本元素添加到文档根目录中。虽然没有运气。
export class LatexLabelSet extends LabelSet {
properties: LatexLabelSet.Props
constructor(attrs?: Partial<LatexLabelSet.Attrs>) {
super(attrs)
}
static init_LatexLabelSet() {
let jsNode = document.createElement('script')
jsNode.id = 'bokeh-katex-js'
jsNode.src = "https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js"
let cssNode = document.createElement('link')
cssNode.id = 'bokeh-katex-css'
cssNode.rel= 'stylesheet'
cssNode.href = "https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css"
document.getElementsByClassName('bk-root')[0].appendChild(cssNode)
document.getElementsByClassName('bk-root')[0].appendChild(jsNode)
this.prototype.default_view = LatexLabelSetView
}
}
任何方向都会有所帮助。