-
Notifications
You must be signed in to change notification settings - Fork 528
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
Creating d2 files programmatically questions #2348
Comments
The tests for our programmatic API is probably the most extensive out of any subpackage in D2. You'll find examples for anything you're looking to do with the API there: https://github.com/alixander/d2/blob/master/d2oracle/edit_test.go For example, for |
Thanks for you help. I see that the package has been updated quite a lot since the blog post was written . I updated my
I can't see how the board is being created in the test cases in this file you linked to: https://github.com/alixander/d2/blob/master/d2oracle/edit_test.go package main
import (
"context"
"fmt"
"os"
"path/filepath"
"oss.terrastruct.com/d2/d2format"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2oracle"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
"oss.terrastruct.com/util-go/go2"
)
func layoutResolver(engine string) (d2graph.LayoutGraph, error) {
return d2dagrelayout.DefaultLayout, nil
}
func main() {
ctx := context.Background()
ruler, _ := textmeasure.NewRuler()
opts := &d2lib.CompileOptions{
Ruler: ruler,
LayoutResolver: layoutResolver,
Layout: go2.Pointer("dagre"),
}
_, graph, _ := d2lib.Compile(ctx, "", opts, nil)
var err error
dbs := []string{"child_a", "child_b"}
for _, db := range dbs {
graph, err = createGraph(db, graph)
if err != nil {
log.Error(ctx, err.Error())
}
}
var out []byte
out, err = RenderGraph(graph, ruler)
if err == nil {
err = os.WriteFile(filepath.Join("svgs", "database.svg"), out, 0600)
if err != nil {
log.Error(ctx, err.Error())
}
err = os.WriteFile("out.d2", []byte(d2format.Format(graph.AST)), 0600)
if err != nil {
log.Error(ctx, err.Error())
}
} else {
log.Error(ctx, err.Error())
}
}
func createGraph(db string, g *d2graph.Graph) (*d2graph.Graph, error) {
g, _, err := d2oracle.Create(g, []string{"x"}, db)
if err != nil {
return nil, err
}
return d2oracle.Set(g, []string{"x"}, fmt.Sprintf("%s.style.stroke-width", db), nil, go2.Pointer(`3`))
}
func RenderGraph(graph *d2graph.Graph, ruler *textmeasure.Ruler) ([]byte, error) {
script := d2format.Format(graph.AST)
opts := &d2lib.CompileOptions{
Ruler: ruler,
LayoutResolver: layoutResolver,
Layout: go2.Pointer("dagre"),
}
diagram, _, _ := d2lib.Compile(context.Background(), script, opts, nil)
return d2svg.Render(diagram, &d2svg.RenderOpts{})
} |
Below is a simplified version of what I'm trying to create. Ultimately I want to loop through a JSON list of objects and dynamically create the diagram.
|
Hi @alixander just wondering if you saw my last comments? If you give me some pointers I'd be happy writing an updated blog post to showcase the new API and the extra functionality as I'm sure others will likely benefit from it. |
Hi, to create a board in root:
After, to add things to that, you use that as the board path
|
I've been trying to create d2 files programmatically based on code from your blog post.
Summary
What I can do
See below for code.
child_a.class: db
)What I haven't been able to do
child_a -> child_b
)child_a: {link: layers.roles}
Want
This is an example of what I'm trying to generate. Could you help steer me in the right direction?
Got
This is the best I've come up with so far.
Source
Output d2 file
The text was updated successfully, but these errors were encountered: