Skip to main content

Generate schemdraw diagrams from YAML files or Python dictionaries.

Project description

yaml-to-schemdraw

Generate schemdraw diagrams from YAML files or Python dictionaries.

pip install yaml-to-schemdraw

The following YAML spec:

V1:
  - elements
  - SourceV
  - label: ["5V"]

line1:
  - elements
  - Line
  - right: [0.75]

S1:
  - elements
  - SwitchSpdt2: [{ action: close }]
  - up
  - anchor: ["b"]
  - label: ["$t=0$", { loc: rgt }]

line2:
  - elements
  - Line
  - right: [0.75]
  - at: ["S1.c"]

R1:
  - elements
  - Resistor
  - down
  - label: ["$100\\Omega$"]
  - label: [["+", "$v_o$", "-"], { loc: bot }]

line3:
  - elements
  - Line
  - to: ["V1.start"]

C1:
  - elements
  - Capacitor
  - at: ["S1.a"]
  - toy: ["V1.start"]
  - label: ["1$\\mu$F"]
  - dot

Represents the equivalent Python code:

with schemdraw.Drawing() as d:
    V1 = elm.SourceV().label('5V')
    elm.Line().right(d.unit*.75)
    S1 = elm.SwitchSpdt2(action='close').up().anchor('b').label('$t=0$', loc='rgt')
    elm.Line().right(d.unit*.75).at(S1.c)
    elm.Resistor().down().label(r'$100\Omega$').label(['+','$v_o$','-'], loc='bot')
    elm.Line().to(V1.start)
    elm.Capacitor().at(S1.a).toy(V1.start).label(r'1$\mu$F').dot()

And can be loaded with this library as follows:

Usage

from pathlib import Path
from ruamel.yaml import YAML
from yaml_to_schemdraw import from_dict

yaml = YAML()
spec = yaml.load(Path("diagram.yaml").read_text())
diagram = from_dict(spec)  # <- Schemdraw diagram object

You can now call diagram.draw() or diagram.save("diagram.svg") as usual.

Why?

Schemdraw was always intended to be used as a Python library, with developers manually writing diagrams in code.

However, when it comes to accepting diagram definitions provided by clients through the network or originating from an untrusted environment, the naive approach of running arbitrary Python code with a function like exec() poses a significant security risk.

This module proposes an alternative, declarative way to represent Schemdraw diagrams as a YAML file or a Python dictionary.

How it works

The module parses the dictionary and resolves the function calls against the schemdraw library.

Internally, it uses getattr() to resolve function calls, with an attribute whitelist to prevent module escalation.

It can easily and safely parse most of the schemdraw examples library, which was used to generate the included whitelist.

If you encounter any valid diagrams that cannot be parsed (or easily adapted into something that this module can parse), please open an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yaml_to_schemdraw-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yaml_to_schemdraw-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file yaml_to_schemdraw-0.1.0.tar.gz.

File metadata

  • Download URL: yaml_to_schemdraw-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for yaml_to_schemdraw-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6db7d649de6e95f2672427b5d4b9eb77b2a1c1763e32569dc9677bc50cf5563
MD5 3e7f44ffd136005e03f49845b4e4c4c6
BLAKE2b-256 ff3a2df33181ebefe817b0c4e2153c209a076cdca6a008a4f89ae1b825743085

See more details on using hashes here.

File details

Details for the file yaml_to_schemdraw-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: yaml_to_schemdraw-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for yaml_to_schemdraw-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a18782c5ef9d3051ba12d2b71cc5c958e14e479501fdceae12eb364bcf12a3dc
MD5 c573b41f7e9b435a6821d3c067707ee5
BLAKE2b-256 6473aa9fce0090125d7c2b89c9a0caf7e3035a4d3285bd75047592e84a9a4d12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page