{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Hyperlinks\n\n## Non-interactive figures\n\nNetgraph uses matplotlib to draw all figure elements, and `matplotib\nhas only limited support for hyperlinks`_. Specifically, you have to use the\nSVG backend or export the figure to SVG.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom netgraph import Graph\n\nfig, ax = plt.subplots()\ng = Graph([(0, 1)], ax=ax)\n\ng.node_artists[0].set_url(\"https://www.google.com\")\ng.edge_artists[(0, 1)].set_url(\"https://www.stackoverflow.com\")\n\nfig.savefig('image.svg')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Opening the image.svg file in a browser of your choice, you can click on\nthe plot elements to open the corresponding web pages.\n\n## Interactive figures\n\nTechnically, matplotlib does not support hyperlinks when using an\ninteractive backend. However, hyperlink behaviour can easily be\nemulated using `matplotlib pick-events`_ and the python in-built\n:code:`webbrowser` module. Note that these hyperlinks are not\navailable when exporting the figure (unless the figure is exported\nto SVG). Clicking on the PNG image embedded below will hence have\nno effect.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import webbrowser\nimport matplotlib.pyplot as plt\n\nfrom netgraph import Graph\n\ndef on_pick(event):\n    webbrowser.open(event.artist.get_url())\n\nfig, ax = plt.subplots()\n\nfig.canvas.mpl_connect('pick_event', on_pick)\n\ng = Graph([(0, 1)],\n          node_labels={1 : \"https://www.github.com\"},\n          node_label_offset=0.1,\n          edge_labels={(0, 1) : \"https://matplotlib.org/\"},\n          ax=ax)\n\ng.node_artists[0].set_picker(True)\ng.node_artists[0].set_url(\"https://www.google.com\")\n\ng.edge_artists[(0, 1)].set_picker(10) # increases the pick radius\ng.edge_artists[(0, 1)].set_url(\"https://www.stackoverflow.com\")\n\ng.node_label_artists[1].set_picker(True)\ng.node_label_artists[1].set_url(g.node_label_artists[1].get_text()) # assumes the label text defines the link target\n\ng.edge_label_artists[(0, 1)].set_picker(True)\ng.edge_label_artists[(0, 1)].set_url(g.edge_label_artists[(0, 1)].get_text()) # ditto\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.9.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}