{"id":421382,"date":"2024-06-30T04:03:47","date_gmt":"2024-06-30T04:03:47","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=421382"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=421382","title":{"rendered":"<span>Jupyter for .NET. \u00abLike Python\u00bb<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">A few months ago Microsoft <a href=\"https:\/\/habr.com\/ru\/company\/microsoft\/blog\/487532\/\">announced<\/a> 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 <code>Entity<\/code> class from a symbolic algebra library:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/c5e\/234\/adb\/c5e234adb1eaaae6bdcd219b2adeab3a.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/c5e\/234\/adb\/c5e234adb1eaaae6bdcd219b2adeab3a.png\"\/><\/p>\n<p>  Looks awesome. Is simple. Very enjoyable. Let&#8217;s see more!<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h3>About <a href=\"https:\/\/jupyter.org\" rel=\"nofollow\">Jupyter<\/a><\/h3>\n<p>  It&#8217;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&#8217; values. It&#8217;s very convenient for research and simple everyday scripts for whatever needs.<\/p>\n<h3>About <a href=\"https:\/\/github.com\/dotnet\/interactive\" rel=\"nofollow\">dotnet\/interactive<\/a><\/h3>\n<p>  This is what allows us to use .NET inside Jupyter. You can literally write like this<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/_6\/in\/2u\/_6in2uarxs-i66hxpspnm-eq9aa.png\" data-src=\"https:\/\/habrastorage.org\/webt\/_6\/in\/2u\/_6in2uarxs-i66hxpspnm-eq9aa.png\"\/><br \/>  and see the results immediately, without rerunning the whole code.<\/p>\n<p>  There are some features without any additional overhead:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/0p\/b4\/3h\/0pb43houk89l05vi-fcmcmf6ook.png\" data-src=\"https:\/\/habrastorage.org\/webt\/0p\/b4\/3h\/0pb43houk89l05vi-fcmcmf6ook.png\"\/><\/p>\n<h3>About <a href=\"https:\/\/github.com\/asc-community\/AngouriMath\" rel=\"nofollow\">AngouriMath<\/a><\/h3>\n<p>  It&#8217;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&#8217;s still inconvenient.<\/p>\n<p>  All mathematical expressions in this library inherit from <code>Entity<\/code>, which has method <code>Latexise<\/code> of the <code>string<\/code>. We only need render this LaTeX.<\/p>\n<h3>Injecting LaTeX rendering<\/h3>\n<p>  We can register our own, custom output for any type. That is how I did it:<\/p>\n<pre><code class=\"plaintext\">let magic() =     let register (value : ILatexiseable) = $@\"             &lt;script src='https:\/\/polyfill.io\/v3\/polyfill.min.js?features=es6'>&lt;\/script>             &lt;script id='MathJax-script' async src='https:\/\/cdn.jsdelivr.net\/npm\/mathjax@3\/es5\/tex-mml-chtml.js'>&lt;\/script>             \\[{value.Latexise()}\\]             \"      Formatter.Register&lt;ILatexiseable>(register, \"text\/html\") <\/code><\/pre>\n<p>  That is, we register type <code>ILatexiseable<\/code> to be printed in html which is generated by our function. I use <a href=\"https:\/\/www.mathjax.org\" rel=\"nofollow\">MathJax<\/a>, a well-known LaTeX rendering library.<\/p>\n<p>  Now all classes inhereting from <code>ILatexiseable<\/code> will be rendered like this:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/q1\/v_\/sy\/q1v_syu-o6av_85m1byrh5ly-q0.png\" data-src=\"https:\/\/habrastorage.org\/webt\/q1\/v_\/sy\/q1v_syu-o6av_85m1byrh5ly-q0.png\"\/><\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">Explanation for every block<\/b>                         <\/p>\n<div class=\"spoiler_text\">1. In the first block we call an extension method <code>ToEntity()<\/code>, which parses the expression<br \/>  2. In the second block we create a system of equations and immediately print out it<br \/>  3. In the third block we create a matrix and immediately print out it  <\/div>\n<\/p><\/div>\n<p>  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:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/o3\/pa\/bs\/o3pabsbr1ptojopfgd3vpsspa6q.png\" data-src=\"https:\/\/habrastorage.org\/webt\/o3\/pa\/bs\/o3pabsbr1ptojopfgd3vpsspa6q.png\"\/><\/p>\n<h3>Further ideas<\/h3>\n<p>  I&#8217;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.<\/p>\n<p>  Now I think of creating something lke <code>Entity.Plot()<\/code>, which would plot a function&#8217;s graph. Imagine you write something like  <\/p>\n<pre><code class=\"plaintext\">plot \"x\" 10 12 \"x2 + sqrt(x) + e^x\" <\/code><\/pre>\n<p>  and get the graph immediately. In my opinion, it can be a useful feature for simple use-cases.<\/p>\n<p>  If you want to try it without installation, try this: <a href=\"https:\/\/mybinder.org\/v2\/gh\/asc-community\/Try\/main\" rel=\"nofollow\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/117\/793\/ab7\/117793ab76524046ef44e2d2d5af220c.svg\"\/><\/a> (for some reason, it&#8217;s quite slow, so you will have to wait)<\/p>\n<p>  Thank you for your attention! Hope you liked this mini-article.<\/p>\n<h4>References<\/h4>\n<p>  1. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Project_Jupyter\" rel=\"nofollow\">Jupyter<\/a> \u2014 Interactive IDE for research &amp; creating notebooks<br \/>  2. <a href=\"https:\/\/github.com\/dotnet\/interactive\" rel=\"nofollow\">.NET Interactive<\/a> \u2014 Wrapper for .NET allowing to use .NET in Jupyter<br \/>  3. <a href=\"https:\/\/github.com\/asc-community\/AngouriMath\" rel=\"nofollow\">AngouriMath<\/a> \u2014 Computer algebra library, for which I wrote a <a href=\"https:\/\/github.com\/asc-community\/AngouriMath#jupyter-integration\" rel=\"nofollow\">wrapper<\/a> for LaTeX<br \/>  4. <a href=\"https:\/\/mybinder.org\/v2\/gh\/asc-community\/Try\/main\" rel=\"nofollow\">MyBinder<\/a> \u2014 Demo to play<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/528816\/\"> https:\/\/habr.com\/ru\/articles\/528816\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">A few months ago Microsoft <a href=\"https:\/\/habr.com\/ru\/company\/microsoft\/blog\/487532\/\">announced<\/a> 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 <code>Entity<\/code> class from a symbolic algebra library:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/c5e\/234\/adb\/c5e234adb1eaaae6bdcd219b2adeab3a.png\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/c5e\/234\/adb\/c5e234adb1eaaae6bdcd219b2adeab3a.png\"\/><\/p>\n<p>  Looks awesome. Is simple. Very enjoyable. Let&#8217;s see more!  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-421382","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/421382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=421382"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/421382\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=421382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=421382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=421382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}