From 84e0ff6244c21214809aa58d0e5c0f4a0e5a6c63 Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 20 Jan 2020 11:05:59 -0700 Subject: [PATCH] gave latex its own file --- LatexLabel.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ ae.py | 88 ++------------------------------------------------- 2 files changed, 89 insertions(+), 86 deletions(-) create mode 100644 LatexLabel.py diff --git a/LatexLabel.py b/LatexLabel.py new file mode 100644 index 0000000..29f2a49 --- /dev/null +++ b/LatexLabel.py @@ -0,0 +1,87 @@ +from bokeh.util.compiler import TypeScript +from bokeh.models import Label + +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.panel != null ? this.panel : this.plot_view.frame + + const xscale = this.plot_view.frame.xscales[this.model.x_range_name] + const yscale = this.plot_view.frame.yscales[this.model.y_range_name] + + let sx = this.model.x_units == "data" ? xscale.compute(this.model.x) : panel.xview.compute(this.model.x) + let sy = this.model.y_units == "data" ? yscale.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.plot_view.canvas_view.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 + + export type Props = Label.Props +} + +export interface LatexLabel extends LatexLabel.Attrs {} + +export class LatexLabel extends Label { + properties: LatexLabel.Props + + constructor(attrs?: Partial) { + 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) diff --git a/ae.py b/ae.py index 2a2e040..39a9ef5 100644 --- a/ae.py +++ b/ae.py @@ -1,93 +1,9 @@ from math import sqrt -from bokeh.models import Arrow, Label +from bokeh.models import Arrow from bokeh.plotting import figure, output_file, show -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.panel != null ? this.panel : this.plot_view.frame - - const xscale = this.plot_view.frame.xscales[this.model.x_range_name] - const yscale = this.plot_view.frame.yscales[this.model.y_range_name] - - let sx = this.model.x_units == "data" ? xscale.compute(this.model.x) : panel.xview.compute(this.model.x) - let sy = this.model.y_units == "data" ? yscale.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.plot_view.canvas_view.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 - - export type Props = Label.Props -} - -export interface LatexLabel extends LatexLabel.Attrs {} - -export class LatexLabel extends Label { - properties: LatexLabel.Props - - constructor(attrs?: Partial) { - 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) +from LatexLabel import LatexLabel class PlaneStress: