|
| 1 | +--- |
| 2 | +title: "Show Table in Polyglot Notebook" |
| 3 | +author: "PrashantUnity" |
| 4 | +weight: 115 |
| 5 | +date: 2025-03-14 |
| 6 | +lastmod: 2025-03-14 |
| 7 | +dateString: March 2025 |
| 8 | +description: "Show list as Table in Polyglot note book using dataframe" |
| 9 | +#canonicalURL: "https://canonical.url/to/page" |
| 10 | +cover: |
| 11 | + image: "cover.jpg" # image path/url |
| 12 | + alt: "Download Logo" # alt text |
| 13 | + #caption: "Optical Character Recognition" display caption under cover |
| 14 | + |
| 15 | +tags: [ "NET", "codefrydev", "C sharp", "CFD", "Table","Polyglotnotebook","Dataframe"] |
| 16 | +keywords: [ "NET", "codefrydev", "C sharp", "CFD","Table","Dataframe"] |
| 17 | +hideMeta: true |
| 18 | +--- |
| 19 | + |
| 20 | +## Create .ipynb file using selwct .net Enviroment and select C# in cell for execution |
| 21 | + |
| 22 | +### Import nuget package |
| 23 | + |
| 24 | +```cs |
| 25 | +#r "nuget: Microsoft.Data.Analysis" |
| 26 | +``` |
| 27 | + |
| 28 | +### Import namespace |
| 29 | + |
| 30 | +```cs |
| 31 | +using System; |
| 32 | +using System.Collections.Generic; |
| 33 | +using Microsoft.Data.Analysis; |
| 34 | +``` |
| 35 | + |
| 36 | +### Create class |
| 37 | + |
| 38 | +```cs |
| 39 | +public class RedirectEntry |
| 40 | +{ |
| 41 | + public string WebsiteUrl { get; set; } |
| 42 | + public string RedirectUrl { get; set; } |
| 43 | + public string Status => IsValid ? "✅" : "❌"; |
| 44 | + public bool IsValid { get; set; } |
| 45 | +} |
| 46 | + |
| 47 | +var data = new List<RedirectEntry> |
| 48 | +{ |
| 49 | + new RedirectEntry |
| 50 | + { |
| 51 | + WebsiteUrl = "https://codefrydev.in/Updates", |
| 52 | + RedirectUrl = "https://codefrydev.in/", |
| 53 | + IsValid = true |
| 54 | + }, |
| 55 | +}; |
| 56 | + |
| 57 | +``` |
| 58 | + |
| 59 | +### Create Generic Function for anytype of list incection |
| 60 | + |
| 61 | +```cs |
| 62 | +DataFrame ShowTable<T>(List<T> items) |
| 63 | +{ |
| 64 | + var properties = typeof(T).GetProperties(); |
| 65 | + List<DataFrameColumn> columns = new(); |
| 66 | + |
| 67 | + foreach (string header in properties.Select(p => p.Name)) |
| 68 | + { |
| 69 | + columns.Add(new StringDataFrameColumn(header, new List<string>())); // Read as string |
| 70 | + } |
| 71 | + foreach(var item in items) |
| 72 | + { |
| 73 | + for (int i = 0; i < properties.Length; i++) |
| 74 | + { |
| 75 | + var values = properties.Select(p => p.GetValue(item)?.ToString() ?? "").ToList(); |
| 76 | + ((StringDataFrameColumn)columns[i]).Append(values[i]); |
| 77 | + } |
| 78 | + } |
| 79 | + return new DataFrame(columns); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +### Simply call the function in next cell without any commas or stuff |
| 84 | + |
| 85 | +```cs |
| 86 | +ShowTable(data) |
| 87 | +``` |
0 commit comments