Importing Code in Polyglot Notebooks

Using NuGet and Local Files to Import Code into Notebooks

Matt Eland
AccessibleAI

--

We’ve seen that Polyglot Notebooks allow you to mix together markdown and code (including C# code) in an interactive notebook and these notebooks allow you to share data between cells and between languages. However, frequently in programming you want to reference code that others have written without having to redefine everything yourself.

In this article we’ll explore how Polyglot Notebooks allows you to import dotnet code from stand-alone files, DLLs, and NuGet packages so your notebooks can take advantage of external code files and the same libraries that you can work with from your code in Visual Studio.

Importing Local Code Files

First of all, if you have a small amount of code that lives in an individual C# file and you wanted to reference it in your notebook, you can do this via the #!import magic command as shown below:

#!import Person.cs

This assumes that there is a file named Person.cs in the current directory, but assuming it is present the code in that file will be imported as if you had declared it in your notebook.

This can be helpful for hiding code like test data or class definitions that isn’t the…

--

--

Matt Eland
AccessibleAI

Microsoft MVP in AI, AI Specialist at Leading EDJE. Author of "Refactoring with C#".