From 1640d4c488ac84b56a73d52aa8e9bc423d025060 Mon Sep 17 00:00:00 2001 From: pafo Date: Sun, 12 May 2024 15:56:18 +0200 Subject: [PATCH] allow redirecting outputstream of create plot --- generated/v2.19.0/graph_objects/plot_gen.go | 9 +++++++++ generated/v2.29.1/graph_objects/plot_gen.go | 9 +++++++++ generated/v2.31.1/graph_objects/plot_gen.go | 9 +++++++++ generator/templates/plot.tmpl | 9 +++++++++ 4 files changed, 36 insertions(+) diff --git a/generated/v2.19.0/graph_objects/plot_gen.go b/generated/v2.19.0/graph_objects/plot_gen.go index f2682c4..154be7d 100644 --- a/generated/v2.19.0/graph_objects/plot_gen.go +++ b/generated/v2.19.0/graph_objects/plot_gen.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "log" "net/http" "os" @@ -23,6 +24,14 @@ type Options struct { Addr string } +// ToWriter saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. +func ToWriter(fig *Fig, w io.Writer, options ...FigOptions) { + _, err := w.Write(figToBuffer(fig, options...).Bytes()) + if err != nil { + panic(errors.New(fmt.Sprintf("failed to write figure to passed writer: %v", err))) + } +} + // ToHtml saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. func ToHtml(fig *Fig, path string, options ...FigOptions) { buf := figToBuffer(fig, options...) diff --git a/generated/v2.29.1/graph_objects/plot_gen.go b/generated/v2.29.1/graph_objects/plot_gen.go index 1a9fef8..a272cc1 100644 --- a/generated/v2.29.1/graph_objects/plot_gen.go +++ b/generated/v2.29.1/graph_objects/plot_gen.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "log" "net/http" "os" @@ -23,6 +24,14 @@ type Options struct { Addr string } +// ToWriter saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. +func ToWriter(fig *Fig, w io.Writer, options ...FigOptions) { + _, err := w.Write(figToBuffer(fig, options...).Bytes()) + if err != nil { + panic(errors.New(fmt.Sprintf("failed to write figure to passed writer: %v", err))) + } +} + // ToHtml saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. func ToHtml(fig *Fig, path string, options ...FigOptions) { buf := figToBuffer(fig, options...) diff --git a/generated/v2.31.1/graph_objects/plot_gen.go b/generated/v2.31.1/graph_objects/plot_gen.go index b61e9c7..b65d00a 100644 --- a/generated/v2.31.1/graph_objects/plot_gen.go +++ b/generated/v2.31.1/graph_objects/plot_gen.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "log" "net/http" "os" @@ -23,6 +24,14 @@ type Options struct { Addr string } +// ToWriter saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. +func ToWriter(fig *Fig, w io.Writer, options ...FigOptions) { + _, err := w.Write(figToBuffer(fig, options...).Bytes()) + if err != nil { + panic(errors.New(fmt.Sprintf("failed to write figure to passed writer: %v", err))) + } +} + // ToHtml saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. func ToHtml(fig *Fig, path string, options ...FigOptions) { buf := figToBuffer(fig, options...) diff --git a/generator/templates/plot.tmpl b/generator/templates/plot.tmpl index 7d28d59..9574310 100644 --- a/generator/templates/plot.tmpl +++ b/generator/templates/plot.tmpl @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "log" "net/http" "os" @@ -23,6 +24,14 @@ type Options struct { Addr string } +// ToWriter saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. +func ToWriter(fig *Fig, w io.Writer, options ...FigOptions) { + _, err := w.Write(figToBuffer(fig, options...).Bytes()) + if err != nil { + panic(errors.New(fmt.Sprintf("failed to write figure to passed writer: %v", err))) + } +} + // ToHtml saves the figure as standalone HTML. It still requires internet to load plotly.js from CDN. func ToHtml(fig *Fig, path string, options ...FigOptions) { buf := figToBuffer(fig, options...)