SVG rendering
The chess.svg module renders SVG Tiny 1.2 images
(mostly for IPython/Jupyter Notebook integration).
The piece images by
Colin M.L. Burnett are triple
licensed under the GFDL, BSD and GPL.
- chess.svg.piece(piece: Piece, size: int | None = None) str[source]
Renders the given
chess.Pieceas an SVG image.>>> import chess >>> import chess.svg >>> >>> chess.svg.piece(chess.Piece.from_symbol("R"))
- chess.svg.board(board: BaseBoard | None = None, *, orientation: chess.Color = True, lastmove: Move | None = None, check: chess.Square | None = None, arrows: Iterable[Arrow | Tuple[chess.Square, chess.Square]] = [], fill: Dict[chess.Square, str] = {}, squares: chess.IntoSquareSet | None = None, size: int | None = None, coordinates: bool = True, colors: Dict[str, str] = {}, borders: bool = False, style: str | None = None) str[source]
Renders a board with pieces and/or selected squares as an SVG image.
- Parameters:
board – A
chess.BaseBoardfor a chessboard with pieces, orNone(the default) for a chessboard without pieces.orientation – The point of view, defaulting to
chess.WHITE.lastmove – A
chess.Moveto be highlighted.check – A square to be marked indicating a check.
arrows – A list of
Arrowobjects, like[chess.svg.Arrow(chess.E2, chess.E4)], or a list of tuples, like[(chess.E2, chess.E4)]. An arrow from a square pointing to the same square is drawn as a circle, like[(chess.E2, chess.E2)].fill – A dictionary mapping squares to a colors that they should be filled with.
squares – A
chess.SquareSetwith selected squares to mark with an X.size – The size of the image in pixels (e.g.,
400for a 400 by 400 board), orNone(the default) for no size limit.coordinates – Pass
Falseto disable the coordinate margin.colors – A dictionary to override default colors. Possible keys are
square light,square dark,square light lastmove,square dark lastmove,margin,coord,inner border,outer border,arrow green,arrow blue,arrow red, andarrow yellow. Values should look like#ffce9e(opaque), or#15781B80(transparent).borders – Pass
Trueto enable a border around the board and, (if coordinates is enabled) the coordinate margin.style – A CSS stylesheet to include in the SVG image.
>>> import chess >>> import chess.svg >>> >>> board = chess.Board("8/8/8/8/4N3/8/8/8 w - - 0 1") >>> >>> chess.svg.board( ... board, ... fill=dict.fromkeys(board.attacks(chess.E4), "#cc0000cc"), ... arrows=[chess.svg.Arrow(chess.E4, chess.F6, color="#0000cccc")], ... squares=chess.SquareSet(chess.BB_DARK_SQUARES & chess.BB_FILE_B), ... size=350, ... )
- class chess.svg.Arrow(tail: chess.Square, head: chess.Square, *, color: str = 'green')[source]
Details of an arrow to be drawn.
- pgn() str[source]
Returns the arrow in the format used by
[%csl ...]and[%cal ...]PGN annotations, e.g.,Ga1orYa2h2.Colors other than
red,yellow, andbluedefault to green.
- classmethod from_pgn(pgn: str) Arrow[source]
Parses an arrow from the format used by
[%csl ...]and[%cal ...]PGN annotations, e.g.,Ga1orYa2h2.Also allows skipping the color prefix, defaulting to green.
- Raises:
ValueErrorif the format is invalid.