Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-sizeable show function #20

Open
rijfas opened this issue Aug 10, 2021 · 0 comments
Open

re-sizeable show function #20

rijfas opened this issue Aug 10, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@rijfas
Copy link

rijfas commented Aug 10, 2021

it's kinda weird to set a static 300*300 dimension to the sketches, can we change the show() method and SVG class to have a width, height property (with default values 300 of course!) ?
Here is an example:

class SVG:
    """SVG renders any svg element into an svg image.
    """
    def __init__(self, nodes, width=300, height=300):
        self.nodes = nodes
        self.width = width
        self.height = height

    def render(self):
        attrs = {
            "tag": "svg",
            "width": self.width,
            "height": self.height,
            "viewBox": f"-{self.width//2} -{self.height//2} {self.width} {self.height}",
            "fill": "none",
            "stroke": "black",
            "xmlns": "http://www.w3.org/2000/svg",
            "xmlns:xlink": "http://www.w3.org/1999/xlink"
        }
        svg_header = render_tag(**attrs)+ "\n"
        svg_footer = "</svg>\n"

        # flip the y axis so that y grows upwards
        node = Group(self.nodes) | Scale(sx=1, sy=-1)

        return svg_header + node._svg() + svg_footer

    def _repr_svg_(self):
        return self.render()

    def __str__(self):
        return self.render()

    def __repr__(self):
        return "SVG:{self.nodes}"

def show(*shapes, width=300, height=300):
    """Shows the given shapes.

    It also adds a border to the canvas and axis at the origin with
    a light color as a reference.

    Parameters:

        shapes:
            The shapes to show.

    Examples:

    Show a circle:

        >>> show(circle())

    Show a circle and square.

        >>> c = circle()
        >>> s = rect()
        >>> show(c, s)
    """
    markers = [
        Rectangle(width=width, height=height, stroke="#ddd"),
        Line(start=Point(x=-(width//2), y=0), end=Point(x=(width//2), y=0), stroke="#ddd"),
        Line(start=Point(x=0, y=-(height//2)), end=Point(x=0, y=(height//2)), stroke="#ddd")
    ]
    shapes = markers + list(shapes)
    img = SVG(shapes, width=width, height=height)

    from IPython.display import display
    display(img)
@rijfas rijfas changed the title Re-sizeable show function re-sizeable show function Aug 10, 2021
@anandology anandology added the enhancement New feature or request label Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants