Download file in Fable - f#

How do I download file in Fable? Let's say I have a CSV data and I want to save it on my machine.
I tried using ts2fable with the Download.js. However, it creates just an interface:
// ts2fable 0.6.1
module rec Download
open Fable.Import
type [<AllowNullLiteral>] IExports =
abstract downloadUrl: title: string * url: string -> unit
abstract downloadBlob: title: string * blob: Browser.Blob -> unit
abstract downloadText: title: string * content: string -> unit
I think there is a way directly import a .js module
let download = Fable.Core.JsInterop.importMember "../src/download.js"
But it doesn't look nice and elegant.

you can use the import function in Fable in order to use the same syntax in JavaScript.
So in your case, it would give something like:
// If the file is in your src folder
let download : Download.IExports = import "*" "./path/to/download.js"
// If you use npm to manage the library
let download : Download.IExports = import "*" "download.js"
And you can use the code like that:
let anchor = Browser.document.createElement("a") :?> Browser.HTMLAnchorElement
anchor.innerText <- "Test download"
anchor.onclick <- fun _ ->
download.downloadText("test.txt", "Hello, this is the content of the file.")
Browser.document.body.appendChild(anchor)
|> ignore
For information, I tested the code under Firefox, Chrome, and Safari. It works for Chrome and Safari but nothing happens under Firefox. I don't know if this is a limitation of Firefox or the library.

Related

How to use results of FSharp.Compiler.Services

I'm trying to build a system that is similar to FsBolero (TryWebassembly), Fable Repl and many more that uses Fsharp.Compiler.Services.
So I expect it is feasible to achieve my goals but I encountered a problem that I hope is only a result of my lack of experience with that realm of software development
I'm implementing a service that gives user the power to write custom algorithms (DSL) in the context of the domain system.
The code to compile come as a plain raw string that is fully correct F# code.
Sample DSL algorithm looks like:
let code = """
module M
open Lifespace
open Lifespace.LocationPricing
let alg (pricing:LocationPricing) =
let x=pricing.LocationComparisions.CityLevel.Transportation
(8.*x.PublicTransportationStation.Data+ x.RailwayStation.Data+ 5.*x.MunicipalBikeStation.Data) / 14.
"""
that code compiles correctly via CompileToDynamicAssembly. I also provided proper reference to my domain *.dll via -r Fsc parameter.
And here comes my problems as next I have the generated dynamic assembly and want to invoke that algorithm.
I do it with reflection (is there any other way?) with
f.Invoke(null, [|arg|]) when arg is of type LocationPricing and comes from main/hosting project reference.
The Invoke doesn't work because I have error:
Cannot cast LocationPricing to LocationPricing
I had the same problem when tried to use F# interactive services, the error was similar:
Cannot cast [A]LocationPricing to [B]LocationPricing
I'm aware I have two same dlls in the context and F# does have extern alias syntax to solve it.
But other mentioned public systems somehow deals with that or I'm doing it wrongly.
I will look at code of Bolero and FableRepl but it will definately take some time to understand the pitfalls.
Update: Full code (Azure Function)
namespace AzureFunctionFSharp
open System.IO
open System.Text
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Extensions.Http
open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Mvc
open Microsoft.Extensions.Logging
open FSharp.Compiler.SourceCodeServices
open Lifespace.LocationPricing
module UserCodeEval =
type CalculationResult = {
Value:float
}
type Error = {
Message:string
}
[<FunctionName("UserCodeEvalSampleLocation")>]
let Run([<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)>] req: HttpRequest, log: ILogger , [<Blob("ranks/short-ranks.json", FileAccess.Read)>] myBlob:Stream)=
log.LogInformation("F# HTTP trigger function processed a request.")
// confirm valid domain dll location
// for a in System.AppDomain.CurrentDomain.GetAssemblies() do
// if a.FullName.Contains("wrometr.lam.to.ranks") then log.LogInformation(a.Location)
// let code = req.Query.["code"].ToString()
// replaced just to show how the user algorithm can looks like
let code =
"""
module M
open Lifespace
open Lifespace.LocationPricing
open Math.MyStatistics
open MathNet.Numerics.Statistics
let alg (pricing:LocationPricing) =
let x= pricing.LocationComparisions.CityLevel.Transportation
(8.*x.PublicTransportationStation.Data+ x.RailwayStation.Data+ 5.*x.MunicipalBikeStation.Data) / 14.
"""
use reader = new StreamReader(myBlob, Encoding.UTF8)
let content = reader.ReadToEnd()
let encode x = LocationPricingStore.DecodeArrayUnpack x
let pricings = encode content
let checker = FSharpChecker.Create()
let fn = Path.GetTempFileName()
let fn2 = Path.ChangeExtension(fn, ".fsx")
let fn3 = Path.ChangeExtension(fn, ".dll")
File.WriteAllText(fn2, code)
let errors, exitCode, dynAssembly =
checker.CompileToDynamicAssembly(
[|
"-o"; fn3;
"-a"; fn2
"-r";#"C:\Users\longer\azure.functions.compiler\bin\Debug\netstandard2.0\bin\MathNet.Numerics.dll"
"-r";#"C:\Users\longer\azure.functions.compiler\bin\Debug\netstandard2.0\bin\Thoth.Json.Net.dll"
// below is crucial and obtained with AppDomain resolution on top, comes as a project reference
"-r";#"C:\Users\longer\azure.functions.compiler\bin\Debug\netstandard2.0\bin\wrometr.lam.to.ranks.dll"
|], execute=None)
|> Async.RunSynchronously
let assembly = dynAssembly.Value
// get one item to test the user algorithm works in the funtion context
let arg = pricings.[0].Data.[0]
let result =
match assembly.GetTypes() |> Array.tryFind (fun t -> t.Name = "M") with
| Some moduleType ->
moduleType.GetMethods()
|> Array.tryFind (fun f -> f.Name = "alg")
|>
function
| Some f -> f.Invoke(null, [|arg|]) |> unbox<float>
| None -> failwith "Function `f` not found"
| None -> failwith "Module `M` not found"
// end of azure function, not important in the problem context
let res = req.HttpContext.Response
match String.length code with
| 0 ->
res.StatusCode <- 400
ObjectResult({ Message = "No Good, Please provide valid encoded user code"})
| _ ->
res.StatusCode <-200
ObjectResult({ Value = result})
**Update: changing data flow **
To move forward I resigned to use domain types in both places. Instead I do all logic in domain assembly and only pass primitives (strings) to reflected invocation. I'm also suprised a lot that caching still works everytime I do compilation on each Azure Function call. I will experiment as well with FSI, in theory it should be faster than reflection but with additional burden to pass parameters to evaluations
In your example, the code that runs inside your dynamically compiled assembly and the code calling it need to share a type LocationPricing. The error you are seeing typically means that you somehow ended up with different assembly loaded in the process that is calling the dynamically compiled code and the code actually running the computation.
It is hard to say exactly why this happened, but you should be able to check whether this is indeed the case by looking at assemblies loaded in the current App Domain. Say that your shared assembly is MyAssembly. You can run:
for a in System.AppDomain.CurrentDomain.GetAssemblies() do
if a.FullName.Contains("MyAssembly") then printfn "%s" a.Location
If you were using F# Interactive Services, then a trick to fix this is to start an FSI session and then send an interaction to the service that loads the assembly from the right place. Something along those lines:
let myAsm = System.AppDomain.CurrentDomain.GetAssemblies() |> Seq.find (fun asm ->
asm.FullName.Contains("MyAssembly"))
fsi.EvalInteraction(sprintf "#r #\"%s\"" myAsm.Location)

Calling ES 3rd party script method from F# Fable

I'm trying to get Fable to compile the following code correctly but am unable to do so:
module AppView
#r "../../../node_modules/fable-core/Fable.Core.dll"
open Fable.Core
open Fable.Import.Browser
open Fable.Core.JsInterop
[<Import("default", from="../../../js/3rd/riot.js")>]
module riot_js =
let mount:((string*obj)->array<obj>) = jsNative
type App
(
tagName:string
,state
,store
) =
member public x.AppTag =
(riot_js?mount ("app", state))
// does not compile: The value or constructor 'riot_js' is not defined.
// (riot_js.mount ("app", state))
// compiles wrongly to: riot_js.mount(["app", this.state]);
Trying riot_js?mount would magically cause riot_js to not exist any more and trying riot_js.mount compiles into riot_js.mount(["app", this.state]);.
Mount does not take one argument but 2 but it either won't transpile or transpile wrong.
For now I have one of the strangest looking solutions:
[<Emit("riot_js")>]
let riot_js (x: int): obj = jsNative
...
((riot_js 1)?mount ("app", state))
This returns an array but again Fable does not let me take the first element in a "normal" way:
((riot_js 1)?mount ("app", state))?[0]
Gives me red on [ with error Unexpected symbol '[' in expression. Expected identifier, '(' or other token.
And
((riot_js 1)?mount ("app", state)).[0]
Gives red on everything with error The field, constructor or member 'Item' is not defined.
The following "works"
((riot_js 1)?mount ("app", state))?``0``
And is compiled to:
riot_js.mount("app", this.state)["0"];
Not the best result someone can get. I'll let this question sit for a while and set a bounty on it for a week or so before opening 2 issues with Fable.
The following seems to compile to the right ES and does not need the ? so it'll be strongly typed.
open Fable.Core.JsInterop
type Riotjs =
{
mount:(System.Func<string,obj,string []>)
}
let riot = (importAll<obj> "../js/3rd/riot.js") :?> Riotjs
let app = riot.mount.Invoke("app",(createObj []))
I set initial state to type obj but can use a strong typed application state as well.
ES generated is:
export var app = riot.mount("app", {});

F# Type Provider dependency resolution - "Could not load file or assembly..."

I am encountering a "Could not load file or assembly... ... The system could not find the file specified" error when trying to consume my type provider.
The error appears on building the consuming application but does not show up as a 'red squiggly' in visual studio prior to the build.
I have copied my TP below but the issue occurs inside the Database.listDbs call and I strongly suspect the issue is not the code below but how I am packaging the dependencies.
I call into the Microsoft.Azure.DocumentDB package which, in turn, depends on Newtonsoft.Json. It is the Newtonsoft.Json package which cannot be found. I am using Paket to manage dependencies and have redirects on.
The full code (including all the paket files) is on github here: https://github.com/stewart-r/AzureDocumentDbTypeProvider/tree/dependency-issue
I found this question which seems very similar but the solution did not make any difference.
My TP code is as follows:
namespace ProviderImplementation
open ProviderImplementation.ProvidedTypes
open Microsoft.FSharp.Core.CompilerServices
open System.Reflection
open System
open Config
open Database
[<TypeProvider>]
type public DocumentDbTypeProvider(config: TypeProviderConfig) as this =
inherit TypeProviderForNamespaces()
let thisAssembly = Assembly.GetExecutingAssembly()
let docDbType = ProvidedTypeDefinition(thisAssembly,namespaceName,"DocumentDbTypeProvider", baseType = Some typeof<obj>)
let initFn (typeName : string) (args : obj []) =
let acProvidedType = ProvidedTypeDefinition(thisAssembly, namespaceName, typeName, baseType = Some typeof<obj>)
acProvidedType.AddMember(ProvidedConstructor(parameters = [], InvokeCode = (fun args -> <## null ##>)))
let getDbProperties () =
Database.listDbs (args.[0] :?> string) (args.[1]:?> string)
|> List.map(fun d -> new ProvidedProperty(d.Name, typeof<string>, IsStatic = true, GetterCode = (fun _ -> <## "Test db name" ##>)))
acProvidedType.AddMembers(getDbProperties())
acProvidedType
let parameters =
[ ProvidedStaticParameter("accountEndPointUri", typeof<string>, String.Empty)
ProvidedStaticParameter("accountKey", typeof<string>, String.Empty)]
do
docDbType.DefineStaticParameters(parameters,initFn)
this.AddNamespace(namespaceName,[docDbType])
[<TypeProviderAssembly>]
do ()
This is a binding redirect issue - you need to handle the BR inside the type provider. Alternatively, you can restrict the dependencies to the minimum version needed by your direct dependency e.g. DocumentDB.
Have you tried making sure that your "TP dependencies are located in the same folder that the TP itself resides in"?
It sounds like you have the same issue as described in this answer:
https://stackoverflow.com/a/33889287/371698 (quote from this answer)

Infer the type information for any arbitrary CSV files?

I want to use the following console program to get the type information (not the data) of Csv type provider. The file name will be passed as a command line argument. However, it seems the CsvProvider<> only accept constant literal.
Is there a way to workaround it? Or is it possible to do it using F# script?
Or can F# compiler service help?
Or is there any other project does this?
open FSharp.Data
open Microsoft.FSharp.Collections
open System
[<Literal>]
let fn = """C:\...\myfile.csv""" // Want to dynamically set the fn from arguments
[<EntryPoint>]
let main argv =
let myFile = CsvProvider<fn>.GetSample()
// The following doesn't work
let fn = argv.[0]
let myFile = CsvProvider<fn>.GetSample()
// code to get type information of myFile
I think you might be misunderstanding the purpose of the CSV type provider - the idea is that you have a representative sample of your data available at compile time (and can use it to guide the type inference). At runtime, you just give it (possibly a different) file with the same format. This gives you a nice way of handling files with known format.
If you want to parse arbitrary CSV files (with different headers etc.) then CSV type provider won't help. However, you can still use the CsvFile type from F# Data which provides a simple CSV parser. Example from the documentation:
// Download the stock prices
let msft = CsvFile.Load("http://ichart.finance.yahoo.com/table.csv?s=MSFT")
// Print the prices in the HLOC format
for row in msft.Rows do
printfn "HLOC: (%s, %s, %s)" (row.GetColumn "High")
(row.GetColumn "Low") (row.GetColumn "Date")
Here, you loose the nice static typing, but you can load file with any format (and then dynamically look at the columns that were available in the file).
Suggested by Tomas, the following F#-Data CSV provider function can be used to resolve the issue.
let data = CsvFile.Load(....)
let inferredProperties =
// InferColumnTypes : inferRows:int
// * missingValues:string []
// * cultureInfo:CultureInfo
// * schema:string
// * assumeMissingValues:bool
// * preferOptionals:bool
// * ?unitsOfMeasureProvider:IUnitsOfMeasureProvider
// -> PrimitiveInferedProperty list
data.InferColumnTypes(10000, [|""|], CultureInfo.InvariantCulture, "", false, true)
Not sure what the parameters should be used. But the above settings seem work OK.

The field, constructor or member 'AsyncReadToEnd' is not defined error

I am learning RX (Reactive Extensions), and have found someone posted some code nearly one year ago using F# and RX to make a simple webCrawler. I tried to see if I can re-use the code. I download RX, and create a F# windows application, add reference to System.Reactive. My IDE is VS 2010 Ultimate, RX version is: 1.1.11111. The following is the code:
#light
open System
open System.Linq
open System.Collections.Generic
open System.Net
open System.IO
open System.Threading
open System.Text.RegularExpressions
open System.Reactive
open System.Reactive.Linq
let create f =
Observable.Create<_>(fun x ->
f x
new System.Action((fun () -> ())))
let ofAsync async =
create (fun obs -> Async.StartWithContinuations(async, obs.OnNext,obs.OnError,obs.OnError))
let fromEvent (event:IEvent<_,_>) = create (fun x -> event.Add x.OnNext)
let tickEvent = new Event<unit> ()
let tickEventObs = tickEvent.Publish |> fromEvent
let fetch(url:string) =
async { let req = WebRequest.Create(url)
let! resp = req.AsyncGetResponse()
let stream = resp.GetResponseStream()
let reader = new StreamReader(stream)
let! html = reader.AsyncReadToEnd()
return html
} |> ofAsync
But the code can not get compiled, I got the error message:
Error 1 The field, constructor or member 'AsyncReadToEnd' is not defined
So the error was on this line:
let! html = reader.AsyncReadToEnd()
I guess there could be some changes for the past one year in RX or F#.
For my current environment, what is the correct way to re-write the above code?
Thanks and happy new year to you all!
John
AsyncReadToEnd() extension method of StreamReader is part of FSharpPowerPack now. Install FSharpPowerPack from this link, if not yet, then add reference to FSharp.PowerPack to your project. This should make AsyncReadToEnd()method accessible from the rest of your code.
Use the dedicated AsyncStreamReader type
AsyncReadToEnd() extension method does not exists anymore in the FSharp.PowerPack.
It has been replaced with the AsyncStreamReader dedicated type that contains proper asynchronous implementation of stream reading (like ReadToEnd, ReadLine, etc.)
It can be used like that:
async {
use asyncReader = new AsyncStreamReader(stream)
return! asyncReader.ReadToEnd() }
Note: Once you have installed FSharp.PowerPack, the AsyncStreamReader type will be 'injected' in the Microsoft.FSharp.Control namespace
Other related answer: https://stackoverflow.com/a/7925440/1480391

Resources