Compile and render Razor components (.razor) in .NET Interactive Notebooks.
To get started with Blazor in .NET Interactive Notebooks, first install the BlazorInteractive
NuGet package.
In a new C# (.NET Interactive)
cell enter and run the following:
#r "nuget: BlazorInteractive, 1.2.0"
Using the #!blazor
magic command your code cell will be parsed by a Blazor engine and the results displayed using the "txt/html"
mime type.
#!blazor
<h1>Hello @name</h1>
@code {
string name = "Alice";
}
You can name the generated Razor components using the -n
or --name
options.
#!blazor --name Counter
<h1>Counter</h1>
<p>
Current count: @currentCount
</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
public int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
}
You can then use the component as any other class in the next code cells:
var componentName = typeof(Counter).Name;
componentName
var counter = new Counter();
counter.currentCount
Here is an overview of the sequence of commands and events:
Contributions are always welcome!
Since this project requires a git submodule, you'll need to initialize and update the Blazor REPL submodule.
git clone --recurse-submodules -j8 https://github.com/plbonneville/BlazorInteractive.git
git submodule init
git submodule update