Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ See the [tests](/tests/test_d2) for more detailed usage examples.

Names and labels that D2 cannot read bare (such as `a.b`, `x: y`, or the keyword `shape`) are quoted for you. Connection endpoints are written as given, so they can be dotted paths.
Style values you set are always written, even `0` or `False`. `None` means unset.
Colors are checked when the `Style` is built. Pass a CSS color name, `transparent`, a 3 or 6 digit hex code with or without `#`, a `linear-gradient(...)`, a `radial-gradient(...)`, a `${variable}` or a `d2.Color`. Hex codes are lowercased and written quoted, and anything else raises `ValueError`.
Numbers that D2 limits are checked too: `opacity` 0 to 1, `stroke_width` 0 to 15, `stroke_dash` 0 to 10, `font_size` 8 to 100, and non-negative `border_radius`, grid gaps, `top` and `left`.

```python
import d2
Expand Down
2 changes: 2 additions & 0 deletions src/d2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ._render import D2Error, fmt, render, validate
from ._syntax import Color
from .class_shape import ClassField, ClassMethod, ClassShape
from .connection import Arrowhead, ArrowheadShape, Connection, Direction
from .diagram import Config, Diagram, Layer, Legend
Expand All @@ -12,6 +13,7 @@
"ClassField",
"ClassMethod",
"ClassShape",
"Color",
"Config",
"Connection",
"D2Error",
Expand Down
13 changes: 13 additions & 0 deletions src/d2/_limits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import TypeVar

Number = TypeVar("Number", bound=float)


def bounded(
name: str, value: Number | None, low: float, high: float | None = None
) -> Number | None:
if value is None or (value >= low and (high is None or value <= high)):
return value
if high is None:
raise ValueError(f"{name} must be at least {low}, got {value}")
raise ValueError(f"{name} must be between {low} and {high}, got {value}")
4 changes: 2 additions & 2 deletions src/d2/_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import subprocess
from typing import Literal

from d2.diagram import Diagram
from d2.diagram import Diagram, LayoutEngine

RenderFormat = Literal["svg", "png", "pdf", "txt", "gif", "pptx"]

Expand Down Expand Up @@ -48,7 +48,7 @@ def render(
diagram: Diagram | str,
format: RenderFormat = "svg",
*,
layout: Literal["dagre", "elk", "tala"] | None = None,
layout: LayoutEngine | None = None,
theme: int | None = None,
target: str = "",
dark_theme: int | None = None,
Expand Down
Loading
Loading