-
Notifications
You must be signed in to change notification settings - Fork 81
/
00_1_image-export.fsx
188 lines (144 loc) · 4.81 KB
/
00_1_image-export.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(**
---
title: Static image export
category: General
categoryindex: 1
index: 2
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: Newtonsoft.JSON, 13.0.1"
#r "nuget: DynamicObj, 1.0.1"
#r "nuget: PuppeteerSharp"
#r "../bin/Plotly.NET/netstandard2.0/Plotly.NET.dll"
#r "../bin/Plotly.NET.ImageExport/netstandard2.0/Plotly.NET.ImageExport.dll"
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, {{fsdocs-package-version}}"
#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}"
#r "nuget: Plotly.NET.ImageExport, {{fsdocs-package-version}}"
#endif // IPYNB
(**
[![Binder]({{root}}img/badge-binder.svg)](https://mybinder.org/v2/gh/plotly/Plotly.NET/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
[![Script]({{root}}img/badge-script.svg)]({{root}}{{fsdocs-source-basename}}.fsx) 
[![Notebook]({{root}}img/badge-notebook.svg)]({{root}}{{fsdocs-source-basename}}.ipynb)
# Static image export
### Table of contents
- [Saving static images](#Saving-static-images)
- [Generating URIs for static chart images](#Generating-URIs-for-static-chart-images)
- [Including static images in dotnet interactive notebooks](#Including-static-images-in-dotnet-interactive-notebooks)
As Plotly.NET generates static html pages that contain charts rendered by plotly.js, static image export needs a lot more overhead under the hood
than you might expect. The underlying renderer needs to execute javascript, leading to the usage of headless browsers.
The package `Plotly.NET.ImageExport` contains extensions for Plotly.NET to render static images. It is designed with extensibility in mind and
it is very easy to add a new rendering engine. The current engines are provided:
| Rendering engine | Type | Prerequisites |
|-|-|-|
| [PuppeteerSharp](https://github.com/hardkoded/puppeteer-sharp) | headless browser | [read more here](https://github.com/hardkoded/puppeteer-sharp#prerequisites) |
## Saving static images
By referencing the `Plotly.NET.ImageExport` package, you get access to:
- jpg via `Chart.SaveJPG`
- png via `Chart.SavePNG`
- svg via `Chart.SaveSVG`
(and Extensions for C# style fluent interfaces by opening the `GenericChartExtensions` namespace)
The parameters for all three functions are exactly the same.
*)
open Plotly.NET
open Plotly.NET.ImageExport
let exampleChart =
Chart.Histogram2DContour(
[1.;2.;2.;4.;5.],
[1.;2.;2.;4.;5.]
)
(***do-not-eval***)
exampleChart
|> Chart.saveJPG(
"/your/path/without/extension/here",
Width=300,
Height=300
)
(*** condition: ipynb ***)
#if IPYNB
let imgString = $"""<img
src= "{exampleChart|> Chart.toBase64JPGString(Width=300,Height=300)}"
/>"""
DisplayExtensions.DisplayAs(imgString,"text/html")
#endif // IPYNB
(***hide***)
$"""<img
src= "{exampleChart|> Chart.toBase64JPGString(Width=300,Height=300)}"
/>"""
(***include-it-raw***)
(**
## Generating URIs for static chart images
By referencing the `Plotly.NET.ImageExport` package, you get access to:
- jpg via `Chart.toBase64JPGString`
- png via `Chart.toBase64PNGString`
- svg via `Chart.toSVGString`
(and Extensions for C# style fluent interfaces by opening the `GenericChartExtensions` namespace)
*)
let base64JPG =
exampleChart
|> Chart.toBase64JPGString(
Width=300,
Height=300
)
(**
It is very easy to construct a html tag that includes this image via a base64 uri. For SVGs,
not even that is necessary and just the SVG string can be used.
*)
(***do-not-eval***)
$"""<img
src= "{base64JPG}"
/>"""
(*** condition: ipynb ***)
#if IPYNB
let imgString = $"""<img
src= "{base64JPG}"
/>"""
DisplayExtensions.DisplayAs(imgString,"text/html")
#endif // IPYNB
(***hide***)
$"""<img
src= "{base64JPG}"
/>"""
(***include-it-raw***)
(**
SVGs can be included without the image tag:
*)
let svgString =
exampleChart
|> Chart.toSVGString(
Width=300,
Height=300
)
svgString.Substring(0,300)
|> printfn "%s"
(***include-output***)
(**
In fact, the images shown on this site are included just the same way.
## Including static images in dotnet interactive notebooks
To include the images in dotnet interactive, convert them to html tags as above and include them via
dotnet interactive's `DisplayAs` function. The content type for PNG/JPG is "text/html", and "image/svg+xml" for SVG.
*)
let base64PNGTag =
let base64 =
exampleChart
|> Chart.toBase64PNGString(
Width=300,
Height=300
)
$"""<img src= "{base64JPG}"/>"""
let svgString2 =
exampleChart
|> Chart.toSVGString(
Width=300,
Height=300
)
// DisplayExtensions.DisplayAs(base64PNG,"text/html")
// DisplayExtensions.DisplayAs(svgString2,"image/svg+xml")
(*** condition: ipynb ***)
#if IPYNB
DisplayExtensions.DisplayAs(base64PNG,"text/html")
DisplayExtensions.DisplayAs(svgString,"image/svg+xml")
#endif // IPYNB