{"spec_id":"network-directed","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nnetwork-directed: Directed Network Graph\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import Arrow, ColumnDataSource, LabelSet, NormalHead\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"\n\n# Data: Software module dependencies\nnp.random.seed(42)\n\n# Define nodes (software modules)\nnodes = {\n    \"api\": {\"label\": \"API\", \"group\": \"core\"},\n    \"auth\": {\"label\": \"Auth\", \"group\": \"core\"},\n    \"database\": {\"label\": \"Database\", \"group\": \"core\"},\n    \"cache\": {\"label\": \"Cache\", \"group\": \"infra\"},\n    \"logger\": {\"label\": \"Logger\", \"group\": \"infra\"},\n    \"config\": {\"label\": \"Config\", \"group\": \"infra\"},\n    \"utils\": {\"label\": \"Utils\", \"group\": \"shared\"},\n    \"models\": {\"label\": \"Models\", \"group\": \"core\"},\n    \"routes\": {\"label\": \"Routes\", \"group\": \"core\"},\n    \"middleware\": {\"label\": \"Middleware\", \"group\": \"core\"},\n    \"validators\": {\"label\": \"Validators\", \"group\": \"shared\"},\n    \"tests\": {\"label\": \"Tests\", \"group\": \"dev\"},\n}\n\n# Define directed edges (dependencies: source imports target)\nedges = [\n    (\"api\", \"routes\"),\n    (\"api\", \"middleware\"),\n    (\"api\", \"config\"),\n    (\"routes\", \"auth\"),\n    (\"routes\", \"database\"),\n    (\"routes\", \"models\"),\n    (\"routes\", \"validators\"),\n    (\"middleware\", \"auth\"),\n    (\"middleware\", \"logger\"),\n    (\"auth\", \"database\"),\n    (\"auth\", \"cache\"),\n    (\"auth\", \"config\"),\n    (\"database\", \"config\"),\n    (\"database\", \"logger\"),\n    (\"cache\", \"config\"),\n    (\"cache\", \"logger\"),\n    (\"models\", \"database\"),\n    (\"models\", \"validators\"),\n    (\"validators\", \"utils\"),\n    (\"tests\", \"api\"),\n    (\"tests\", \"models\"),\n    (\"tests\", \"utils\"),\n]\n\n# Use circular layout for clear visualization - scaled for 4800x2700 canvas\nn_nodes = len(nodes)\nnode_ids = list(nodes.keys())\nangles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False)\n\n# Position nodes in a circle - use larger scale for canvas\nradius = 900\ncenter_x, center_y = 2400, 1350\n\npositions = {}\nfor i, node_id in enumerate(node_ids):\n    # Offset angle to have 'api' at the top\n    angle = angles[i] - np.pi / 2\n    positions[node_id] = {\"x\": center_x + radius * np.cos(angle), \"y\": center_y + radius * np.sin(angle)}\n\n# Okabe-Ito color palette for groups\ngroup_colors = {\n    \"core\": BRAND,  # #009E73 - bluish green (Okabe-Ito position 1)\n    \"infra\": \"#C475FD\",  # vermillion\n    \"shared\": \"#4467A3\",  # blue\n    \"dev\": \"#BD8233\",  # reddish purple\n}\n\n# Prepare node data\nnode_x = [positions[n][\"x\"] for n in node_ids]\nnode_y = [positions[n][\"y\"] for n in node_ids]\nnode_labels = [nodes[n][\"label\"] for n in node_ids]\nnode_colors = [group_colors[nodes[n][\"group\"]] for n in node_ids]\n\nnode_source = ColumnDataSource(data={\"x\": node_x, \"y\": node_y, \"label\": node_labels, \"color\": node_colors})\n\n# Create figure with proper canvas size\np = figure(\n    width=4800,\n    height=2700,\n    title=\"network-directed · bokeh · anyplot.ai\",\n    x_range=(0, 4800),\n    y_range=(0, 2700),\n    tools=\"\",\n    toolbar_location=None,\n)\n\n# Theme styling\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None\np.axis.visible = False\np.grid.visible = False\n\n# Style title\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.title.align = \"center\"\n\n# Draw edges with arrows\narrow_color = INK_SOFT\nfor source_node, target_node in edges:\n    sx, sy = positions[source_node][\"x\"], positions[source_node][\"y\"]\n    tx, ty = positions[target_node][\"x\"], positions[target_node][\"y\"]\n\n    # Calculate direction vector\n    dx = tx - sx\n    dy = ty - sy\n    length = np.sqrt(dx**2 + dy**2)\n\n    # Shorten edges to not overlap with nodes\n    node_radius = 100  # Visual node radius scaled for canvas\n    start_offset = node_radius / length\n    end_offset = (node_radius + 35) / length  # Extra space for arrow head\n\n    # Adjusted start and end points\n    start_x = sx + dx * start_offset\n    start_y = sy + dy * start_offset\n    end_x = tx - dx * end_offset\n    end_y = ty - dy * end_offset\n\n    # Add arrow\n    p.add_layout(\n        Arrow(\n            end=NormalHead(size=30, fill_color=arrow_color, line_color=arrow_color),\n            x_start=start_x,\n            y_start=start_y,\n            x_end=end_x,\n            y_end=end_y,\n            line_color=arrow_color,\n            line_width=4,\n            line_alpha=0.7,\n        )\n    )\n\n# Draw nodes - larger size for visibility\np.scatter(x=\"x\", y=\"y\", source=node_source, size=200, fill_color=\"color\", line_color=INK_SOFT, line_width=4, alpha=0.9)\n\n# Add labels on nodes\nlabels = LabelSet(\n    x=\"x\",\n    y=\"y\",\n    text=\"label\",\n    source=node_source,\n    text_font_size=\"22pt\",\n    text_color=INK,\n    text_align=\"center\",\n    text_baseline=\"middle\",\n    text_font_style=\"bold\",\n)\np.add_layout(labels)\n\n# Add legend in upper right\nlegend_x = 4100\nlegend_y = 2450\nlegend_items = [\n    (\"Core Modules\", group_colors[\"core\"]),\n    (\"Infrastructure\", group_colors[\"infra\"]),\n    (\"Shared Utils\", group_colors[\"shared\"]),\n    (\"Development\", group_colors[\"dev\"]),\n]\n\n# Legend background\np.rect(\n    x=4250, y=2300, width=500, height=400, fill_color=ELEVATED_BG, fill_alpha=0.95, line_color=INK_SOFT, line_width=2\n)\n\nfor i, (label, color) in enumerate(legend_items):\n    y_pos = legend_y - i * 80\n    p.scatter(x=[legend_x], y=[y_pos], size=50, fill_color=color, line_color=INK_SOFT, line_width=2)\n    p.text(\n        x=[legend_x + 60], y=[y_pos], text=[label], text_font_size=\"22pt\", text_baseline=\"middle\", text_color=INK_SOFT\n    )\n\n# Write the interactive HTML (also a required catalog artifact)\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot it with headless Chrome — Selenium 4 / Selenium Manager\n# auto-resolves a working driver for the system Chrome.\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)  # let bokeh's JS render the canvas\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}