Using Nuget packages with .NET Interactive

Last time I installed Jupyter with .NET and tried to run a few commands to get a feel for what it's like.

Before starting today's session and exploring how I could use Nuget packages, I wondered about the limits of Jupyter / .Net Interactive. Specifically, what kind of code I can execute.

Does it run within a sandbox?

At first glance, it does not appear to do so.

Hello, C Drive!

This is fine on my PC where I am the only one responsible for what code gets executed, not so much if I wanted to host a Jupyter notebook online.

Nuget packages in a notebook

The docs for .NET Interactive are sparse as of today. I'm exploring it using a few samples that are well hidden inside the repo, specifically a sample for Importing packages, libraries, and scripts.

Let's say we want to play around with taglib-sharp. First, we reference it:

#r "nuget: TagLibSharp, 2.2.0"

In the next cell, we can import its namespace, instantiate its classes and invoke their methods.

First, let's expand the original Song class to work with more metadata attributes and make TagLib a dependency.

public class Song
{
    public Song(TagLib.Tag tag)
    {
        _tag = tag;
    }

    private readonly TagLib.Tag _tag;

    public string Title => _tag.Title;
    public string Artist => string.Join(", ", _tag.Performers);
    public string Genre => string.Join(", ", _tag.Genres);
}

The rest is straightforward:

var tag = TagLib.File.Create(@"C:\Users\Tomas\Music\Library\Juan D'Arienzo\Todo de Juan 5\Ansiedad 12562-1_RP.flac").Tag;
var song = new Song(tag);
display(song);

Lo and behold, the code can access my song and read its metadata:
Exploring TagLibSharp in .NET Interactive

Very cool.

In the next installment, I will look at my hosting options, what the security boundaries are, and what it means for deployment.
PS I reviewed Jupyter docs for public server installation, which is single-user and not really what I meant by "hosting".

The docs led me to JupyterHub, which seems appropriate for class-room use. From what I was able to understand, it's still not meant for a wider public use (aka, "come to my site and play with my notebooks, anonymously"). For even more users, there's Jupyter on Kubernetes - I know Docker and k8s well enough to not want to pay for the infrastructure if all I wanted to do was to expose a little playground.

Microsoft's own docs talk about Binder, which takes the pain (and cost) of Docker deployment out of your hands. As much as I wanted to do this on my own, that's what I would probably use.

P.S.

If you feel like we'd get along, you can follow me on Twitter, where I document my journey.

Published on

What do you think? Sound off in the comments!

Thanks, helpful!