{"spec_id":"sudoku-basic","library":"altair","language":"python","code":"\"\"\" anyplot.ai\nsudoku-basic: Basic Sudoku Grid\nLibrary: altair 6.2.2 | Python 3.13.14\nQuality: 88/100 | Updated: 2026-06-25\n\"\"\"\n\nimport os\n\nimport altair as alt\nimport pandas as pd\nfrom PIL import Image\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# Sudoku grid data - \"World's Hardest Sudoku\"\ngrid = [\n    [5, 3, 0, 0, 7, 0, 0, 0, 0],\n    [6, 0, 0, 1, 9, 5, 0, 0, 0],\n    [0, 9, 8, 0, 0, 0, 0, 6, 0],\n    [8, 0, 0, 0, 6, 0, 0, 0, 3],\n    [4, 0, 0, 8, 0, 3, 0, 0, 1],\n    [7, 0, 0, 0, 2, 0, 0, 0, 6],\n    [0, 6, 0, 0, 0, 0, 2, 8, 0],\n    [0, 0, 0, 4, 1, 9, 0, 0, 5],\n    [0, 0, 0, 0, 8, 0, 0, 7, 9],\n]\n\n# Cell data with tooltip metadata\ncell_data = []\nfor row in range(9):\n    for col in range(9):\n        value = grid[row][col]\n        if value != 0:\n            cell_data.append(\n                {\n                    \"col\": col + 0.5,\n                    \"row\": 8 - row + 0.5,\n                    \"value\": str(value),\n                    \"row_num\": row + 1,\n                    \"col_num\": col + 1,\n                    \"box\": (row // 3) * 3 + col // 3 + 1,\n                }\n            )\n\ndf_numbers = pd.DataFrame(cell_data)\n\n# Alternating 3×3 box backgrounds — checkerboard pattern for visual depth\nshaded_boxes = [\n    {\"x\": bc * 3, \"x2\": bc * 3 + 3, \"y\": br * 3, \"y2\": br * 3 + 3}\n    for br in range(3)\n    for bc in range(3)\n    if (br + bc) % 2 == 0\n]\ndf_shaded = pd.DataFrame(shaded_boxes)\n\n# Grid lines — thin for cells, thick for 3×3 box boundaries\nthin_lines = []\nthick_lines = []\nfor i in range(10):\n    v_line = {\"x\": i, \"x2\": i, \"y\": 0, \"y2\": 9}\n    h_line = {\"x\": 0, \"x2\": 9, \"y\": i, \"y2\": i}\n    if i % 3 == 0:\n        thick_lines.append(v_line)\n        thick_lines.append(h_line)\n    else:\n        thin_lines.append(v_line)\n        thin_lines.append(h_line)\n\ndf_thin = pd.DataFrame(thin_lines)\ndf_thick = pd.DataFrame(thick_lines)\n\n# Layer 1: subtle checkerboard shading for 3×3 box regions\nbox_bg = alt.Chart(df_shaded).mark_rect(color=INK, fillOpacity=0.07).encode(x=\"x:Q\", x2=\"x2:Q\", y=\"y:Q\", y2=\"y2:Q\")\n\n# Layer 2 & 3: thin cell lines, thick box-separator lines\nthin_grid = alt.Chart(df_thin).mark_rule(color=INK_SOFT, strokeWidth=1.0).encode(x=\"x:Q\", x2=\"x2:Q\", y=\"y:Q\", y2=\"y2:Q\")\n\nthick_grid = alt.Chart(df_thick).mark_rule(color=INK, strokeWidth=4.5).encode(x=\"x:Q\", x2=\"x2:Q\", y=\"y:Q\", y2=\"y2:Q\")\n\n# Layer 4: numbers with interactive tooltips for the HTML output\nnumbers = (\n    alt.Chart(df_numbers)\n    .mark_text(fontSize=40, fontWeight=\"bold\", color=INK)\n    .encode(\n        x=\"col:Q\",\n        y=\"row:Q\",\n        text=\"value:N\",\n        tooltip=[\n            alt.Tooltip(\"row_num:Q\", title=\"Row\"),\n            alt.Tooltip(\"col_num:Q\", title=\"Column\"),\n            alt.Tooltip(\"value:N\", title=\"Digit\"),\n            alt.Tooltip(\"box:Q\", title=\"Box\"),\n        ],\n    )\n)\n\nchart = (\n    alt.layer(box_bg, thin_grid, thick_grid, numbers)\n    .properties(\n        width=500,\n        height=500,\n        background=PAGE_BG,\n        title=alt.Title(\n            \"sudoku-basic · python · altair · anyplot.ai\",\n            fontSize=16,\n            fontWeight=\"normal\",\n            color=INK,\n            anchor=\"middle\",\n            offset=16,\n        ),\n    )\n    .configure_view(fill=PAGE_BG, stroke=None)\n    .configure_axis(grid=False, domain=False, ticks=False, labels=False, title=None)\n    .configure_scale(bandPaddingInner=0, bandPaddingOuter=0)\n)\n\n# Save — square target 2400×2400\nTW, TH = 2400, 2400\nchart.save(f\"plot-{THEME}.png\", scale_factor=4.0)\nchart.save(f\"plot-{THEME}.html\")\n\n# Pad to exact target dimensions (vl-convert inner-view is smaller than the PNG target)\n_img = Image.open(f\"plot-{THEME}.png\").convert(\"RGB\")\n_w, _h = _img.size\nif _w > TW or _h > TH:\n    raise SystemExit(\n        f\"altair vl-convert produced {_w}×{_h}, exceeds target {TW}×{TH}. \"\n        f\"Shrink chart .properties(width=, height=) values and re-render.\"\n    )\nif _w < TW or _h < TH:\n    _canvas = Image.new(\"RGB\", (TW, TH), PAGE_BG)\n    _canvas.paste(_img, ((TW - _w) // 2, (TH - _h) // 2))\n    _canvas.save(f\"plot-{THEME}.png\")\n"}