Stanley Solutions Blog

engineering and creativity - all under one hat

Making Drawing Circuits in Markdown a Cinch!


So, wouldn't it be nice to be able to draw circuits right in your markdown? The same way that PlantUML drawings can be carved out from plain-text.

Sure, but that technology doesn't exist... right?

Hah! That's what you thought...

Enter: schemdraw-markdown, a brand-new tool built by yours truly that can take schemdraw logic embedded in special blocks of markdown and build appropriate SVG circuits to illustrate the circuits, embedding them directly in the markdown!

I can't take all of the credit, there's some good folks who built the plantuml-markdown extension extension, which I've already built into my Pelican-blogsite generation, allowing me to make those awesome little drawings in some of my past articles. That tool was the starting-point for my work. Call it inspiration; call it shameless, code-plundering; they built an excellent framework which I was able to reconfigure to support schemdraw. Either way, those folks built an awesome tool, and made it really easy for me to build something similar.

C'mon, let's show this off!

Want to see it in action? Here's a few samples:

The Standard Schemdraw Intro Diagram:

Schemdraw Basic Diagram

Click to expand all the Schemdraw-Markdown Goodness!
::_schemdraw_:: alt="My super diagram" color="white"
    += elm.Resistor().right().label('1Ω')
    += elm.Capacitor().down().label('10μF')
    += elm.Line().left()
    += elm.SourceSin().up().label('10V')
::end-schemdraw::

Something a bit Juicier:

Example from Schemdraw Docs

Analog Circuit

Click to expand all the Schemdraw-Markdown Goodness!
::_schemdraw_:: alt="Analog Circuit" color="white"
    (V1 := elm.SourceV().label('5V'))
    elm.Line().right(drawing.unit*.75)
    (S1 := elm.SwitchSpdt2(action='close').up().anchor('b').label('$t=0$', loc='rgt'))
    elm.Line().right(drawing.unit*.75).at(S1.c)
    elm.Resistor().down().label('$100\Omega$').label(['+','$v_o$','-'], loc='bot')
    elm.Line().to(V1.start)
    elm.Capacitor().at(S1.a).toy(V1.start).label('1$\mu$F').dot()
::end-schemdraw::

How Does it Work?

In a nutshell, we use some regular expressions to slurp the specially formatted section out of the markdown, and then we do some conditioning for the "instructions" and punch them into a custom exec block. Yeah, that's right, the dreaded exec block. I'm not particularly pleased about it, either, but it could be worse...

right?

Granny's got it...

At any rate, there's no sense crying over that spilled milk, for the time-being, it's simple enough, and doesn't cause too much trouble. I clearly call out a warning in the README docs for the new package. And speaking of the "new package," it's already on PyPI.

To get started with it in your documentation, just go ahead and...

$ pip3 install schemdraw-markdown

And let me know what you think in the comments below!