2

I have a .NET Interactive Notebook, works fine when I execute from VS Code with the extension

This contains a sequence of PowerShell scripts, that I use for local builds on my system

I want to re-use the same in CI

Is there a way to Invoke the ipynb file from command line?


Edit

I did come across PowerShellNotebook module

So I can do something like

Invoke-ExecuteNotebook -InputNotebook .\quick.ipynb

Problem is

I refer to files using relative path (reading json, importing module, ...) the relative path doesn't work very well with Invoke-ExecuteNotebook

Meaning, something like this won't work https://i.imgur.com/E9h0EbF.png https://i.imgur.com/Jv2zxKr.png

I tries Get-Location, Split-Path $MyInvocation.MyCommand.Path -Parent

Both didn't work for finding current location

SpicyCoder
  • 21
  • 4
  • What do you mean by "_I refer to files using relative path (reading json, importing module, ...) the relative path doesn't work very well with Invoke-ExecuteNotebook_"? – Karolina Ochlik Jun 02 '21 at 08:38
  • If I try to do something like ```ps1 $currentPath = Get-Location Remove-Item (Resolve-Path "$currentPath\hello.txt") ``` It errors out – SpicyCoder Jun 02 '21 at 09:03
  • But what's the error? What does it say? EDIT: Ok, I see you've edited a question. :) – Karolina Ochlik Jun 02 '21 at 09:16

2 Answers2

1

There is support for running .NET Interactive notebooks (including both the .ipynb and .dib formats) in dotnet-repl. Unlike with Papermill, it's not necessary to install a Jupyter kernel when using dotnet-repl.

dotnet repl --run /path/to/input.ipynb --output-path /path/to/output.ipynb --exit-after-run --output-format ipynb
jonsequitur
  • 536
  • 4
  • 8
0

At the time of writing (April 2023) this doesn't seem possible out of the box. There's an open issue for this on the .NET Interactive GitHub repo. https://github.com/dotnet/interactive/issues/839

The PowerShellNotebook module you mentioned in the edit is an option you can use. From this blogpost and this other answer I discovered there's also Papermill from nteract, which can execute your notebooks provided they are using the .ipynb file format.

In my case I got this to work by first installing .NET Interactive CLI and the Jupyter kernels with.

dotnet tool install --global Microsoft.dotnet-interactive

dotnet-interactive jupyter install

python3 -m pip install papermill

papermill --kernel .net-powershell notebook.ipynb output.ipynb

One limitation of this that I've found is that papermill can only handle one kernel at a time -- that is to say, you can't mix C# and Powershell in a notebook.

teauxfu
  • 39
  • 7
  • Combining languages in one notebook should work, since the kernel itself is polyglot-aware. – jonsequitur Apr 05 '23 at 19:19
  • This seems to be a limitation of papermill, not the kernel. https://github.com/nteract/papermill/blob/main/papermill/execute.py Either way, your solution is cleaner. – teauxfu Apr 05 '23 at 20:12