{"spec_id":"chessboard-pieces","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nchessboard-pieces: Chess Board with Pieces for Position Diagrams\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 85/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\nimport plotly.graph_objects as go\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Chess piece Unicode symbols\nPIECE_SYMBOLS = {\n    \"K\": \"♔\",\n    \"Q\": \"♕\",\n    \"R\": \"♖\",\n    \"B\": \"♗\",\n    \"N\": \"♘\",\n    \"P\": \"♙\",\n    \"k\": \"♚\",\n    \"q\": \"♛\",\n    \"r\": \"♜\",\n    \"b\": \"♝\",\n    \"n\": \"♞\",\n    \"p\": \"♟\",\n}\n\n# Example position: Scholar's Mate (checkmate in 4 moves)\npieces = {\n    \"a8\": \"r\",\n    \"b8\": \"n\",\n    \"c8\": \"b\",\n    \"d8\": \"q\",\n    \"f8\": \"r\",\n    \"g8\": \"n\",\n    \"h8\": \"k\",\n    \"a7\": \"p\",\n    \"b7\": \"p\",\n    \"c7\": \"p\",\n    \"d7\": \"p\",\n    \"f7\": \"Q\",\n    \"g7\": \"p\",\n    \"h7\": \"p\",\n    \"e6\": \"p\",\n    \"f3\": \"N\",\n    \"e4\": \"P\",\n    \"c4\": \"B\",\n    \"a2\": \"P\",\n    \"b2\": \"P\",\n    \"c2\": \"P\",\n    \"d2\": \"P\",\n    \"f2\": \"P\",\n    \"g2\": \"P\",\n    \"h2\": \"P\",\n    \"a1\": \"R\",\n    \"b1\": \"N\",\n    \"c1\": \"B\",\n    \"d1\": \"Q\",\n    \"e1\": \"K\",\n    \"h1\": \"R\",\n}\n\n# Board colors (traditional chess)\nLIGHT_SQUARE = \"#F0D9B5\"\nDARK_SQUARE = \"#B58863\"\n\n# Create figure\nfig = go.Figure()\n\n# Draw board squares\nfor row in range(8):\n    for col in range(8):\n        is_light = (row + col) % 2 == 1\n        color = LIGHT_SQUARE if is_light else DARK_SQUARE\n        fig.add_shape(type=\"rect\", x0=col, y0=row, x1=col + 1, y1=row + 1, fillcolor=color, line={\"width\": 0})\n\n# Add pieces as text annotations\nfor square, piece in pieces.items():\n    col = ord(square[0]) - ord(\"a\")\n    row = int(square[1]) - 1\n    symbol = PIECE_SYMBOLS.get(piece, \"\")\n\n    fig.add_annotation(\n        x=col + 0.5,\n        y=row + 0.5,\n        text=symbol,\n        font={\"size\": 70, \"color\": INK, \"family\": \"Arial\"},\n        showarrow=False,\n        xanchor=\"center\",\n        yanchor=\"middle\",\n    )\n\n# Add file labels (a-h) at bottom\nfor col, file_letter in enumerate(\"abcdefgh\"):\n    fig.add_annotation(\n        x=col + 0.5,\n        y=-0.25,\n        text=file_letter,\n        font={\"size\": 32, \"color\": INK_SOFT},\n        showarrow=False,\n        xanchor=\"center\",\n        yanchor=\"top\",\n    )\n\n# Add rank labels (1-8) on left\nfor row in range(8):\n    fig.add_annotation(\n        x=-0.25,\n        y=row + 0.5,\n        text=str(row + 1),\n        font={\"size\": 32, \"color\": INK_SOFT},\n        showarrow=False,\n        xanchor=\"right\",\n        yanchor=\"middle\",\n    )\n\n# Layout\nfig.update_layout(\n    title={\n        \"text\": \"Scholar's Mate · chessboard-pieces · plotly · anyplot.ai\",\n        \"font\": {\"size\": 36, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"range\": [-0.6, 8],\n        \"showgrid\": False,\n        \"zeroline\": False,\n        \"showticklabels\": False,\n        \"fixedrange\": True,\n        \"scaleanchor\": \"y\",\n        \"scaleratio\": 1,\n    },\n    yaxis={\"range\": [-0.6, 8], \"showgrid\": False, \"zeroline\": False, \"showticklabels\": False, \"fixedrange\": True},\n    plot_bgcolor=PAGE_BG,\n    paper_bgcolor=PAGE_BG,\n    margin={\"l\": 60, \"r\": 60, \"t\": 100, \"b\": 60},\n    showlegend=False,\n)\n\n# Add board border\nfig.add_shape(type=\"rect\", x0=0, y0=0, x1=8, y1=8, line={\"color\": INK_SOFT, \"width\": 3}, fillcolor=\"rgba(0,0,0,0)\")\n\n# Save as PNG (square format for chess board) and HTML\nfig.write_image(f\"plot-{THEME}.png\", width=1200, height=1200, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}