machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

Create HTML from C# in Jupyter

Open markbeij opened this issue 4 months ago • 1 comments

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.

markbeij avatar Apr 11 '24 09:04 markbeij