Skip to content
Bruno Sonnino
Menu
  • Home
  • About
Menu

Interactive Notebooks with C#

Posted on 11 February 2022

Sometime ago I posted an article about Dotnet Try. This is a wonderful tool to document your code but, since then, a lot has changed.

.NET 6 and C#10 are here, Visual Studio Code is a nice environment for editing your code, and you can even edit your code in the web. And a new feature has appeared to document your code: .NET Interactive. With it, you can create interactive notebooks and create interactive pieces of code, where you can share and try sample code.

Its use is very simple: just install the .NET Interactive extension in VS Code and you’re ready to go. Just press Ctrl+Shift+Alt+N and open a new notebook (you can choose C#, F# or even Powershell as the default language) and a new notebook is ready to use. If you want to open an existing notebook, just press Ctrl+Shift+P and select .NET Interactive: Open Notebook and open the existing notebook.

When you create a new notebook, you can add markdown or code cells. The markdown cells have text formatted with the Markdown syntax and the code cells can have code, in the selected language. You can run the code by clicking the arrow icon next to the cell or by pressing the Ctrl+Alt+Enter keys.

To display the values, you can use Console.WriteLine or simply declare a variable and put it in a single line like in

if you want to display more than a single value, you can use the display function, like in

You can even mix languages in the notebook. If you add !#fsharp in the first line, you can add F# code, like in:

If you want to use javascript, you can use something like:

Getting data from an URL

You can get some data from an URL, using the command

#!value --from-url https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.csv --name housingPath
PowerShell

Once you have the data, you can display it with SandDance, with this code:

using Microsoft.Data.Analysis;
using Microsoft.ML;
using System.Collections.Generic;

var housingData = DataFrame.LoadCsv(housingPath);
housingData.ExploreWithSandDance().Display();
C#

Using Nuget Packages

You can also use Nuget packages with the #r directive. For example, we can get the suppliers dataset as a Json file from the url “https://northwind.vercel.app/api/suppliers”, with this code:

#!value --from-url https://northwind.vercel.app/api/suppliers --name suppliersJson
PowerShell

Then we can show the data as C# classes with:

#!share --from value suppliersJson
#r "nuget:Newtonsoft.Json"

using Newtonsoft.Json;

public record Address(string Street, string City, string Region, string PostalCode, string Country, string Phone);
public record Supplier(string Id, string CompanyName, string ContactName, string ContactTitle, Address Address);
var suppliers = JsonConvert.DeserializeObject<List<Supplier>>(suppliersJson);
suppliers
C#

As you can see there are multiple possibilities. I even created the notebook for this article (you can download it at https://github.com/bsonnino/CSharpNotebooks) based on my last article, about Linq in .NET 6. You can use it as a notebook for you, as a learning tool, as a prototype tool, or even as a tool for sharing knowledge.

The notebook for this article is at https://github.com/bsonnino/CSharpNotebooks

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • May 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • June 2024
  • November 2023
  • October 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • April 2020
  • March 2020
  • January 2020
  • November 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • October 2015
  • August 2013
  • May 2013
  • February 2012
  • January 2012
  • April 2011
  • March 2011
  • December 2010
  • November 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • Development
  • English
  • Português
  • Uncategorized
  • Windows

.NET AI Algorithms asp.NET Backup C# Debugging Delphi Dependency Injection Desktop Bridge Desktop icons Entity Framework JSON Linq Mef Minimal API MVVM NTFS Open Source OpenXML OzCode PowerShell Sensors Silverlight Source Code Generators sql server Surface Dial Testing Tools TypeScript UI Unit Testing UWP Visual Studio VS Code WCF WebView2 WinAppSDK Windows Windows 10 Windows Forms Windows Phone WPF XAML Zip

  • Entries RSS
  • Comments RSS
©2025 Bruno Sonnino | Design: Newspaperly WordPress Theme
Menu
  • Home
  • About