Skip to content

Commit ef98069

Browse files
authored
Merge pull request #24 from plotly/issue-17
2 parents 4c2559e + 80ad82a commit ef98069

File tree

18 files changed

+712
-44
lines changed

18 files changed

+712
-44
lines changed

Diff for: Dash.NET.sln

+56
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Dash.NET.ComponentGeneratio
5858
EndProject
5959
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Dash.NET.ComponentGeneration.Tests", "tests\Dash.NET.ComponentGeneration.Tests\Dash.NET.ComponentGeneration.Tests.fsproj", "{0F230F90-C64E-4CAA-8B6D-A27353375D56}"
6060
EndProject
61+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Dash.NET.Suave", "src\Dash.NET.Suave\Dash.NET.Suave.fsproj", "{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}"
62+
EndProject
63+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Dash.NET.Giraffe", "src\Dash.NET.Giraffe\Dash.NET.Giraffe.fsproj", "{0FD6384A-19BD-4CF9-8A14-FA5008734024}"
64+
EndProject
65+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Dash.NET.Suave.Example", "examples\Dash.NET.Suave.Example\Dash.NET.Suave.Example.fsproj", "{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}"
66+
EndProject
67+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Dash.NET.Giraffe.Example", "examples\Dash.NET.Giraffe.Example\Dash.NET.Giraffe.Example.fsproj", "{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}"
68+
EndProject
6169
Global
6270
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6371
Debug|Any CPU = Debug|Any CPU
@@ -128,6 +136,54 @@ Global
128136
{0F230F90-C64E-4CAA-8B6D-A27353375D56}.Release|x64.Build.0 = Release|Any CPU
129137
{0F230F90-C64E-4CAA-8B6D-A27353375D56}.Release|x86.ActiveCfg = Release|Any CPU
130138
{0F230F90-C64E-4CAA-8B6D-A27353375D56}.Release|x86.Build.0 = Release|Any CPU
139+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
140+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
141+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Debug|x64.ActiveCfg = Debug|Any CPU
142+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Debug|x64.Build.0 = Debug|Any CPU
143+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Debug|x86.ActiveCfg = Debug|Any CPU
144+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Debug|x86.Build.0 = Debug|Any CPU
145+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
146+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Release|Any CPU.Build.0 = Release|Any CPU
147+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Release|x64.ActiveCfg = Release|Any CPU
148+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Release|x64.Build.0 = Release|Any CPU
149+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Release|x86.ActiveCfg = Release|Any CPU
150+
{6FF28D2B-F6D6-4BC2-8446-53C66065B3EC}.Release|x86.Build.0 = Release|Any CPU
151+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
152+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Debug|Any CPU.Build.0 = Debug|Any CPU
153+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Debug|x64.ActiveCfg = Debug|Any CPU
154+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Debug|x64.Build.0 = Debug|Any CPU
155+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Debug|x86.ActiveCfg = Debug|Any CPU
156+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Debug|x86.Build.0 = Debug|Any CPU
157+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Release|Any CPU.ActiveCfg = Release|Any CPU
158+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Release|Any CPU.Build.0 = Release|Any CPU
159+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Release|x64.ActiveCfg = Release|Any CPU
160+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Release|x64.Build.0 = Release|Any CPU
161+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Release|x86.ActiveCfg = Release|Any CPU
162+
{0FD6384A-19BD-4CF9-8A14-FA5008734024}.Release|x86.Build.0 = Release|Any CPU
163+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
164+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Debug|Any CPU.Build.0 = Debug|Any CPU
165+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Debug|x64.ActiveCfg = Debug|Any CPU
166+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Debug|x64.Build.0 = Debug|Any CPU
167+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Debug|x86.ActiveCfg = Debug|Any CPU
168+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Debug|x86.Build.0 = Debug|Any CPU
169+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Release|Any CPU.ActiveCfg = Release|Any CPU
170+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Release|Any CPU.Build.0 = Release|Any CPU
171+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Release|x64.ActiveCfg = Release|Any CPU
172+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Release|x64.Build.0 = Release|Any CPU
173+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Release|x86.ActiveCfg = Release|Any CPU
174+
{37F6EACC-A7BE-48BF-815B-5E6D2EA6E728}.Release|x86.Build.0 = Release|Any CPU
175+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
176+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
177+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Debug|x64.ActiveCfg = Debug|Any CPU
178+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Debug|x64.Build.0 = Debug|Any CPU
179+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Debug|x86.ActiveCfg = Debug|Any CPU
180+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Debug|x86.Build.0 = Debug|Any CPU
181+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
182+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Release|Any CPU.Build.0 = Release|Any CPU
183+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Release|x64.ActiveCfg = Release|Any CPU
184+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Release|x64.Build.0 = Release|Any CPU
185+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Release|x86.ActiveCfg = Release|Any CPU
186+
{41D83E3B-EF7E-41F3-BB1C-CA963F172BB2}.Release|x86.Build.0 = Release|Any CPU
131187
EndGlobalSection
132188
GlobalSection(SolutionProperties) = preSolution
133189
HideSolutionNode = FALSE

Diff for: dev/Dash.NET.Dev.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26+
<ProjectReference Include="..\src\Dash.NET.Giraffe\Dash.NET.Giraffe.fsproj" />
2627
<ProjectReference Include="..\src\Dash.NET\Dash.NET.fsproj" />
2728
</ItemGroup>
2829
</Project>

Diff for: dev/Docs.fs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Docs
22

33
open Dash.NET
4+
open Dash.NET.Giraffe
45

56
// temporary workaround for checking correctness of the F# snippets in the documentation.
67
// the usual approach does not work here, because we cannot reference Asp.NetCore in a .fsx script.

Diff for: dev/Program.fs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ let dslLayout =
7777
open Newtonsoft.Json
7878
open Newtonsoft.Json.Linq
7979
open Dash.NET.Operators
80+
open Dash.NET.Giraffe
8081

8182
let callbackArrayInput =
8283
Callback.multiOut(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<WarnOn>3390;$(WarnOn)</WarnOn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="Program.fs" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\src\Dash.NET.Giraffe\Dash.NET.Giraffe.fsproj" />
15+
</ItemGroup>
16+
17+
</Project>

Diff for: examples/Dash.NET.Giraffe.Example/Program.fs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
// Learn more about Dash.NET at https://plotly.github.io/Dash.NET/
3+
4+
open Dash.NET.Giraffe
5+
open Dash.NET.Html
6+
open Dash.NET.DCC
7+
open Plotly.NET
8+
open Microsoft.Extensions.Logging
9+
open Giraffe
10+
11+
let myGraph = Chart.Line([(1,1);(2,2)])
12+
13+
let myLayout =
14+
Html.div [
15+
Attr.children [
16+
Html.h1 [
17+
Attr.children "Hello world from Dash.NET and Giraffe!"
18+
];
19+
Html.h2 [ Attr.children "Take a look at this graph:"];
20+
Graph.graph "my-ghraph-id" [Graph.Attr.figure(GenericChart.toFigure myGraph)]
21+
]
22+
]
23+
24+
[<EntryPoint>]
25+
let main argv =
26+
27+
let dashApp =
28+
DashApp.initDefault()
29+
|> DashApp.withLayout myLayout
30+
31+
let config =
32+
{ HostName = "localhost"
33+
; LogLevel = LogLevel.Debug
34+
; ErrorHandler = (fun ex -> text ex.Message) }
35+
36+
DashApp.run [||] config dashApp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<WarnOn>3390;$(WarnOn)</WarnOn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="Program.fs" />
11+
<None Include="Script.fsx" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\Dash.NET.Suave\Dash.NET.Suave.fsproj" />
16+
</ItemGroup>
17+
18+
</Project>

Diff for: examples/Dash.NET.Suave.Example/Program.fs

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
// Learn more about Dash.NET at https://plotly.github.io/Dash.NET/
3+
4+
open Dash.NET.Suave
5+
open Dash.NET.Html
6+
open Dash.NET.DCC
7+
open Plotly.NET
8+
open Dash.NET
9+
open System
10+
open System.Threading
11+
12+
let myGraph = Chart.Line([(1,1);(2,2)])
13+
14+
let myLayout =
15+
Html.div [
16+
Attr.children [
17+
Html.h1 [
18+
Attr.children "Hello world from Dash.NET and Suave!"
19+
];
20+
Html.h2 [ Attr.children "Take a look at this graph:"];
21+
Graph.graph "my-ghraph-id" [Graph.Attr.figure(GenericChart.toFigure myGraph)];
22+
Html.h2 [Attr.children "Tell us something!"];
23+
Input.input "test-input" [Input.Attr.inputType InputType.Text];
24+
Html.h3 [Attr.children "Input below will not trigger the callback"]
25+
Input.input "test-input-state" [Input.Attr.inputType InputType.Text]
26+
Html.div [ Attr.id "test-output" ];
27+
]
28+
]
29+
30+
let testCallback =
31+
Callback.singleOut(
32+
"test-input" @. Value,
33+
"test-output" @. Children,
34+
(fun (input:string) (state:string) ->
35+
"test-output" @. Children => (
36+
sprintf "You said : '%s' and we added the state: '%s'" input state)
37+
),
38+
State = ["test-input-state" @. Value])
39+
40+
let dashApp =
41+
DashApp.initDefault()
42+
|> DashApp.withLayout myLayout
43+
|> DashApp.addCallback testCallback
44+
45+
let config =
46+
{ hostname = "localhost"
47+
; ip = "127.0.0.1"
48+
; port = 0 // Request the server to be launched on a random ephemeral port
49+
; errorHandler = Suave.Web.defaultErrorHandler }
50+
51+
let listening, server = DashApp.runAsync [||] config dashApp
52+
53+
let cts = new CancellationTokenSource()
54+
55+
Async.Start(server, cts.Token)
56+
57+
printfn "Make requests now"
58+
59+
// Capture assigned port
60+
let [| Some startData |] = Async.RunSynchronously listening
61+
let port = startData.binding.port
62+
63+
let url = sprintf "http://%s:%d" config.ip port
64+
65+
Console.WriteLine ("Opening: {0}", url)
66+
67+
// Open browser
68+
let psi = new System.Diagnostics.ProcessStartInfo()
69+
psi.UseShellExecute <- true
70+
psi.FileName <- url
71+
System.Diagnostics.Process.Start(psi) |> ignore
72+
73+
Console.WriteLine("Press any key to exit application")
74+
Console.ReadKey true |> ignore
75+
cts.Cancel()

Diff for: examples/Dash.NET.Suave.Example/Script.fsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Learn more about Dash.NET at https://plotly.github.io/Dash.NET/
2+
3+
#r "bin/Debug/net5.0/Dash.NET.dll"
4+
#r "bin/Debug/net5.0/Suave.dll"
5+
#r "bin/Debug/net5.0/Suave.Experimental.dll"
6+
#r "bin/Debug/net5.0/Dash.NET.Suave.dll"
7+
#r "bin/Debug/net5.0/Plotly.NET.dll"
8+
#r "bin/Debug/net5.0/Feliz.Engine.dll"
9+
#r "bin/Debug/net5.0/Newtonsoft.Json.dll"
10+
11+
open Dash.NET.Suave
12+
open Dash.NET.Html
13+
open Dash.NET.DCC
14+
open Plotly.NET
15+
16+
let myGraph = Chart.Line([(1,1);(2,2)])
17+
18+
let myLayout =
19+
Html.div [
20+
Attr.children [
21+
Html.h1 [
22+
Attr.children "Hello world from Dash.NET and Suave!"
23+
];
24+
Html.h2 [ Attr.children "Take a look at this graph:"];
25+
Graph.graph "my-ghraph-id" [Graph.Attr.figure(GenericChart.toFigure myGraph)]
26+
]
27+
]
28+
29+
let dashApp =
30+
DashApp.initDefault()
31+
|> DashApp.withLayout myLayout
32+
33+
// run
34+
DashApp.run [||] dashApp

Diff for: src/Dash.NET.Giraffe/Dash.NET.Giraffe.fsproj

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
<WarnOn>3390;$(WarnOn)</WarnOn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="Views.fs" />
11+
<Compile Include="DashApp.fs" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Giraffe" Version="5.0.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\Dash.NET\Dash.NET.fsproj" />
20+
</ItemGroup>
21+
22+
</Project>

Diff for: src/Dash.NET/DashApp.fs renamed to src/Dash.NET.Giraffe/DashApp.fs

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Dash.NET
1+
namespace Dash.NET.Giraffe
22

33
open Giraffe
44
open Giraffe.ViewEngine
@@ -14,9 +14,14 @@ open Microsoft.Extensions.Hosting
1414
open Microsoft.Extensions.Logging
1515
open Microsoft.Extensions.DependencyInjection
1616
open System.Reflection
17+
open Dash.NET
1718

18-
[<assembly:AutoOpen("Dash.NET.DCC")>]
19-
do ()
19+
//Giraffe, Logging and ASP.NET specific
20+
type DashGiraffeConfig = {
21+
HostName: string
22+
LogLevel: LogLevel
23+
ErrorHandler: Exception -> HttpHandler
24+
}
2025

2126
type DashApp =
2227
{
@@ -164,7 +169,7 @@ type DashApp =
164169
setStatusCode 404 >=> text "Not Found"
165170
]
166171

167-
static member run (args: string []) (app: DashApp) =
172+
static member run (args: string []) (config: DashGiraffeConfig) (app: DashApp) =
168173
//Go through an assembly and look for any types that inherit DashComponent
169174
//if one is found then look for the LoadableComponentDefinition and add its script
170175
let tryLoadComponents (innerApp: DashApp) (a: Assembly) =
@@ -213,34 +218,34 @@ type DashApp =
213218

214219
//TODO: make this customizable
215220
let errorHandler (ex : Exception) (logger : ILogger) =
216-
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
217-
clearResponse >=> setStatusCode 500 >=> (loadedApp.Config.ErrorHandler ex)
218-
221+
logger.LogError(ex, "An unhandled exception has occurred while executing the request.")
222+
clearResponse >=> setStatusCode 500 >=> (config.ErrorHandler ex)
223+
219224
let configureCors (builder : CorsPolicyBuilder) =
220-
builder.WithOrigins(sprintf "http://%s:8080" loadedApp.Config.HostName)
225+
builder.WithOrigins(sprintf "http://%s:5001" config.HostName)
221226
.AllowAnyMethod()
222227
.AllowAnyHeader()
223228
|> ignore
224-
229+
225230
let configureApp (appBuilder : IApplicationBuilder) =
226231
let env = appBuilder.ApplicationServices.GetService<IWebHostEnvironment>()
227232
(match env.EnvironmentName with
228233
| "Development" -> appBuilder.UseDeveloperExceptionPage()
229234
| _ -> appBuilder.UseGiraffeErrorHandler(errorHandler))
230-
.UseHttpsRedirection()
231-
.UseCors(configureCors)
232-
.UseStaticFiles()
233-
.UseGiraffe(DashApp.toHttpHandler loadedApp)
234-
235+
.UseHttpsRedirection()
236+
.UseCors(configureCors)
237+
.UseStaticFiles()
238+
.UseGiraffe(DashApp.toHttpHandler loadedApp)
239+
235240
let configureServices (services : IServiceCollection) =
236241
services.AddCors() |> ignore
237242
services.AddGiraffe() |> ignore
238-
243+
239244
let configureLogging (builder : ILoggingBuilder) =
240-
builder.AddFilter(fun l -> l.Equals loadedApp.Config.LogLevel)
245+
builder.AddFilter(fun l -> l.Equals config.LogLevel)
241246
.AddConsole()
242247
.AddDebug() |> ignore
243-
248+
244249
// This folder has to be "<path-to-exe>/WebRoot" for the way generated component javascript injection currently works
245250
let contentRoot = Reflection.Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
246251
let webRoot = Path.Combine(contentRoot, "WebRoot")
@@ -257,6 +262,5 @@ type DashApp =
257262
|> ignore)
258263
.Build()
259264
.Run()
260-
261265
0
262266

Diff for: src/Dash.NET/Views.fs renamed to src/Dash.NET.Giraffe/Views.fs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
namespace Dash.NET
1+
namespace Dash.NET.Giraffe
22

33
module Views =
4+
open Dash.NET
45
open Giraffe.ViewEngine
56

67
let createConfigScript (config:DashConfig) =

0 commit comments

Comments
 (0)