Pull to refresh

Jupyter for .NET. «Like Python»

Reading time 2 min
Views 2K
Original author: WhiteBlackGoose
A few months ago Microsoft announced about the creation of Jupyter for .NET. However, people are barely interested in it despite how attractive the topic is. I decided to make a LaTeX wrapper for the Entity class from a symbolic algebra library:



Looks awesome. Is simple. Very enjoyable. Let's see more!

About Jupyter


It's some kind of IDE for interactive notebooks. Instead of execucting the whole code, you can interact with it by executing some parts of it, preserving the variables' values. It's very convenient for research and simple everyday scripts for whatever needs.

About dotnet/interactive


This is what allows us to use .NET inside Jupyter. You can literally write like this

and see the results immediately, without rerunning the whole code.

There are some features without any additional overhead:


About AngouriMath


It's a symbolic algebra library for .NET for working with mathematical expressions. One surely could work with them in-line, like we normally work with them in the code, but it's still inconvenient.

All mathematical expressions in this library inherit from Entity, which has method Latexise of the string. We only need render this LaTeX.

Injecting LaTeX rendering


We can register our own, custom output for any type. That is how I did it:

let magic() =
    let register (value : ILatexiseable) = $@"
            <script src='https://polyfill.io/v3/polyfill.min.js?features=es6'></script>
            <script id='MathJax-script' async src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'></script>
            \[{value.Latexise()}\]
            "

    Formatter.Register<ILatexiseable>(register, "text/html")


That is, we register type ILatexiseable to be printed in html which is generated by our function. I use MathJax, a well-known LaTeX rendering library.

Now all classes inhereting from ILatexiseable will be rendered like this:



Explanation for every block
1. In the first block we call an extension method ToEntity(), which parses the expression
2. In the second block we create a system of equations and immediately print out it
3. In the third block we create a matrix and immediately print out it


As long as Jupyter is more about small pieces of code without following any OOP or other industrial principles, F# is best suited for it. That is why it also supported, let me show an example of a school-level equation, solved via this triplet of technologies:



Further ideas


I'm a big fan of .NET, but I also love Jupyter. That is why the creation of Interactive was an amazing event for me, so I made a wrapper supporting Interactive for AngouriMath for LaTeX output.

Now I think of creating something lke Entity.Plot(), which would plot a function's graph. Imagine you write something like
plot "x" 10 12 "x2 + sqrt(x) + e^x"

and get the graph immediately. In my opinion, it can be a useful feature for simple use-cases.

If you want to try it without installation, try this: (for some reason, it's quite slow, so you will have to wait)

Thank you for your attention! Hope you liked this mini-article.

References


1. Jupyter — Interactive IDE for research & creating notebooks
2. .NET Interactive — Wrapper for .NET allowing to use .NET in Jupyter
3. AngouriMath — Computer algebra library, for which I wrote a wrapper for LaTeX
4. MyBinder — Demo to play
Tags:
Hubs:
+2
Comments 0
Comments Leave a comment

Articles