Sunday 29 March 2020

Getting C# Jupyter Notebooks running on Ubuntu 18.04

Why C# on Jupyter Notebooks?

I’ve heard of Jupyter, or Jupyter-like notebooks often. I've briefly evaluated the SQL Server notebooks in Azure Data Studio, and often hear of them used in data science.

The primary notebook platform, Jupyter notebook, only supports Python, and I haven’t yet invested in learning that language. This has previously been a stumbling block for me learning further about notebooks.

At a recent SQL Saturday precon, notebooks were heavily used as a part of the training material, and I got to experience enough to understand the benefit of using notebooks: annotated, byte-size, interactive, hands-on documentation.

I've been planning an introduction presentation to Azure Cosmos DB for our web developers, and Jupyter notebooks seem to be a great fit for that demo, with the exception that Jupyter notebooks use Python, not C#, the developers' native language. Why not use Visual Studio / VS Code? I don't want the web developers to be judging my console application development abilities, I want them to see how the Cosmos DB client can be interacted with in C#.

Azure Cosmos DB has a C# Notebook sample, but that appears to be broken at present, and I can’t work that out - but the idea is simple enough - it is just a notebook running C#. After a little searching on Google, I found that the .NET team have put out instructions on getting a C# kernel (runner) installed in standard Jupyter Notebooks. Here’s the steps I followed to get that up and running.

Steps to creating a C# Jupyter Notebooks environment running on Ubuntu:

Download and Install Anaconda

  • Download the Anaconda installer from https://www.anaconda.com/distribution/#linux (I chose the Python 3.7 installer) 
  • Make the script executable: Right-click on the .sh installer file, and select Properties > Permissions > Allow executing file as program. 
  • Open Terminal, navigate to the folder containing the Anaconda installer, and execute the .sh file 
  • I ran the two commands given as optional in the Anaconda installer. 
  • Run jupyter notebook from Terminal. All well, Jupyter Notebook will load. 
  • Select New, and note that only “Python 3” is an option. Additional engines, called “kernels” can be installed to add additional languages to this list. 
  • In Terminal, press CTRL+C to kill Jupyter Notebook.

Install dotnet interactive and enable C# and PowerShell support to Jupyter Notebooks

  • Install the .NET 3.1 SDK by following the Register Microsoft Key and Feed and Install the .NET Core SDK steps at Install .NET Core on Ubuntu 18.04 package manager - .NET Core 
  • Run the command to install dotnet interactive at dotnet/interactive 
  • Run the command to add the C# and Powershell kernels to Jupyter
  • Notebooks ( dotnet interactive jupyter install ) 
  • Check the C# and Powershell kernels have been added with the command jupyter kernelspec list 
  • Run the command jupyter notebook from Terminal again 
  • Select the New menu item, and you should now see “.NET (C#)” as an option, select this. 
  • Enter Console.WriteLine("Hello from C#!"); into the box beside In [ ]:, and click the Run button. 
  • The In [ ]: text should change to In [ * ]: text to indicate that block is now running.
You now have a C# Jupyter Notebook running in Ubuntu. Switch to Powershell, replace that sample with Write-Host("Hello from PowerShell!");

There we have it - below is a screenshot of the C# tutorial Tutorial: Build a .NET console app to manage data in Azure Cosmos DB SQL API account running successfully from a Jupyter notebook.

No comments:

Post a Comment