PyJSONCanvas is a Python library for working with JSON Canvas (previously known as Obsidian Canvas) files. It provides a simple and intuitive API for creating, editing, and manipulating canvas objects, nodes, and edges.
You can install PyJSONCanvas using pip:
pip install PyJSONCanvas
Here's a basic example of how to use PyJSONCanvas:
from pyjsoncanvas import Canvas, TextNode, FileNode, Edge, Color
# Create a new canvas
canvas = Canvas(nodes=[], edges=[])
# Add some nodes
text_node = TextNode(x=100, y=100, width=200, height=100, text="Hello, world!")
canvas.add_node(text_node)
file_node = FileNode(x=300, y=100, width=100, height=100, file="/path/to/file.png")
canvas.add_node(file_node)
# Add an edge
edge = Edge(
fromNode=text_node.id,
fromSide="bottom",
toNode=file_node.id,
toSide="top",
color=Color("#FF0000"),
label="Edge 1",
)
canvas.add_edge(edge)
# Save the canvas as JSON
json_str = canvas.to_json()
# Load the canvas from JSON
loaded_canvas = Canvas.from_json(json_str)
# Get a node
node = loaded_canvas.get_node(text_node.id)
# Get connections for a node
connections = loaded_canvas.get_connections(text_node.id)
The Canvas
class is the main container for nodes and edges.
to_json()
: Convert the canvas to a JSON string.from_json(json_str)
: Create a canvas from a JSON string.export(file_path)
: Save the canvas to a file.validate()
: Validate the canvas structure.get_node(node_id)
: Get a node by its ID.get_edge(edge_id)
: Get an edge by its ID.add_node(node)
: Add a node to the canvas.add_edge(edge)
: Add an edge to the canvas.remove_node(node_id)
: Remove a node from the canvas.remove_edge(edge_id)
: Remove an edge from the canvas.get_connections(node_id)
: Get all edges connected to a node.get_edge_nodes(edge_id)
: Get the nodes connected by an edge.get_adjacent_nodes(node_id)
: Get all nodes adjacent to a given node.
PyJSONCanvas supports four types of nodes:
TextNode
: A node containing text.FileNode
: A node representing a file.LinkNode
: A node containing a URL.GroupNode
: A node that can contain other nodes.
All node types inherit from the GenericNode
class and have the following common attributes:
id
: Unique identifier for the node.type
: The type of the node (TextNode, FileNode, LinkNode, or GroupNode).x
: X-coordinate of the node.y
: Y-coordinate of the node.width
: Width of the node.height
: Height of the node.color
: Color of the node (optional).
Additional attributes:
text
: The text content of the node.
Additional attributes:
file
: The path to the file.subpath
: Subpath within the file (optional).
Additional attributes:
url
: The URL of the link.
Additional attributes:
label
: Label for the group (optional).background
: Background image or color (optional).backgroundStyle
: Style of the background (cover, ratio, or repeat).
Edges represent connections between nodes.
id
: Unique identifier for the edge.fromNode
: ID of the source node.toNode
: ID of the target node.fromSide
: Side of the source node where the edge starts (top, right, bottom, left).toSide
: Side of the target node where the edge ends (top, right, bottom, left).fromEnd
: Style of the edge start (none or arrow).toEnd
: Style of the edge end (none or arrow).color
: Color of the edge.label
: Label for the edge (optional).
Colors can be specified using either hex codes or preset values:
color1 = Color("#FF0000") # Red using hex code
color2 = Color("4") # Green using preset value
PyJSONCanvas defines several custom exceptions to handle various error scenarios. Here's a complete list of exceptions:
JsonCanvasException
: Base exception for all JsonCanvas errors.InvalidJsonError
: Raised when the JSON is invalid or malformed.NodeNotFoundError
: Raised when a specified node is not found.EdgeNotFoundError
: Raised when a specified edge is not found.InvalidNodeTypeError
: Raised when a node has an invalid or unsupported type.InvalidNodeAttributeError
: Raised when a node attribute is invalid or missing.InvalidEdgeConnectionError
: Raised when an edge's connection points are invalid.InvalidEdgeAttributeError
: Raised when an edge attribute is invalid or missing.NodeIDConflictError
: Raised when two nodes have the same ID.EdgeIDConflictError
: Raised when two edges have the same ID.InvalidColorValueError
: Raised when a color value is invalid or not supported.InvalidPositionError
: Raised when a node's position is outside the allowed range.InvalidDimensionError
: Raised when a node's dimensions are invalid.InvalidGroupOperationError
: Raised for invalid operations on group nodes.OrphanEdgeError
: Raised when an edge refers to a non-existent node.UnsupportedFileTypeError
: Raised when a file node points to an unsupported file type.FileReadError
: Raised when there is an error reading a file.FileWriteError
: Raised when there is an error writing to a file.RenderingError
: Raised when there is an error during rendering.CanvasValidationError
: Raised when the canvas does not pass validation checks.
This documentation provides an overview of the PyJSONCanvas library. For more detailed information about specific classes, methods, and attributes, please refer to the docstrings and comments in the source code.