Creating a canvas programmatically

Create a canvas programmatically

I’m looking for a way to create a folder with markdown files and canvases that link/diagram the relationships between fhe files.

The markdown files are created from XML which defines the relationships.

Things I have tried

I’ve studied the raw canvas file. I get everything structured except the id.
I tried using the file name without the extension - that failed.
I tried creating a uuid without dashes referencing an existing file - that failed.

It’s clear a valid uuid is required. It’s unclear where that uuid is created.

The question boils down to - how do you create valid uuid’s that can be used in a canvas file?

I’m not sure of having understood the problem.

The “id” field is only a string which is required to be unique, but no structure is imposed on it. Even a string like “foobar” would be valid (tested). Of course a simple way of ensuring unicity is to generate one via some library (python comes with uuid module). But I guess you can also programatically generate autoincremental numbers and generate strings like “node-1”, “node-2”, etc.

I tried using the file name without the extension - that failed.

This is the way to go. The following mini-example shows how to create that file reference:

{
        "nodes":[
                {"id":"node-1",
                  "x":19,"y":287,"width":400,"height":400,
                  "type":"file","file":"First Note.md"},
                {"id":"node-2",
                  "x":600,"y":287,"width":400,"height":400,
                  "type":"file","file":"Second Note.md"}
        ],
        "edges":[
                {"id":"edge-1-2",
                 "fromNode":"node-1","fromSide":"right",
                 "toNode":"node-2","toSide":"left"}
        ]
}

(Note that I made up the ids, in this case having some structural meaning instead of being random. As long as they are unique strings, they should work)

This is how this example it is rendered in Obsidian (assuming that files “First Note.md” and “Second Note.md” do exist in the vault, and have that content):

The Canvas specs (https://jsoncanvas.org/spec/1.0/) only specify that the id should be unique.

  • id (required, string) is a unique ID for the node.
  • id (required, string) is a unique ID for the edge.

In my reading, ids only need to be unique within a Canvas, not beyond that.

Thanks. It failed for me at first but I got something on the canvas saying it could not find to file and had a file picker.

I added the parent directory and voila!

Cheers and thanks!

Keith