{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0. Import dependencies.\n",
    "import random\n",
    "import pydot\n",
    "from pydot_flow import Chart\n",
    "from IPython.display import Image\n",
    "\n",
    "\n",
    "# 1. Define a function for creating a random color code.\n",
    "def random_color():\n",
    "    \"Generate a random color.\"\n",
    "    return \"#\" + hex(random.randint(0, 0xFFFFFF))[2:].rjust(6, \"0\")\n",
    "\n",
    "\n",
    "# 2. Create a Pydot flow Chart.\n",
    "chart = Chart(\n",
    "    rankdir=\"TB\",\n",
    "    splines=\"true\",\n",
    "    label=\"Chart\",\n",
    "    labelloc=\"t\",\n",
    "    fontname=\"Sans\",\n",
    "    pad=0.2,\n",
    "    ratio=0.5,\n",
    "    graph_type=\"graph\",\n",
    ")\n",
    "\n",
    "# 3. Set Node defaults.\n",
    "chart.get_graph().set_node_defaults(style=\"filled\")\n",
    "\n",
    "# 4. Create the first Node and name it Node 1.\n",
    "node_1 = chart.create_node(\n",
    "    src_node_attrs={\n",
    "        \"label\": \"Node 1\",\n",
    "        \"shape\": \"diamond\",\n",
    "        \"color\": random_color(),\n",
    "    }\n",
    ")\n",
    "\n",
    "# 5. Flow from Node 1 to Node 2a.\n",
    "node_2a = node_1.flow(\n",
    "    src_port=\"s\",\n",
    "    dst_node_attrs={\n",
    "        \"label\": \"Node 2a\",\n",
    "        \"shape\": \"oval\",\n",
    "        \"color\": random_color(),\n",
    "    },\n",
    "    edge_attrs={\"label\": \"Edge 1-2a\"},\n",
    ")\n",
    "\n",
    "# 6. Flow from Node 2a to 3a.\n",
    "node_3a = node_2a.flow(\n",
    "    src_port=\"s\",\n",
    "    dst_node_attrs={\n",
    "        \"label\": \"Node 3a\",\n",
    "        \"shape\": \"square\",\n",
    "        \"color\": random_color(),\n",
    "    },\n",
    "    edge_attrs={\"label\": \"Edge 2a-3a\"},\n",
    ")\n",
    "\n",
    "# 7. Return to Node 1 and flow to Node 2b.\n",
    "node_2b = node_1.flow(\n",
    "    src_port=\"s\",\n",
    "    dst_node_attrs={\n",
    "        \"label\": \"Node 2b\",\n",
    "        \"shape\": \"rectangle\",\n",
    "        \"color\": random_color(),\n",
    "    },\n",
    "    edge_attrs={\"label\": \"Edge 1-2b\"},\n",
    ")\n",
    "\n",
    "# 8. Flow from Node 2b to Node 2c.\n",
    "node_2c = node_2b.flow(\n",
    "    src_port=\"e\",\n",
    "    dst_node_attrs={\n",
    "        \"label\": \"Node 2c\",\n",
    "        \"shape\": \"circle\",\n",
    "        \"color\": random_color(),\n",
    "    },\n",
    "    edge_attrs={\"label\": \"Edge 2b-2c\"},\n",
    "    graph=pydot.Subgraph(rank=\"same\"),\n",
    ")\n",
    "\n",
    "# 9. Return to Node 2b and flow to Node 3b.\n",
    "node_3b = node_2b.flow(\n",
    "    src_port=\"s\",\n",
    "    dst_node_attrs={\n",
    "        \"label\": \"Node 3b\",\n",
    "        \"shape\": \"triangle\",\n",
    "        \"color\": random_color(),\n",
    "    },\n",
    "    edge_attrs={\"label\": \"Edge 2b-3b\"},\n",
    ")\n",
    "\n",
    "# 10. Flow from Node 3b to Node 2c - reference Node 2c by name.\n",
    "node_3b.flow(\n",
    "    src_port=\"e\",\n",
    "    dst_node_attrs={\"name\": node_2c.get_name()},\n",
    "    edge_attrs={\"label\": \"Edge 3b-2c\"},\n",
    ")\n",
    "\n",
    "# 11. Save the graph image to a file.\n",
    "chart.get_graph().write_png(\"output.png\")\n",
    "\n",
    "# 12. Output an image in a Jupyter Notebook.\n",
    "Image(chart.get_graph().create_png(prog=\"dot\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pydot-flow",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
