|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Html; |
| 10 | +using Microsoft.DotNet.Interactive; |
| 11 | +using Microsoft.DotNet.Interactive.Formatting; |
| 12 | +using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags; |
| 13 | + |
| 14 | +namespace Microsoft.Data.Analysis.Interactive |
| 15 | +{ |
| 16 | + public class DataFrameKernelExtension : IKernelExtension |
| 17 | + { |
| 18 | + public Task OnLoadAsync(Kernel kernel) |
| 19 | + { |
| 20 | + RegisterDataFrame(); |
| 21 | + |
| 22 | + return Task.CompletedTask; |
| 23 | + } |
| 24 | + |
| 25 | + public static void RegisterDataFrame() |
| 26 | + { |
| 27 | + Formatter<DataFrame>.Register((df, writer) => |
| 28 | + { |
| 29 | + const int MAX = 10000; |
| 30 | + const int SIZE = 10; |
| 31 | + |
| 32 | + var uniqueId = DateTime.Now.Ticks; |
| 33 | + |
| 34 | + var header = new List<IHtmlContent> |
| 35 | + { |
| 36 | + th(i("index")) |
| 37 | + }; |
| 38 | + header.AddRange(df.Columns.Select(c => (IHtmlContent)th(c.Name))); |
| 39 | + |
| 40 | + if (df.Rows.Count > SIZE) |
| 41 | + { |
| 42 | + var maxMessage = df.Rows.Count > MAX ? $" (showing a max of {MAX} rows)" : string.Empty; |
| 43 | + var title = h3[style: "text-align: center;"]($"DataFrame - {df.Rows.Count} rows {maxMessage}"); |
| 44 | + |
| 45 | + // table body |
| 46 | + var maxRows = Math.Min(MAX, df.Rows.Count); |
| 47 | + var rows = new List<List<IHtmlContent>>(); |
| 48 | + for (var index = 0; index < maxRows; index++) |
| 49 | + { |
| 50 | + var cells = new List<IHtmlContent> |
| 51 | + { |
| 52 | + td(i((index))) |
| 53 | + }; |
| 54 | + foreach (var obj in df.Rows[index]) |
| 55 | + { |
| 56 | + cells.Add(td(obj)); |
| 57 | + } |
| 58 | + rows.Add(cells); |
| 59 | + } |
| 60 | + |
| 61 | + //navigator |
| 62 | + var footer = new List<IHtmlContent>(); |
| 63 | + BuildHideRowsScript(uniqueId); |
| 64 | + |
| 65 | + var paginateScriptFirst = BuildHideRowsScript(uniqueId) + GotoPageIndex(uniqueId, 0) + BuildPageScript(uniqueId, SIZE); |
| 66 | + footer.Add(button[style: "margin: 2px;", onclick: paginateScriptFirst]("⏮")); |
| 67 | + |
| 68 | + var paginateScriptPrevTen = BuildHideRowsScript(uniqueId) + UpdatePageIndex(uniqueId, -10, (maxRows - 1) / SIZE) + BuildPageScript(uniqueId, SIZE); |
| 69 | + footer.Add(button[style: "margin: 2px;", onclick: paginateScriptPrevTen]("⏪")); |
| 70 | + |
| 71 | + var paginateScriptPrev = BuildHideRowsScript(uniqueId) + UpdatePageIndex(uniqueId, -1, (maxRows - 1) / SIZE) + BuildPageScript(uniqueId, SIZE); |
| 72 | + footer.Add(button[style: "margin: 2px;", onclick: paginateScriptPrev]("◀️")); |
| 73 | + |
| 74 | + footer.Add(b[style: "margin: 2px;"]("Page")); |
| 75 | + footer.Add(b[id: $"page_{uniqueId}", style: "margin: 2px;"]("1")); |
| 76 | + |
| 77 | + var paginateScriptNext = BuildHideRowsScript(uniqueId) + UpdatePageIndex(uniqueId, 1, (maxRows - 1) / SIZE) + BuildPageScript(uniqueId, SIZE); |
| 78 | + footer.Add(button[style: "margin: 2px;", onclick: paginateScriptNext]("▶️")); |
| 79 | + |
| 80 | + var paginateScriptNextTen = BuildHideRowsScript(uniqueId) + UpdatePageIndex(uniqueId, 10, (maxRows - 1) / SIZE) + BuildPageScript(uniqueId, SIZE); |
| 81 | + footer.Add(button[style: "margin: 2px;", onclick: paginateScriptNextTen]("⏩")); |
| 82 | + |
| 83 | + var paginateScriptLast = BuildHideRowsScript(uniqueId) + GotoPageIndex(uniqueId, (maxRows - 1) / SIZE) + BuildPageScript(uniqueId, SIZE); |
| 84 | + footer.Add(button[style: "margin: 2px;", onclick: paginateScriptLast]("⏭️")); |
| 85 | + |
| 86 | + //table |
| 87 | + var t = table[id: $"table_{uniqueId}"]( |
| 88 | + caption(title), |
| 89 | + thead(tr(header)), |
| 90 | + tbody(rows.Select(r => tr[style: "display: none"](r))), |
| 91 | + tfoot(tr(td[colspan: df.Columns.Count + 1, style: "text-align: center;"](footer))) |
| 92 | + ); |
| 93 | + writer.Write(t); |
| 94 | + |
| 95 | + //show first page |
| 96 | + writer.Write($"<script>{BuildPageScript(uniqueId, SIZE)}</script>"); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + var rows = new List<List<IHtmlContent>>(); |
| 101 | + for (var index = 0; index < df.Rows.Count; index++) |
| 102 | + { |
| 103 | + var cells = new List<IHtmlContent> |
| 104 | + { |
| 105 | + td(i((index))) |
| 106 | + }; |
| 107 | + foreach (var obj in df.Rows[index]) |
| 108 | + { |
| 109 | + cells.Add(td(obj)); |
| 110 | + } |
| 111 | + rows.Add(cells); |
| 112 | + } |
| 113 | + |
| 114 | + //table |
| 115 | + var t = table[id: $"table_{uniqueId}"]( |
| 116 | + thead(tr(header)), |
| 117 | + tbody(rows.Select(r => tr(r))) |
| 118 | + ); |
| 119 | + writer.Write(t); |
| 120 | + } |
| 121 | + }, "text/html"); |
| 122 | + } |
| 123 | + |
| 124 | + private static string BuildHideRowsScript(long uniqueId) |
| 125 | + { |
| 126 | + var script = $"var allRows = document.querySelectorAll('#table_{uniqueId} tbody tr:nth-child(n)'); "; |
| 127 | + script += "for (let i = 0; i < allRows.length; i++) { allRows[i].style.display='none'; } "; |
| 128 | + return script; |
| 129 | + } |
| 130 | + |
| 131 | + private static string BuildPageScript(long uniqueId, int size) |
| 132 | + { |
| 133 | + var script = $"var page = parseInt(document.querySelector('#page_{uniqueId}').innerHTML) - 1; "; |
| 134 | + script += $"var pageRows = document.querySelectorAll(`#table_{uniqueId} tbody tr:nth-child(n + ${{page * {size} + 1 }})`); "; |
| 135 | + script += $"for (let j = 0; j < {size}; j++) {{ pageRows[j].style.display='table-row'; }} "; |
| 136 | + return script; |
| 137 | + } |
| 138 | + |
| 139 | + private static string GotoPageIndex(long uniqueId, long page) |
| 140 | + { |
| 141 | + var script = $"document.querySelector('#page_{uniqueId}').innerHTML = {page + 1}; "; |
| 142 | + return script; |
| 143 | + } |
| 144 | + |
| 145 | + private static string UpdatePageIndex(long uniqueId, int step, long maxPage) |
| 146 | + { |
| 147 | + var script = $"var page = parseInt(document.querySelector('#page_{uniqueId}').innerHTML) - 1; "; |
| 148 | + script += $"page = parseInt(page) + parseInt({step}); "; |
| 149 | + script += $"page = page < 0 ? 0 : page; "; |
| 150 | + script += $"page = page > {maxPage} ? {maxPage} : page; "; |
| 151 | + script += $"document.querySelector('#page_{uniqueId}').innerHTML = page + 1; "; |
| 152 | + return script; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments