Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tooling to generate multiple plotly schemas #18

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7b2a11a
update to plotly version 2.29.0
Feb 9, 2024
29541a0
fix typos
Feb 9, 2024
7cdb932
update to go 1.22, fix review comments
Apr 13, 2024
0571e9c
reduce number of changes
MetalBlueberry Apr 14, 2024
612c969
add downloader script, improve readme
Apr 20, 2024
78a13f9
run go generate
MetalBlueberry May 1, 2024
ee89afd
create definition to support multiple versions of plotly
May 2, 2024
2293e99
update dependencies
May 11, 2024
e60c99f
complete full generation for each version separately
May 11, 2024
2a3e744
update notes
May 11, 2024
da906d6
enable offline plots
May 12, 2024
6aa6e62
allow arrays when the schema definition states 'arrayOK'
May 12, 2024
1492cb7
allow usage of frames
May 12, 2024
1640d4c
allow redirecting outputstream of create plot
May 12, 2024
3b1fe76
keep the offline folder, generate for each version
May 18, 2024
c85ba65
remove array enabling
May 18, 2024
ef4579f
Merge branch 'master' into feature/plotly_v2.29.0
PatrickVienne May 19, 2024
c3272da
fix typo
MetalBlueberry May 22, 2024
e9d5fa0
Refactor
MetalBlueberry May 22, 2024
b452fc1
Test the packages build
MetalBlueberry May 22, 2024
9411066
remove frames
May 24, 2024
49edb2b
remove change on offline package
MetalBlueberry May 25, 2024
1cd3708
cleanup readme
MetalBlueberry May 25, 2024
92f38a9
fix template syntax
MetalBlueberry May 25, 2024
2682f35
regenerate
MetalBlueberry May 25, 2024
8277e26
fix build issue with import path
MetalBlueberry May 25, 2024
f737784
fix CDN
MetalBlueberry May 25, 2024
bfe1851
fix go mod
MetalBlueberry May 25, 2024
4ced1cd
fix examples output
MetalBlueberry May 25, 2024
b55db8a
clarify the purpose of the schema
MetalBlueberry May 25, 2024
b95b94d
fix loading the schema from embed
MetalBlueberry May 25, 2024
b0c12cd
remove unused code
MetalBlueberry May 25, 2024
fd02e0d
readme update
MetalBlueberry May 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 88 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
},
}

offline.Show(fig)
grob.Show(fig)
}
```

Expand Down Expand Up @@ -113,6 +113,92 @@ For strings... This is a little bit more complicated, In AWS package they are us

For numbers... It's similar to strings, Right now you cannot create plots with integer/float numbers with 0 value. I've only encounter problems when trying to remove the margin and can be workaround with an small value like `0.001`. I would like to avoid using interface{} or defining types again to keep the package interface as simple as possible.

### Go Plotly Update to any json schema version

#### Update the config to add a new version

To add a new version, add a new entry in: [schemas.yaml](schemas.yaml)

The documentation for each field can be found in [schema.go](generator%2Fschema.go)

Example entry:
```yaml
- Name: Plotly 2.31.1
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
CDN: https://cdn.plot.ly/plotly-2.31.1.min.js
```

The local paths are relative to the project root.

#### run download and regeneration

> [!TIP]
> Use this script for easier download of plotly json schemas.
> ```shell
> go run generator/cmd/downloader/main.go --config="schemas.yaml"
> ```
> Then run the generator, which will clean up the generated files in **graph_objects** folder of each version and regenerate the new graph objects. DO NOT REMOVE **graph_objects/plotly.go**
> ```shell
> go run generator/cmd/generator/main.go --config="schemas.yaml"
> ```
> Alternatively, you can also generate the go package using following command from the project root:
> ```shell
> go generate ./...
> ```

#### changing schemas.yaml locations

Be aware, that the template file [plotly.tmpl](generator%2Ftemplates%2Fplotly.tmpl) also has a go generate directive.
This directive has relative paths, based on the assumed final path within the project

#### embedding a downloaded plotly js source

In the example: [main.go](examples%2Fbar%2Fmain.go) you find an example to reuse a static plotly js file and pass that reference to the generated html, instead of using a hardcoded CDN reference which would require internet connection.
```go
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)})
```

#### Missing Files?

if in doubt whether all types and traces have been generated, you can use the jsonviewer tool to introspect the json:
https://jsonviewer.stack.hu/

Or use `jq` tool for quick introspection into the json files.
Example:
Display all traces in the schema.json file.
```shell
jq '.schema.traces | keys' schema.json --sort-keys | less
```

More on the `jq` tool on: https://jqlang.github.io/jq/manual/

### plotly online editor sandbox
http://plotly-json-editor.getforge.io/

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=Metalblueberry/go-plotly&type=Date)](https://star-history.com/#Metalblueberry/go-plotly&Date)
[![Star History Chart](https://api.star-history.com/svg?repos=Metalblueberry/go-plotly&type=Date)](https://star-history.com/#Metalblueberry/go-plotly&Date)

## Update Notes:

### Add version v2.29.1 and v2.31.1
- Added Downloader for schema file
- added generator for different packages based on plotly version in subfolders for each version
- enable easier use for different plotly version by new import paths which include the version generated. Example: `grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"`
- removed go generate directive from the templates which are no longer necessary
- removed offline package, as each version package needs its own **plot.go**.
- Replaced all `offline` imports in the examples packages by just calling the `grob` package of the specified version
- all html's will now refer to the correct plotly version's cdn in the html's head
- allow defining your own head for the plot, which allows embedding the plotly.js source instead of referencing the CDN. This enables generation of offline plots
- all types with "`arrayOk: true`" should be defined as `interface{}` to allow the flexibility of javascript and let the user decide whether a single value or a slice is used.

## Official Plotly Release Notes
For detailed changes please follow the release notes of the original JS repo: https://github.com/plotly/plotly.js/releases
8 changes: 8 additions & 0 deletions examples/asset/plotly-2.29.1.min.js

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions examples/bar/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
"github.com/MetalBlueberry/go-plotly/offline"
"fmt"
"path/filepath"

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

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

offline.ToHtml(fig, "bar.html")
offline.Show(fig)
// by default, using the cdn reference, downloading the plotly js on demand
grob.ToHtml(fig, "bar.html")
grob.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)})
}
8 changes: 4 additions & 4 deletions examples/bar_custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"image/color"
"strconv"

grob "github.com/MetalBlueberry/go-plotly/graph_objects"
"github.com/MetalBlueberry/go-plotly/offline"
"github.com/lucasb-eyer/go-colorful"

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

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

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

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

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

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

func linspace(start, stop float64, points int) []float64 {
Expand Down
15 changes: 11 additions & 4 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
module github.com/metalblueberry/plotly/examples

go 1.14
go 1.22

require (
github.com/MetalBlueberry/go-plotly v0.0.0-20200503142240-1276ab260dcb
github.com/go-gota/gota v0.10.1
github.com/MetalBlueberry/go-plotly v0.4.0
github.com/go-gota/gota v0.12.0
github.com/lucasb-eyer/go-colorful v1.2.0
gonum.org/v1/gonum v0.7.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
)

require (
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
)

replace github.com/MetalBlueberry/go-plotly => ./../
Loading
Loading