
For years now I have seen people talk about Jupyter Notebooks, but I was never sure exactly what they were for. They are tied to Python and academic use, so I never found it convenient to fire one up. Explanations from Python users usually involve references to other Python tools — always a bit of a problem with over-confident ecosystems.
Even in my last post, I mentioned Jupyter Notebooks in reference to talking to an LLM. But I also noticed that while Python is the native language, you can make notebooks use C#. Also, and even better, they use Markdown natively.
So first of all, what are they? A Jupyter Notebook is “a web-based application used to create and share interactive notebook documents, which can contain live code, text, data visualizations, videos and other computational outputs.” That is kind of useful, but before I dive in let me show another example:
So it gives you a continual or interactive environment, making it very good for explaining things step by step. And those code blocks or cells can be recalculated in any order. Technically, it isn’t a bad way to learn a programming language, and I can see why it should be popular in all forms of education. So my aim is to create a notebook that works with C#, but is still shareable.
Perhaps in their attempt to implement Jupyter Notebooks with .NET, Microsoft came out with Polyglot Notebooks, which are actually a little more ambitious than the original. More on that later. They work as an extension within Visual Studio Code. This sounds right for what I want to do.
Because VS Code has a rather opaque install chain, and combined with Microsoft’s habit of renaming things rapidly, prerequisites and dependencies are not always clear.
After opening VSCode and doing some experimentation, I installed PolyGlot Notebooks. This is “powered by” .NET Interactive. I also have the Jupyter Notebook support extension installed — I think I installed that before, but it might be a dependency.
You will then get the chance to “Create a default Polyglot Notebook” from the command palette. Then choose the standard Python notebook ‘ipynb’ extension, and then C# as your language:
You should then be able to write a simple code block or cell, as demonstrated below:
OK, so we have a notebook running in VS Code that should allow us to create and run code snippets within a continuous environment. That tick next to ‘0.1s’ in the cell indicates that the code has been run and it took that much time. And once you get here, everything is very simple to use. Next, we’ll write a cell that uses a variable defined in a previous cell, like we saw in the example at the top:
Note that you get the chance to evaluate a cell or all the cells above it. And you can see that a cell can use any evaluated variable from the cells above. Also, the console places its output just below the cell.
In addition to cells with code, I can write Markdown and mix that in. So we make a Markdown cell (see the “+ Markdown” above) and edit it appropriately:
After selecting the tick when I’m done, I can then move the cell above my code to act as documentation:
For my last trick, I’ll install a plotter library and draw a sine wave. Add the “NuGet package manager” as a VS Codes extension. I’m going to use the XPlot.Plotly library, and I can install this in a cell:
You need the interactive variant to work with the Notebook.
Then we write the code, and execute it. Voila.
This immediate feedback is gratifying. You will be able to turn that into a cosine wave very rapidly. By the way, you can export a notebook to a PDF or as an HTML file.
But one more thing. The other point of these is collaboration. Given that we specifically made this notebook in the common Python ‘ipynb’ format, I’d like to see how easy it would be to use this as a common Notebook environment. We are using a C# code kernel, but the actual notebook itself should still be portable.
Using Google Colaboratory, I was able to load the notebook successfully:
This is great. Of course, running it is a different matter. Colaboratory recognized that the cell kernel was not Python, but does not recognize any other languages as yet. This Google tool is designed with only vague and limited academic use in mind. Other online services offer something similar.
We could certainly run a Jupyter Notebook locally and change the kernel, but that would extend this post deep into Python territory, which isn’t where I want to go. But you could also view this as a simple way to introduce yourself to the language.
Currently, Python and Jupyter Notebooks are a little too tightly coupled, but in the future, I can see a more flexible structure allowing more languages to run as peers in notebook systems. Right now, both are embedded in the world of academia and data science. Microsoft has got Polyglot Notebooks to run multiple languages in one notebook, which is a sensible idea. As notebooks grow beyond the Python ecosystem, I can only see them getting more popular.
The post Introduction to Jupyter Notebooks for Developers appeared first on The New Stack.
David Eastman road-tests Jupyter Notebooks, using Microsoft's .NET implementation in Visual Studio Code, Polyglot Notebooks.