.NET Interactive

repository·main·Indexed 25 days ago

https://github.com/dotnet/interactive

An engine and API for running and editing code interactively, supporting multiple languages including C#, F#, JavaScript, and PowerShell. It powers Polyglot Notebooks, allowing for variable sharing between languages, magic commands, and NuGet package integration. It can be used as a Jupyter kernel, a REPL engine, or embedded in applications via its CLI and API.

Tokens
46.4K
Snippets
159
Records
320
Agent score
82%

What's inside .NET Interactive

  1. Overview of .NET Interactive

    main

    .NET Interactive is an engine and API designed for interactive code execution and editing. It enables:

    • Running code and retrieving results.
    • Providing language services like completions and diagnostics.
    • Sharing data and variables across multiple languages and remote machines.

    While commonly used with notebook technologies (like Jupyter), it can also serve as an execution engine for REPLs or embedded script engines.

  2. Understand .NET Interactive Formatting and Rendering

    main

    In tools like Polyglot Notebooks or Jupyter, output is produced using .NET Interactive formatters (found in the Microsoft.DotNet.Interactive.Formatting namespace).

    Formatting is the process of creating a string representation of an object (e.g., plain text, HTML, JSON, or CSV). Rendering is the process where the notebook interface (like VS Code or JupyterLab) displays that formatted string.

    Output is triggered by:

    • A return statement or trailing expression in C#.
    • A trailing expression in F#.
    • Calling the Display or ToDisplayString extension methods on any object in C# or F#.
    • Calling Out-Display in PowerShell.
  3. Understand the .NET Interactive Kernel concept

    main

    A Kernel is the core abstraction in .NET Interactive that accepts commands (typically code blocks) and produces outputs (events describing results, execution, or language services like completions and diagnostics).

    Kernels can run:

    • In-process: Multiple kernels (e.g., C#, F#, PowerShell) run in a single process, allowing for language-switching and sharing .NET variables by reference.
    • Out-of-process: Kernels can run in separate processes or on remote machines, handled transparently via message-based APIs.

    Communication is supported via multiple protocols, including the Jupyter message protocol and a JSON-based message protocol accessible over standard I/O, named pipes, or HTTP.

  4. Interoperability with Jupyter

    main

    Polyglot Notebooks are fully interoperable with Jupyter and support the .ipynb file extension.

    • File Format: Notebooks saved in .ipynb format can be opened in Jupyter, and cell languages will still be recognized.
    • Jupyter Kernel Usage: When using the .NET Interactive kernel within Jupyter, you can switch cell languages using magic commands.
  5. Provide notebook inputs via .NET REPL command line

    main

    For automation scenarios where no user is present, the .NET REPL allows you to pass values for @input:-prefixed tokens via the command line.

    1. Describe required parameters: Use dotnet repl describe <path-to-notebook> to see the required parameters and example command-line syntax.

    2. Run notebook with inputs: Use the --input flag multiple times to provide values for different parameters.

    Note: This is an experimental feature of the .NET REPL.

  6. Use multiple languages in Jupyter notebooks with .NET Interactive

    main

    While .ipynb notebooks have a default language (visible in the upper right corner in JupyterLab), .NET Interactive allows you to execute different languages within the same notebook using magic commands.

    To switch the language for a specific cell, use a magic command at the start of the cell. For example, if your notebook's default language is F#, you can run C# or PowerShell code by using #!csharp or #!pwsh respectively.

  7. Register .NET Interactive as a Jupyter kernel

    main

    To use .NET Interactive kernels (C#, F#, and PowerShell) in Jupyter Notebook, JupyterLab, or other Jupyter frontends, you must register them as kernels.

    Prerequisites

    Installation Steps

    1. Install the .NET Interactive global tool in an ordinary console:
      dotnet tool install -g Microsoft.dotnet-interactive
    2. Install the Jupyter kernels using your Anaconda Prompt (or terminal) by running:
      dotnet interactive jupyter install

    Verification

    Run the following command in your Anaconda Prompt to ensure the kernels are registered:

    jupyter kernelspec list

    You should see entries for .net-csharp, .net-fsharp, and .net-powershell.

    dotnet tool install -g Microsoft.dotnet-interactive
    dotnet interactive jupyter install
  8. Setup the PostgreSQL test database

    main

    To run tests for the PostgreSQL extension, you must provide a valid connection string via the TEST_POSTGRESQL_CONNECTION_STRING environment variable. The tests require the Northwind database to be installed. You can create a blank database and install it using the Northwind SQL script.

    export TEST_POSTGRESQL_CONNECTION_STRING='Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=northwind'
  9. Install .NET Interactive kernels for Jupyter

    main

    The dotnet interactive jupyter install command registers the .NET Interactive kernel with Jupyter by installing three kernelspecs: C#, F#, and PowerShell. While each kernelspec starts in a different default language mode, they all use the same underlying kernel, and full polyglot capabilities remain available via kernel selector magic commands.

    By default, the command uses the jupyter kernelspec module. If not found, it attempts to install to platform-specific folders for Python or Anaconda. You can specify a custom installation directory using the --path flag.

    dotnet interactive jupyter install --path /location/to/install
  10. Use @input tokens in magic commands

    main

    You can prompt a user for a value within any magic command by using the @input token. When the cell is executed, an input prompt is displayed at the top of the VS Code window. This is useful for parameterizing notebooks with query parameters, authentication tokens, or file paths without hard-coding them.

    #!set --name myVariable --value @input
  11. Create a .NET Interactive extension via NuGet

    main

    You can distribute extensions as NuGet packages. When a user loads your package using #r nuget, .NET Interactive looks for a specific file to activate the extension.

    To implement this:

    1. Create a public method in your library (e.g., Load(Kernel kernel)) to perform the extension logic.
    2. Create a file named extension.dib in your project.
    3. In extension.dib, write code to call your library's method, passing in the root kernel.
    4. Configure your .csproj to place extension.dib in the interactive-extensions/dotnet folder within the NuGet package.
    <!-- extension.dib content -->
    
    ```csharp
    #!csharp
    
    ClockExtension.ClockKernelExtension.Load(Microsoft.DotNet.Interactive.KernelInvocationContext.Current.HandlingKernel.RootKernel);

    <!-- .csproj configuration -->

    <ItemGroup>
        <None Include="extension.dib" Pack="true" PackagePath="interactive-extensions/dotnet" />
    </ItemGroup>