dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.04k stars 1.89k forks source link

Create HTML from C# in Jupyter #7126

Open markbeij opened 7 months ago

markbeij commented 7 months ago

I hope someone here can help me with this.. I'm exploring the possibilities of data analysis using c# and see a lot of samples where you generate html from your Jupyter notebooks, like this blog from Microsoft: https://devblogs.microsoft.com/dotnet/an-introduction-to-dataframe/ by @pgovind

using Microsoft.AspNetCore.Html;
Formatter<DataFrame>.Register((df, writer) =>
{
    var headers = new List<IHtmlContent>();
    headers.Add(th(i("index")));
    headers.AddRange(df.Columns.Select(c => (IHtmlContent) th(c.Name)));
    var rows = new List<List<IHtmlContent>>();
    var take = 20;
    for (var i = 0; i < Math.Min(take, df.Rows.Count); i++)
    {
        var cells = new List<IHtmlContent>();
        cells.Add(td(i));
        foreach (var obj in df.Rows[i])
        {
            cells.Add(td(obj));
        }
        rows.Add(cells);
    }

    var t = table(
        thead(
            headers),
        tbody(
            rows.Select(
                r => tr(r))));

    writer.Write(t);
}, "text/html");

If I paste this in my notebook, I get Error: (2,1): error CS0103: The name 'Formatter' does not exist in the current context

I also tried with a smaller example:

using Microsoft.AspNetCore.Html;
th(i("index"))

But I get Error: (2,1): error CS0103: The name 'th' does not exist in the current context (2,4): error CS0103: The name 'i' does not exist in the current context

I'm using polyglot notebook in VS Code using C# Script. Not sure what more info is helpfull.

feiy-sportsbet commented 5 months ago

to fix this Error: (2,1): error CS0103: The name 'Formatter' does not exist in the current context you need

r "nuget:Microsoft.ML"

r "nuget:Microsoft.DotNet.Interactive.Formatting, -"

and change Formatter.Register to Formatter.Register