Home .NET Working with notebook in VS Code with the “dotnet interactive” extension

Working with notebook in VS Code with the “dotnet interactive” extension

by admin
Working with notebook in VS Code with the "dotnet interactive" extension
Screenshot of the notebook from VS Code

Today I want to tell you about such a wonderful tool as "dotnet interactive". I’ll show you with my own example how and why I started using it, and briefly describe where to start.

Problem

My work involves the development of a program designed to evaluate the effectiveness of physical security equipment. This product contains in its core a simulation model, the result of which is a protocol file in which the events of the simulated experiment are described. These can be events such as "intruder caught in camera zone" or "response team is in pursuit of intruder" and other similar events that may occur in the simulation.

Various statistical metrics are counted from the protocol, which then go into the report. Now the formulas to read the statistics we need are scattered in various stages of TK, so when I learned yesterday about "dotnet-interactive" I immediately thought about creating a notebook in the format "Metrics description"-"Formula"-"Code"-"Graph", in which we can download any file in the protocol and interactively pass and read the metrics that interest us.

Now let’s start creating a notebook

First of all, you need to have the .net5 sdk installed and the latest version of VS Code. Next, you just need to install the ".NET Interactive Notebooks" extension. This extension has the status of "Preview" but you can already do many interesting things there.

When we installed the extension, we can create a working directory, where the necessary libraries, scripts and files will be located. Open it in VS Code and type in ".NET Interactive: Create new blank notebook" in the command window and start filling our notebook.

In the first block of code, I have defined the download of the :

#load "Load.fsx"open Loadlet Experiment = loadSep "2021.02.03_15.55.58_gen.sep"

Here I have plugged in a script in F# that encapsulates the file opening logic and xml-serialization :

#r "nuget: System.Text.Encoding.CodePages"#r "AKIM.Protocol.dll"open System.IOopen AKIM.Protocolopen System.Xml.Serializationopen System.Textlet loadSep path=let deserializeXml (xml : string) =let toBytes (x : string) = Encoding.UTF8.GetBytes xlet xmlSerializer = XmlSerializer(typeof<Experiment> )use stream = new MemoryStream(toBytes xml)xmlSerializer.Deserialize stream :?> ExperimentEncoding.RegisterProvider(CodePagesEncodingProvider.Instance)deserializeXml (File.ReadAllText(path, Encoding.GetEncoding(1251)))

In this script I plug in the nuget package for encoding and my library with dto-classes written in C#.

In the second block of notebook I already in c#-intaractive plug in namespaces needed to work with the experiment and shuffle the object in c# from the f# defined in the first block

#r "AKIM.Protocol.dll"using AKIM.Protocol;using AKIM.Protocol.Events;using AKIM.Protocol.Events.OperatorSvn;using AKIM.Protocol.Events.OperSb;using AKIM.Protocol.Events.RespUnits;using AKIM.Protocol.Events.Intruders;using AKIM.Protocol.Events.Sens;using AKIM.Protocol.Events.System;#!share --from fsharp Experiment

Actually, we can further describe and count the metrics we need, in any order, using the object of the experiment. For example, the following block outputs the total number of penetrations into the object and the number of intruder refusals

var allTests = Experiment.Tests.Count;var penetrations = Experiment.Tests.Where(t => t.Events.Last() is PenetrationEvent).Count();var nonPenetrations = Experiment.Tests.Where(t => t.Events.Last() is NonPenetrationEvent).Count();var eve = Experiment.Tests.First().Events.FirstOrDefault(t => t is VisContactEvent);Console.WriteLine(eve?.GetDescription());Console.WriteLine($"Number of penetrations {penetrations} of {allTests}")

Clicking to run the code execution we get the following output :

Working with notebook in VS Code with the "dotnet interactive" extension

And then I can use these values to make a nice chart or graph, like :

#r "nuget: XPlot.Plotly"#!share --from csharp penetrations#!share --from csharp nonPenetrations#!share --from csharp allTestsopen XPlot.PlotlyChart.Pie(seq {("Number of penetrations", penetrations);("Neutralized", allTests- penetrations-nonPenetrations);("Denied", nonPenetrations)}) |> Chart.Show

When executed, the graph opens in my browser, although I’ve seen it open at the bottom of the code block in some tutorials (upd: use XPlot.Plotly.Interactive. There are examples of plotting in c# and f# in the project repository (link below)).

Working with notebook in VS Code with the "dotnet interactive" extension
Final chart

What’s next

Going forward, there is an idea to describe the event interaction language in F# as a separate library or script, as described, e.g, here Since, in my opinion, a notebook like this should be filled out by a project analyst, not a programmer.

What’s not working?

I was unable to load a C# script file with extensions "*.csx" in c#-interactive. Probably haven’t gotten it in yet, maybe I’m not preparing it correctly. Plus couldn’t solve why the graphs open in the browser and not at the bottom of the code block. Also in the markdown block, formulas in $$…$$ format do not want to be displayed. (upd: in the examples in rep there is a variant of executing latech code, but then the formula is displayed in the output block, but I would like to have a nice formula in the markdown block)

Outputs

I think every .net developer should try this tool. There are a lot of ways to use it: processing results, prototyping ideas, learning F# or C#, scripting for operating system, to manage some files for example. Personally, when I accidentally heard about this tool yesterday, I was wildly delighted, which prompted me to make this post, as few people have heard about this thing (but that’s not for sure).

I want to thank my youtube subscriber, Arkadiy Kuznetsov, Who tipped me off to the existence of such a tool.

Looking forward to feedback on using this thing in the comments. I suggest you download all the notebook examples from the project repository and probe them in VS Code.

Thank you for your attention.

Useful Links

The official repa, which also has documentation
NET Interactive + ML.NET
New f# features (uses dotnet-intaractive at the beginning of the video)

You may also like