Skip to content

Commit 03b4916

Browse files
committed
CSharp -- Examples -- Use CsvHelper library
1 parent b65e6dd commit 03b4916

File tree

5 files changed

+34
-58
lines changed

5 files changed

+34
-58
lines changed

Diff for: Dash.NET.Giraffe.CSharp.Example/Dash.NET.Giraffe.CSharp.Example.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net5.0</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="CsvHelper" Version="27.1.1" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<ProjectReference Include="..\Dash.NET.CSharp.Giraffe\Dash.NET.CSharp.Giraffe.fsproj" />
1014
<ProjectReference Include="..\Dash.NET.CSharp\Dash.NET.CSharp.fsproj" />

Diff for: Dash.NET.Giraffe.CSharp.Example/Program.cs

+14-29
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,29 @@
1111
using System.Linq;
1212
using System.Globalization;
1313
using Microsoft.FSharp.Core;
14+
using System.Net.Http;
15+
using CsvHelper;
1416

1517
namespace Dash.Giraffe.CSharp.Example
1618
{
17-
class Program
19+
class Iris
1820
{
19-
static string GetCSV(string url)
20-
{
21-
var req = (HttpWebRequest)WebRequest.Create(url);
22-
var resp = (HttpWebResponse)req.GetResponse();
23-
24-
var sr = new StreamReader(resp.GetResponseStream());
25-
var results = sr.ReadToEnd();
26-
sr.Close();
27-
28-
return results;
29-
}
21+
public decimal sepal_length { get; set; }
22+
public decimal sepal_width { get; set; }
23+
public decimal petal_length { get; set; }
24+
public decimal petal_width { get; set; }
25+
public string species { get; set; }
26+
public int species_id { get; set; }
27+
}
3028

29+
class Program
30+
{
3131
static void Main(string[] args)
3232
{
3333
// Read and parse CSV
3434

35-
var csv = GetCSV("https://raw.githubusercontent.com/plotly/datasets/master/iris-id.csv");
36-
var rows = csv
37-
.Split("\r\n")
38-
.Skip(1) // Skip header
39-
.SkipLast(1) // Skip empty line after split
40-
.Select(x => x.Split(","))
41-
.Select(x =>
42-
new {
43-
sepal_length = decimal.Parse(x[0], CultureInfo.InvariantCulture.NumberFormat),
44-
sepal_width = decimal.Parse(x[1], CultureInfo.InvariantCulture.NumberFormat),
45-
petal_length = decimal.Parse(x[2], CultureInfo.InvariantCulture.NumberFormat),
46-
petal_width = decimal.Parse(x[3], CultureInfo.InvariantCulture.NumberFormat),
47-
species = x[4],
48-
species_id = int.Parse(x[5], CultureInfo.InvariantCulture.NumberFormat),
49-
}
50-
)
51-
.ToList();
35+
var csv = new StreamReader(new HttpClient().GetStreamAsync("https://raw.githubusercontent.com/plotly/datasets/master/iris-id.csv").Result);
36+
var rows = new CsvReader(csv, CultureInfo.InvariantCulture).GetRecords<Iris>().ToList();
5237

5338
// Use plotly charts
5439

Diff for: Dash.NET.Giraffe.CSharp.Tests/Dash.NET.Giraffe.CSharp.Documentation.Examples.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<TargetFramework>net5.0</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="CsvHelper" Version="27.1.1" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<ProjectReference Include="..\Dash.NET.CSharp.Giraffe\Dash.NET.CSharp.Giraffe.fsproj" />
1014
<ProjectReference Include="..\Dash.NET.CSharp\Dash.NET.CSharp.fsproj" />

Diff for: Dash.NET.Giraffe.CSharp.Tests/Layout_ReusableComponents.cs

+9-14
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,21 @@
44
using static Dash.NET.CSharp.Dsl;
55
using System.Linq;
66
using Dash.NET.CSharp.Giraffe;
7+
using System.IO;
8+
using System.Net.Http;
9+
using CsvHelper;
10+
using System.Globalization;
711

812
namespace Documentation.Examples
913
{
1014
class Layout_ReusableComponents
1115
{
1216
public static void RunExample()
1317
{
14-
List<string> splitted = new List<string>();
15-
var csv = Program.GetCSV("https://gist.githubusercontent.com/chriddyp/c78bf172206ce24f77d6363a2d754b59/raw/c353e8ef842413cae56ae3920b8fd78468aa4cb2/usa-agricultural-exports-2011.csv");
16-
var headers = csv
17-
.Split("\n")
18-
.First()
19-
.Split(",");
20-
var rows = csv
21-
.Split("\n")
22-
.Skip(1)
23-
.SkipLast(1)
24-
.Select(x => x.Split(","))
25-
.ToList();
26-
18+
var csv = new HttpClient().GetStringAsync("https://gist.githubusercontent.com/chriddyp/c78bf172206ce24f77d6363a2d754b59/raw/c353e8ef842413cae56ae3920b8fd78468aa4cb2/usa-agricultural-exports-2011.csv").Result;
19+
var rows = csv.Split("\n").SkipLast(1).Select(x => x.Split(","));
20+
var headers = rows.First();
21+
var data = rows.Skip(1);
2722

2823
var layout =
2924
Html.div(
@@ -38,7 +33,7 @@ public static void RunExample()
3833
),
3934
Html.tbody(
4035
Attr.children(
41-
rows.Select(x => Html.tr(Attr.children(x.Select(x => Html.td(Attr.children(x))))))
36+
data.Select(x => Html.tr(Attr.children(x.Select(x => Html.td(Attr.children(x))))))
4237
)
4338
)
4439
)

Diff for: Dash.NET.Giraffe.CSharp.Tests/Program.cs

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,12 @@ namespace Documentation.Examples
1515
{
1616
class Program
1717
{
18-
public static string GetCSV(string url)
19-
{
20-
var req = (HttpWebRequest)WebRequest.Create(url);
21-
var resp = (HttpWebResponse)req.GetResponse();
22-
23-
var sr = new StreamReader(resp.GetResponseStream());
24-
var results = sr.ReadToEnd();
25-
sr.Close();
26-
27-
return results;
28-
}
29-
3018
static void Main(string[] args)
3119
{
32-
Layout_FirstExample.RunExample();
20+
//Layout_FirstExample.RunExample();
3321
//Layout_MoreAboutHtmlComponents.RunExample();
3422
//Layout_Markdown.RunExample();
35-
//Layout_ReusableComponents.RunExample();
23+
Layout_ReusableComponents.RunExample();
3624
}
3725
}
3826
}

0 commit comments

Comments
 (0)