{ "metadata": { "language": "fsharp", "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "#N \"Deedle/1.0.0-alpha2/pre\"\n", "#N \"FSharp.Formatting\"" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "NuGet package: Deedle" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "Referenced: C:\\Github\\ProjectEuler\\notebooks\\packages\\Deedle.1.0.0-alpha2\\lib\\net40\\Deedle.dll" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "NuGet package: FSharp.Formatting" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "Referenced: C:\\Github\\ProjectEuler\\notebooks\\packages\\FSharp.Formatting.2.4.8\\lib\\net40\\CSharpFormat.dll" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "Referenced: C:\\Github\\ProjectEuler\\notebooks\\packages\\FSharp.Formatting.2.4.8\\lib\\net40\\FSharp.CodeFormat.dll" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "Referenced: C:\\Github\\ProjectEuler\\notebooks\\packages\\FSharp.Formatting.2.4.8\\lib\\net40\\FSharp.Literate.dll" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "Referenced: C:\\Github\\ProjectEuler\\notebooks\\packages\\FSharp.Formatting.2.4.8\\lib\\net40\\FSharp.Markdown.dll" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "Referenced: C:\\Github\\ProjectEuler\\notebooks\\packages\\FSharp.Formatting.2.4.8\\lib\\net40\\FSharp.MetadataFormat.dll" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "module DeedleFormatter =\n", " open System.IO\n", " open Deedle\n", " open Deedle.Internal\n", " open FSharp.Literate\n", " open FSharp.Markdown\n", " \n", " // --------------------------------------------------------------------------------------\n", " // Implements Markdown formatters for common FsLab things - including Deedle series\n", " // and frames, F# Charting charts and System.Image values\n", " // --------------------------------------------------------------------------------------\n", " // How many columns and rows from frame should be rendered\n", " let startColumnCount = 3\n", " let endColumnCount = 3\n", " let startRowCount = 8\n", " let endRowCount = 4\n", " // How many items from a series should be rendered\n", " let startItemCount = 5\n", " let endItemCount = 3\n", " \n", " // --------------------------------------------------------------------------------------\n", " // Helper functions etc.\n", " // --------------------------------------------------------------------------------------\n", " /// Extract values from any series using reflection\n", " let (|SeriesValues|_|) (value : obj) = \n", " let iser = value.GetType().GetInterface(\"ISeries`1\")\n", " if iser <> null then \n", " let keys = \n", " value.GetType().GetProperty(\"Keys\").GetValue(value) :?> System.Collections.IEnumerable\n", " let vector = value.GetType().GetProperty(\"Vector\").GetValue(value) :?> IVector\n", " Some(Seq.zip (Seq.cast keys) vector.ObjectSequence)\n", " else None\n", " \n", " /// Format value as a single-literal paragraph\n", " let formatValue def = \n", " function \n", " | Some v -> [ Paragraph [ Literal(v.ToString()) ] ]\n", " | _ -> [ Paragraph [ Literal def ] ]\n", " \n", " /// Format body of a single table cell\n", " let td v = [ Paragraph [ Literal v ] ]\n", " \n", " /// Use 'f' to transform all values, then call 'g' with Some for \n", " /// values to show and None for \"...\" in the middle\n", " let mapSteps (startCount, endCount) f g input = \n", " input\n", " |> Seq.map f\n", " |> Seq.startAndEnd startCount endCount\n", " |> Seq.map (function \n", " | Choice1Of3 v | Choice3Of3 v -> g (Some v)\n", " | _ -> g None)\n", " |> List.ofSeq\n", " \n", " // Tuples with the counts, for easy use later on\n", " let fcols = startColumnCount, endColumnCount\n", " let frows = startRowCount, endRowCount\n", " let sitms = startItemCount, endItemCount\n", " \n", " let getHtml (value : obj) = \n", " match value with\n", " | SeriesValues s -> \n", " // Pretty print series!\n", " let heads = \n", " s |> mapSteps sitms fst (function \n", " | Some k -> td (k.ToString())\n", " | _ -> td \" ... \")\n", " \n", " let row = \n", " s |> mapSteps sitms snd (function \n", " | Some v -> formatValue \"N/A\" (OptionalValue.asOption v)\n", " | _ -> td \" ... \")\n", " \n", " let aligns = s |> mapSteps sitms id (fun _ -> AlignDefault)\n", " [ InlineBlock \"
\"\n", " TableBlock(Some((td \"Keys\") :: heads), AlignDefault :: aligns, [ (td \"Values\") :: row ])\n", " InlineBlock \"
\" ]\n", " |> Some\n", " | :? IFrame as f -> \n", " { // Pretty print frame!\n", " new IFrameOperation<_> with\n", " member x.Invoke(f) = \n", " let heads = \n", " f.ColumnKeys |> mapSteps fcols id (function \n", " | Some k -> td (k.ToString())\n", " | _ -> td \" ... \")\n", " \n", " let aligns = f.ColumnKeys |> mapSteps fcols id (fun _ -> AlignDefault)\n", " \n", " let rows = \n", " f.Rows\n", " |> Series.observationsAll\n", " |> mapSteps frows id (fun item -> \n", " let def, k, data = \n", " match item with\n", " | Some(k, Some d) -> \n", " \"N/A\", k.ToString(), Series.observationsAll d |> Seq.map snd\n", " | Some(k, _) -> \n", " \"N/A\", k.ToString(), f.ColumnKeys |> Seq.map (fun _ -> None)\n", " | None -> \" ... \", \" ... \", f.ColumnKeys |> Seq.map (fun _ -> None)\n", " \n", " let row = \n", " data |> mapSteps fcols id (function \n", " | Some v -> formatValue def v\n", " | _ -> td \" ... \")\n", " \n", " (td k) :: row)\n", " Some [ InlineBlock \"
\"\n", " TableBlock(Some([] :: heads), AlignDefault :: aligns, rows)\n", " InlineBlock \"
\" ] }\n", " |> f.Apply\n", " | _ -> None\n", " \n", " |> function\n", " | Some pars -> \n", " let doc = MarkdownDocument(pars, null)\n", " Markdown.WriteHtml(doc)\n", " | None -> value.ToString()\n", " \n", "\n", "App.AddFsiPrinter(fun (x:obj) -> \n", " App.Kernel.Value.SendDisplayData(\"text/html\", DeedleFormatter.getHtml(box x))\n", " DeedleFormatter.getHtml(box x))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" }, { "html": [], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "open Deedle\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "val it : unit = ()" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "//App.AddFsiPrinter(fun (x:Deedle.IFrame) -> \n", "// App.Kernel.Value.SendDisplayData(\"text/html\", DeedleFormatter.getHtml(box x))\n", "// DeedleFormatter.getHtml(box x))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "open Deedle\n", "\n", "frame [ \"Map\" => series [ 1 => 2; 2 => 3]]" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "

Map

1

2

2

3

\r\n", "\r\n", "\r\n", "
\r\n" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "open Deedle\n", "\n", "series [ 1 => 2; 2 => 3]" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "

Keys

1

2

Values

2

3

\r\n", "\r\n", "\r\n", "
\r\n" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "6" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "6" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }