Segments of a custom Schemdraw Element are not rendered

Viewed 57

My aim is to create a custom SchemDraw Element. I am following the Defining custom elements documentation. SchemDraw is version 0.14, Python is version 3.9, matplotlib is 3.5.1, I am running the code in Jupyter 6.4.7 and the OS is Windows 10.

Here's my MWE:

import schemdraw 
print("SchemDraw version is {}".format(schemdraw.__version__))
from schemdraw.elements import Element
from schemdraw.segments import *
class Flash(Element):
    def __init_(self, *d, **kwargs):
        super().__init__(*d, **kwargs)
        self.segments.append(Segment([(0,0),(10,0),(10,10),(0,10)]))
d = schemdraw.Drawing()
d += Flash(label="Flash")
d.draw()

Expected: a square labelled with the text "Flash" appears in the Out[n] cell.

Observed: the text "Flash" appears in the Out[n] cell.

The enclosing square does not appear. Experiments with adding other elements around the "Flash" object indicate it has zero extent.

Any ideas? Does anyone have a working example of sub-classing schemdraw.elements.Element they could share?

Cheers,
Colin.

1 Answers

You're missing an underscore in the __init__ line, it needs a double-underscore both before and after:

def __init__(self, *d, **kwargs):

With just one underscore, the init function was never run, and the segment never added to your element.

Related