Tuesday, October 24, 2023

Enable Python preview in Polyglot notebook

It is a big difference if anyone ever tried the Jupyter Notebook. Once we experience, we need that experience everywhere including .Net. That experience was there in terms of a Jupyter kernel for .Net¹. That requires Jupyter installed in the machine already. Finally, more native experience came for .Net in the form of a VS Code extension. That was called .Net Interactive Notebooks² which uses .Net Interactive (formerly Try.Net) behind the scene. That started supporting many languages such as PowerShell, JavaScript, SQL etc... which is kinda out of the .Net world. Finally, in Nov 2022, .Net Interactive was renamed to Polyglot notebooks³ to reflect its purpose.

Is Python supported by Polyglot Notebooks?

The Polyglot notebooks didn't have the support for Python. We had to switch to Jypyter to work with Python and come back. Now Polyglot Notebooks has Python support as a preview.

How to enable Python in Polyglot Notebook?

Obviously, we should have Polyglot notebooks up and running. The first step to enable Python preview is that we need to install Jupyter on the machine and make sure the Python kernel spec is available. Run the below command to make sure it is there.
Once this is done, we can see 2 Kernel options at the Notebook level in Visual Studio Code.

The enabling preview feature requires the below command to be run on the notebook cell.

#!connect jupyter --kernel-name pythonkernel --kernel-spec python3

The command output shows it is in preview mode.
Command is - #!connect jupyter --kernel-name pythonkernel --kernel-spec python3

Once we add support, we can start having Python cells alongside C#. The cell kernel type needs to be the name we have given in the connect command. Below is the final state. Here we can learn C# and Python side by side.

We can even set up R language in Polyglot Notebooks. Hereafter don't waste time creating an entire project to confirm small language or platform features.

References

¹ - https://devblogs.microsoft.com/dotnet/net-core-with-juypter-notebooks-is-here-preview-1/

² - https://hanselman.com/blog/announcing-net-interactive-try-net-includes-net-notebooks-and-more

³ - https://devblogs.microsoft.com/dotnet/dotnet-interactive-notebooks-is-now-polyglot-notebooks/

⁴ - https://techcommunity.microsoft.com/t5/educator-developer-blog/using-visual-studio-notebooks-for-learning-c/ba-p/3580015

⁵ - https://github.com/dotnet/interactive/blob/main/docs/jupyter-in-polyglot-notebooks.md

No comments: