liveBook · Manning

FSharp.Data

As explained in F# in Action: A Revised Edition of Get Programming with F#

FSharp.Data is an F# and .NET library that provides type providers and data-access abstractions for common external data formats and sources.

FSharp.Data provides F# type providers for working with common external data formats and sources. It can generate usable types from CSV, JSON, or HTML, making data ingestion and exploration concise without requiring manually defined classes. section 10.7

An HTML type provider can treat a local file or live web page as a schema. The generated API exposes tables and rows directly, so a Wikipedia table can become a programmatic dataset with only a few lines of F#. section 10.3.3

Reading a table and calculating density

type LondonBoroughs = HtmlProvider<"https://en.wikipedia.org/wiki/List_of_London_boroughs">\nlet boroughs = LondonBoroughs.GetSample().Tables.``List of boroughs and local authoritiesEdit``\n\nlet density =\n boroughs.Rows\n |> Array.map (fun row ->\n row.Borough,\n row.``Population (2019 est)`` / row.``Area (sq mi)``\n )\n |> Array.sortByDescending snd\n |> Chart.Column\n |> Chart.show section 10.3.3 section 10.4

The resulting array of tuples is enough to pass data into Plotly.NET: neither library requires custom interfaces or classes. Standard F# arrays, collection functions, pipelines, and lambdas connect ingestion, calculation, and presentation. section 10.4

Listing 10.5 Using the HTML type provider
#r "nuget:FSharp.Data"
open FSharp.Data
 
type LondonBoroughs =
    HtmlProvider<"https://en.wikipedia.org/wiki/
 List_of_London_boroughs">                          #1
 
let boroughs =
    LondonBoroughs.GetSample().Tables.
 ``List of boroughs and local authoritiesEdit``     #2
 
boroughs.Rows |> Array.map (fun row -> row.Borough)   #3
#1 Defines a schema based on a live HTML web page
#2 Accesses a specific table
#3 Uses the data to list all boroughs

Inspect the complete HTML type-provider example, including the NuGet reference, generated schema, named table access, and row mapping. section 10.3.3

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Demonstrates generated schemas, table and row access, collection mapping, and visualization, but defers provider internals.
Examples
Uses a Wikipedia London borough table to calculate population density and render a Plotly.NET column chart.
What it assumes
Assumes basic F# arrays, collection functions, pipelines, and lambdas while demonstrating external data access from the beginning.
What you can do after
Build data-exploration scripts that read typed external data, transform rows with pipelines, and visualize results.
How it connects
Connects type providers with CSV, JSON, HTML, F# scripts, data visualization, and Plotly.NET.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.