C# Jupyter Notebook

This notebook demonstrates the features of the C# kernel for Jupyter Notebook. The C# Kernel plugin is using Roslyn as a backend to implement C# scripting.

Basics in C#

The kernel is using C# scripting which is a little different than using C# with Visual Studio. We can define variables and print out their values or evaluate expressions.


In [1]:
int someInteger = 14323456;
someInteger


Out[1]:
14323456

In [2]:
double someDouble = 22.0/7;
someDouble


Out[2]:
3.1428571428571428

It is also possible to evaluate expressions and print it out.


In [3]:
(1233+7)/32


Out[3]:
38

Console output also can be used to print out values.


In [4]:
var z = 12451;

Console.WriteLine($"The result is {z}.");


The result is 12451.

Here is an array and a list definition


In [5]:
var l = new int[] {1, 2, 3, 5, 7};
l


Out[5]:
int[5] { 1, 2, 3, 5, 7 }

In [6]:
var li = new List<int>() {1, 2, 3, 4, 5};
li


Out[6]:
List<int>(5) { 1, 2, 3, 4, 5 }

It is also possible define methods and then call them later


In [7]:
int Add(int x, int y) { return x+y; }

Add(12,13)


Out[7]:
25

Tables

There are a few extension that C# kernel defines that would help to present data in a better looking format. For example, printing out collection of classes as HTML or displaying mathematical figures using LaTex.


In [8]:
public class Person
{
    public string FirstName { get; set;}
    public string LastName { get; set; }
    public int Age {get; set; }
}; 

List<Person> persons = new List<Person> {
    new Person {FirstName = "John", LastName = "Doe", Age = 35}, 
    new Person {FirstName = "Jane", LastName = "Doe", Age = 26}, 
};

persons.AsTable()


Out[8]:
FirstNameLastNameAge
JohnDoe35
JaneDoe26

It is also possible to select the columns to display.


In [9]:
persons.AsTable(new List<string> {"FirstName", "Age"})


Out[9]:
FirstNameAge
John35
Jane26

Using LaTex


In [10]:
"f(x)".AsMath()


Out[10]:
$$f(x)$$

In [11]:
"x=\\frac{1+y}{1+2z^2}".AsMath()


Out[11]:
$$x=\frac{1+y}{1+2z^2}$$

HTML

It is also possible to inline html in the notebook. It is good for inlining html.


In [12]:
var htmlContent = "<div style=\"background-color: #007fff; font-weight: bold;\">Would you like some toast?</div>";

htmlContent.AsHtml()


Out[12]:
Would you like some toast?

Or embedding video...


In [15]:
var video = "<video width=\"480\" controls poster=\"https://archive.org/download/WebmVp8Vorbis/webmvp8.gif\" >"
   + "<source src=\"https://archive.org/download/WebmVp8Vorbis/webmvp8.webm\" type=\"video/webm\">" 
   + "<source src=\"https://archive.org/download/WebmVp8Vorbis/webmvp8_512kb.mp4\" type=\"video/mp4\">"
   + "<source src=\"https://archive.org/download/WebmVp8Vorbis/webmvp8.ogv\" type=\"video/ogg\">"
   + "Your browser doesn't support HTML5 video tag."
   + "</video>"; 
   
video.AsHtml()


Out[15]:

Charts

It is also possible to render graph using using XPlot.Plotly. For example:


In [3]:
#load "XPlot.Plotly.csx"

new int[,] data = {{1, 20, 30}, {20, 1, 60}, {30, 60 ,1}}; 

data


Out[3]:
int[3, 3] { { 1, 20, 30 }, { 20, 1, 60 }, { 30, 60, 1 } }

In [1]:
Graph.Heatmap h = new Graph.Heatmap();
h.z = new int[,] { { 1, 20, 30 }, { 20, 1, 60 }, { 30, 60, 1 } };

var chart = Chart.Plot(h);
chart.WithLayout(new Layout.Layout() { title = "Heatmap example!" });
chart.WithHeight(500);
chart.WithWidth(700);

string html = chart.GetInlineHtml();

html.AsHtml()


Out[1]:

Notes and Credits

This notebook loosely based on the demo notebook from the IFsharp Kernel. Big kudos for the F# community for putting such a cool kernel together.