Skip to content

Commit

Permalink
keep the offline folder, generate for each version
Browse files Browse the repository at this point in the history
  • Loading branch information
pafo committed May 18, 2024
1 parent 1640d4c commit 3b1fe76
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Example entry:
Tag: v2.31.1
URL: https://raw.githubusercontent.com/plotly/plotly.js/v2.31.1/test/plot-schema.json
Path: schemas/v2.31.1/plot-schema.json
Generated: generated/v2.31.1/graph_objects
Generated: generated/v2.31.1
CDN: https://cdn.plot.ly/plotly-2.31.1.min.js
```
Expand Down
11 changes: 6 additions & 5 deletions examples/bar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"fmt"
"path/filepath"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.29.1/graph_objects"
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -32,14 +33,14 @@ func main() {
}

// by default, using the cdn reference, downloading the plotly js on demand
grob.ToHtml(fig, "bar.html")
grob.Show(fig)
offline.ToHtml(fig, "bar.html")
offline.Show(fig)

// example for using static assets, which can be embedded into the golang application
abs, err := filepath.Abs("asset/plotly-2.29.1.min.js")
if err != nil {
return
}
grob.ToHtml(fig, "bar.html", grob.FigOptions{HeadContent: fmt.Sprintf(`<title>Offline Bars</title><script src="%s"></script>`, abs)})
grob.Show(fig, grob.FigOptions{HeadContent: fmt.Sprintf(`<title>Offline Bars</title><script src="%s"></script>`, abs)})
offline.ToHtml(fig, "bar.html", offline.FigOptions{HeadContent: fmt.Sprintf(`<title>Offline Bars</title><script src="%s"></script>`, abs)})
offline.Show(fig, offline.FigOptions{HeadContent: fmt.Sprintf(`<title>Offline Bars</title><script src="%s"></script>`, abs)})
}
5 changes: 3 additions & 2 deletions examples/bar_custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/lucasb-eyer/go-colorful"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -77,8 +78,8 @@ func main() {
Layout: layout,
}

grob.ToHtml(fig, "bar_custom.html")
grob.Show(fig)
offline.ToHtml(fig, "bar_custom.html")
offline.Show(fig)
}

func toString(in []int) []string {
Expand Down
3 changes: 2 additions & 1 deletion examples/colorscale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

type ColorScale struct {
Expand Down Expand Up @@ -65,7 +66,7 @@ func main() {
},
},
}
grob.Show(fig)
offline.Show(fig)
}

func linspace(start, stop float64, points int) []float64 {
Expand Down
5 changes: 3 additions & 2 deletions examples/range_slider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-gota/gota/dataframe"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -126,8 +127,8 @@ func main() {
},
}

grob.ToHtml(fig, "range_slider.html")
grob.Show(fig)
offline.ToHtml(fig, "range_slider.html")
offline.Show(fig)
}

// Button is a custom type for the cases where the autogenerated code falls short.
Expand Down
5 changes: 3 additions & 2 deletions examples/responsive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -31,6 +32,6 @@ func main() {
},
}

grob.ToHtml(fig, "bar.html")
grob.Show(fig)
offline.ToHtml(fig, "bar.html")
offline.Show(fig)
}
5 changes: 3 additions & 2 deletions examples/scatter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -47,8 +48,8 @@ func main() {
},
}

grob.ToHtml(fig, "scatter.html")
grob.Show(fig)
offline.ToHtml(fig, "scatter.html")
offline.Show(fig)
}

func linspace(start, stop float64, points int) []float64 {
Expand Down
5 changes: 3 additions & 2 deletions examples/scatter3d/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -37,8 +38,8 @@ func main() {
},
}

grob.ToHtml(fig, "scatter3d.html")
grob.Show(fig)
offline.ToHtml(fig, "scatter3d.html")
offline.Show(fig)
}

func linspace(start, stop float64, points int) []float64 {
Expand Down
5 changes: 3 additions & 2 deletions examples/shapes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -58,8 +59,8 @@ func main() {
},
}

grob.ToHtml(fig, "bar.html")
grob.Show(fig)
offline.ToHtml(fig, "bar.html")
offline.Show(fig)
}

type LineShape struct {
Expand Down
5 changes: 3 additions & 2 deletions examples/subplots_share_axes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -93,6 +94,6 @@ func main() {
},
}

grob.ToHtml(fig, "subplots_share_axes.html")
grob.Show(fig)
offline.ToHtml(fig, "subplots_share_axes.html")
offline.Show(fig)
}
5 changes: 3 additions & 2 deletions examples/transforms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand All @@ -28,6 +29,6 @@ func main() {
},
}

grob.ToHtml(fig, "bar.html")
grob.Show(fig)
offline.ToHtml(fig, "bar.html")
offline.Show(fig)
}
3 changes: 2 additions & 1 deletion examples/unmarshal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand All @@ -30,7 +31,7 @@ func main() {
// Demonstrates that a json representation can be loaded
recoveredFig := &grob.Fig{}
json.Unmarshal(bytes, recoveredFig)
grob.Show(recoveredFig)
offline.Show(recoveredFig)
}

func linspace(start, stop float64, points int) []float64 {
Expand Down
5 changes: 3 additions & 2 deletions examples/waterfall_bar_chart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down Expand Up @@ -208,8 +209,8 @@ func main() {
Layout: layout,
}

grob.ToHtml(fig, "waterfall.html")
grob.Show(fig)
offline.ToHtml(fig, "waterfall.html")
offline.Show(fig)
}

type Annotation struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grob
package offline

import (
"bytes"
Expand All @@ -12,6 +12,8 @@ import (

"github.com/pkg/browser"
"github.com/pkg/errors"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.19.0/graph_objects"
)

// FigOptions enables users to pass their custom cdn or local plotly file reference to allow building offline solutions
Expand All @@ -25,22 +27,22 @@ type Options struct {
}

// 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) {
func ToWriter(fig *grob.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) {
func ToHtml(fig *grob.Fig, path string, options ...FigOptions) {
buf := figToBuffer(fig, options...)
os.WriteFile(path, buf.Bytes(), os.ModePerm)
}

// Show displays the figure in your browser.
// Use serve if you want a persistent view
func Show(fig *Fig, options ...FigOptions) {
func Show(fig *grob.Fig, options ...FigOptions) {
buf := figToBuffer(fig, options...)
browser.OpenReader(buf)
}
Expand All @@ -59,7 +61,7 @@ func computeFigOptions(options ...FigOptions) string {
}

// figToBuffer with optional parameter options, only the first argument is evaluated
func figToBuffer(fig *Fig, options ...FigOptions) *bytes.Buffer {
func figToBuffer(fig *grob.Fig, options ...FigOptions) *bytes.Buffer {
var headContent string = computeFigOptions(options...)

figBytes, err := json.Marshal(fig)
Expand All @@ -77,7 +79,7 @@ func figToBuffer(fig *Fig, options ...FigOptions) *bytes.Buffer {

// Serve creates a local web server that displays the image using plotly.js
// Is a good alternative to Show to avoid creating tmp files.
func Serve(fig *Fig, opt ...Options) {
func Serve(fig *grob.Fig, opt ...Options) {
opts := computeOptions(Options{
FigOptions: FigOptions{HeadContent: cdnUrl},
Addr: "localhost:8080",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grob
package offline

import (
"bytes"
Expand All @@ -12,6 +12,8 @@ import (

"github.com/pkg/browser"
"github.com/pkg/errors"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.29.1/graph_objects"
)

// FigOptions enables users to pass their custom cdn or local plotly file reference to allow building offline solutions
Expand All @@ -25,22 +27,22 @@ type Options struct {
}

// 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) {
func ToWriter(fig *grob.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) {
func ToHtml(fig *grob.Fig, path string, options ...FigOptions) {
buf := figToBuffer(fig, options...)
os.WriteFile(path, buf.Bytes(), os.ModePerm)
}

// Show displays the figure in your browser.
// Use serve if you want a persistent view
func Show(fig *Fig, options ...FigOptions) {
func Show(fig *grob.Fig, options ...FigOptions) {
buf := figToBuffer(fig, options...)
browser.OpenReader(buf)
}
Expand All @@ -59,7 +61,7 @@ func computeFigOptions(options ...FigOptions) string {
}

// figToBuffer with optional parameter options, only the first argument is evaluated
func figToBuffer(fig *Fig, options ...FigOptions) *bytes.Buffer {
func figToBuffer(fig *grob.Fig, options ...FigOptions) *bytes.Buffer {
var headContent string = computeFigOptions(options...)

figBytes, err := json.Marshal(fig)
Expand All @@ -77,7 +79,7 @@ func figToBuffer(fig *Fig, options ...FigOptions) *bytes.Buffer {

// Serve creates a local web server that displays the image using plotly.js
// Is a good alternative to Show to avoid creating tmp files.
func Serve(fig *Fig, opt ...Options) {
func Serve(fig *grob.Fig, opt ...Options) {
opts := computeOptions(Options{
FigOptions: FigOptions{HeadContent: cdnUrl},
Addr: "localhost:8080",
Expand Down
Loading

0 comments on commit 3b1fe76

Please sign in to comment.