Repository navigation
Expand file tree
/
Copy pathKernelExtension.fs
More file actions
51 lines (40 loc) · 2.01 KB
/
Copy pathKernelExtension.fs
File metadata and controls
51 lines (40 loc) · 2.01 KB
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
namespace AngouriMath.InteractiveExtension
open Microsoft.DotNet.Interactive
open Microsoft.DotNet.Interactive.Formatting
open AngouriMath.Core
open System.Threading.Tasks
open PeterO.Numbers
open System
open Plotly.NET
open type Plotly.NET.GenericChart
open AngouriMath.FSharp.Functions
type KernelExtension() =
static member public applyMagic () =
let registerLatexRendering (latexizer : 'a -> string) =
// register text/latex
(fun o -> $"$${latexizer o}$$")
|> (fun f -> new Func<'a, string>(f))
|> (fun f -> Formatter.Register<'a>(f, "text/latex"))
// register text/html
(fun o -> $@"
<script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
\[{latexizer o}\]")
|> (fun f -> new Func<'a, string>(f))
|> (fun f -> Formatter.Register<'a>(f, "text/html"))
// if possible, use text/latex
Formatter.SetPreferredMimeTypesFor(typeof<'a>, "text/latex")
Formatter.SetPreferredMimeTypesFor(typeof<EDecimal>, "text/plain")
Formatter.Register<EDecimal>(new Func<EDecimal, string>(fun o -> o.ToString()), "text/plain")
Formatter.SetPreferredMimeTypesFor(typeof<EInteger>, "text/plain")
Formatter.Register<EInteger>(new Func<EInteger, string>(fun o -> o.ToString()), "text/plain")
registerLatexRendering (fun (o : ILatexizeable) -> latex o)
registerLatexRendering (fun (o : ERational) -> $@"\frac{{{o.Numerator}}}{{{o.Denominator}}}")
Formatter.SetPreferredMimeTypesFor(typeof<GenericChart>, "text/html")
Formatter.Register<GenericChart> (toChartHTML, "text/html")
interface IKernelExtension with
member _.OnLoadAsync _ =
KernelExtension.applyMagic()
// let message = "LaTeX renderer binded. Enjoy!"
// KernelInvocationContext.Current.Display(message, "text/html") |> ignore
printfn $"LaTeX renderer binded. Enjoy!"
Task.CompletedTask