|
| 1 | +# ogdf-python 0.3.5-dev: Automagic Python Bindings for the Open Graph Drawing Framework |
| 2 | + |
| 3 | +`ogdf-python` uses the [black magic](http://www.camillescott.org/2019/04/11/cmake-cppyy/) |
| 4 | +of the awesome [cppyy](https://bitbucket.org/wlav/cppyy/src/master/) library to automagically generate python bindings |
| 5 | +for the C++ [Open Graph Drawing Framework (OGDF)](https://ogdf.uos.de/). |
| 6 | +It is available for Python\>=3.6 and is Apache2 licensed. |
| 7 | +There are no binding definitions files, no stuff that needs extra compiling, it just works™, believe me. |
| 8 | +Templates, namespaces, cross-language callbacks and inheritance, pythonic iterators and generators, it's all there. |
| 9 | +If you want to learn more about the magic behind the curtains, read [this article](http://www.camillescott.org/2019/04/11/cmake-cppyy/). |
| 10 | + |
| 11 | +## Useful Links |
| 12 | + |
| 13 | +[Original repository](https://github.com/N-Coder/ogdf-python) (GitHub) - |
| 14 | +[Bugtracker and issues](https://github.com/N-Coder/ogdf-python) (GitHub) - |
| 15 | +[PyPi package](https://pypi.python.org/pypi/ogdf-python) (PyPi `ogdf-python`) - |
| 16 | +[Try it out!](https://mybinder.org/v2/gh/N-Coder/ogdf-python/HEAD?labpath=docs%2Fexamples%2Fsugiyama-simple.ipynb) (mybinder.org). |
| 17 | + |
| 18 | +[Official OGDF website](https://ogdf.uos.de/) (ogdf.net) - |
| 19 | +[Public OGDF repository](https://github.com/ogdf/ogdf) (GitHub) - |
| 20 | +[OGDF Documentation](https://ogdf.github.io/docs/ogdf/) (GitHub / Doxygen) - |
| 21 | +[cppyy Documentation](https://cppyy.readthedocs.io) (Read The Docs). |
| 22 | + |
| 23 | +## Quickstart |
| 24 | + |
| 25 | +Click here to start an interactive online Jupyter Notebook with an example OGDF graph where you can try out `ogdf-python`: [](https://mybinder.org/v2/gh/N-Coder/ogdf-python/HEAD?labpath=docs%2Fexamples%2Fsugiyama-simple.ipynb)<br/> |
| 26 | +Simply re-run the code cell to see the graph. You can also find further examples next to that Notebook (i.e. via the folder icon on the left). |
| 27 | + |
| 28 | +To get a similar Jupyter Notebook with a little more compute power running on your local machine, use the following install command and open the link to `localhost`/`127.0.0.1` that will be printed in your browser: |
| 29 | + |
| 30 | +``` bash |
| 31 | +pip install 'ogdf-python[quickstart]' |
| 32 | +jupyter lab |
| 33 | +``` |
| 34 | + |
| 35 | +The optional `[quickstart]` pulls in matplotlib and jupyter lab as well as a ready-to-use binary build of the OGDF via [ogdf-wheel](https://github.com/ogdf/ogdf-wheel). |
| 36 | +Please note that downloading and installing all dependencies (especially building `cppyy`) may take a moment. |
| 37 | +If you want to use your own local build of the OGDF, see the instructions [below](#manual-installation) for installing `ogdf-python` without `ogdf-wheel`. |
| 38 | + |
| 39 | +> [!IMPORTANT] |
| 40 | +> We currently support Linux, MacOS on Intel and Apple Silicon, and the Windows Subsytem for Linux. |
| 41 | +> Directly running on **Windows is not supported**, see [issue 4](https://github.com/ogdf/ogdf-python/issues/8#issuecomment-2820925482). |
| 42 | +> When using WSL, make sure that you are using the Linux python(3) and not the Windows python.exe, i.e. the [startup message](https://docs.python.org/3/tutorial/interpreter.html#interactive-mode) of the python interpreter should end with `on linux` instead of `on win32`. |
| 43 | +
|
| 44 | +## Usage |
| 45 | + |
| 46 | +`ogdf-python` works very well with Jupyter: |
| 47 | + |
| 48 | +``` python |
| 49 | +# %matplotlib widget |
| 50 | +# uncomment the above line if you want the interactive display |
| 51 | + |
| 52 | +from ogdf_python import * |
| 53 | +cppinclude("ogdf/basic/graph_generators/randomized.h") |
| 54 | +cppinclude("ogdf/layered/SugiyamaLayout.h") |
| 55 | + |
| 56 | +G = ogdf.Graph() |
| 57 | +ogdf.setSeed(1) |
| 58 | +ogdf.randomPlanarTriconnectedGraph(G, 20, 40) |
| 59 | +GA = ogdf.GraphAttributes(G, ogdf.GraphAttributes.all) |
| 60 | + |
| 61 | +for n in G.nodes: |
| 62 | + GA.label[n] = "N%s" % n.index() |
| 63 | + |
| 64 | +SL = ogdf.SugiyamaLayout() |
| 65 | +SL.call(GA) |
| 66 | +GA |
| 67 | +``` |
| 68 | + |
| 69 | +[{height="300px"}](docs/examples/sugiyama-simple.ipynb) |
| 70 | + |
| 71 | +Read the [pitfalls section](#pitfalls) and check out [docs/examples/pitfalls.ipynb](docs/examples/pitfalls.ipynb) |
| 72 | +for the more advanced Sugiyama example from the OGDF docs. |
| 73 | +There is also a bigger example in [docs/examples/ogdf-includes.ipynb](docs/examples/ogdf-includes.ipynb). |
| 74 | +If anything is unclear, check out the python help `help(ogdf.Graph)` and read the corresponding OGDF documentation. |
| 75 | + |
| 76 | +## Installation without ogdf-wheel |
| 77 | + |
| 78 | +Use pip to install the `ogdf-python` package locally on your machine. |
| 79 | +Please note that building `cppyy` from sources may take a while. |
| 80 | +Furthermore, you will need a local shared library build (`-DBUILD_SHARED_LIBS=ON`) of the [OGDF](https://ogdf.github.io/doc/ogdf/md_doc_build.html). |
| 81 | +If you didn't install the OGDF globally on your system, |
| 82 | +either set the `OGDF_INSTALL_DIR` to the prefix you configured in `cmake`, |
| 83 | +or set `OGDF_BUILD_DIR` to the subdirectory of your copy of the OGDF repo where your |
| 84 | +[out-of-source build](https://ogdf.github.io/doc/ogdf/md_doc_build.html#autotoc_md4) (and especially the generated `OgdfTargets.cmake` file) lives. |
| 85 | + |
| 86 | +``` bash |
| 87 | +$ pip install ogdf-python |
| 88 | +$ OGDF_BUILD_DIR=~/ogdf/build-release python3 |
| 89 | +``` |
| 90 | + |
| 91 | +### Debug and Release Mode |
| 92 | + |
| 93 | +Starting with OGDF 2025.10 (Foxglove), your chosen build mode (debug or release) also affects |
| 94 | +the name of the built shared libraries (e.g. `libOGDF-debug.so` instead of `libOGDF.so`) as well as |
| 95 | +the location of the header file containing configuration information (`ogdf-{debug,release}/ogdf/basic/internal/config_autogen.h`). |
| 96 | +By default, ogdf-python will attempt to load the release versions, even if you only have the debug version built/installed. |
| 97 | +To load the debug versions instead, set the environment variable `OGDF_PYTHON_MODE=debug`. |
| 98 | + |
| 99 | +``` bash |
| 100 | +$ OGDF_BUILD_DIR=~/ogdf/build-debug OGDF_PYTHON_MODE=debug python3 |
| 101 | +$ OGDF_PYTHON_MODE=debug python3 # also works if you have both versions installed next to each other |
| 102 | +``` |
| 103 | + |
| 104 | +## Pitfalls |
| 105 | + |
| 106 | +See also [docs/examples/pitfalls.ipynb](docs/examples/pitfalls.ipynb) for full examples. |
| 107 | + |
| 108 | +OGDF sometimes takes ownership of objects (usually when they are passed as modules), |
| 109 | +which may conflict with the automatic cppyy garbage collection. |
| 110 | +Set `__python_owns__ = False` on those objects to tell cppyy that those objects |
| 111 | +don't need to be garbage collected, but will be cleaned up from the C++ side. |
| 112 | + |
| 113 | +``` python |
| 114 | +SL = ogdf.SugiyamaLayout() |
| 115 | +ohl = ogdf.OptimalHierarchyLayout() |
| 116 | +ohl.__python_owns__ = False |
| 117 | +SL.setLayout(ohl) |
| 118 | +``` |
| 119 | + |
| 120 | +When you overwrite a python variable pointing to a C++ object (and it is the only |
| 121 | +python variable pointing to that object), the C++ object will usually be immediately deleted. |
| 122 | +This might be a problem if another C++ objects depends on that old object, e.g. |
| 123 | +a `GraphAttributes` instance depending on a `Graph` instance. |
| 124 | +Now the other C++ object has a pointer to a deleted and now invalid location, |
| 125 | +which will usually cause issues down the road (e.g. when the dependant object is |
| 126 | +deleted and wants to deregister from its no longer alive parent). |
| 127 | +This overwriting might easily happen if you run a Jupyter cell multiple times or some code in a `for`-loop. |
| 128 | +Please ensure that you always overwrite or delete dependent C++ variables in |
| 129 | +the reverse order of their initialization. |
| 130 | + |
| 131 | +``` python |
| 132 | +for i in range(5): |
| 133 | + # clean-up all variables |
| 134 | + CGA = CG = G = None # note that order is different from C++, CGA will be deleted first, G last |
| 135 | + # now we can re-use them |
| 136 | + G = ogdf.Graph() |
| 137 | + CG = ogdf.ClusterGraph(G) |
| 138 | + CGA = ogdf.ClusterGraphAttributes(CG, ogdf.ClusterGraphAttributes.all) |
| 139 | + |
| 140 | + # alternatively manually clean up in the right order |
| 141 | + del CGA |
| 142 | + del CG |
| 143 | + del G |
| 144 | +``` |
| 145 | + |
| 146 | +There seems to be memory leak in the Jupyter Lab server which causes it to use large amounts of memory |
| 147 | +over time while working with ogdf-python. On Linux, the following command can be used to limit this memory usage: |
| 148 | + |
| 149 | +``` bash |
| 150 | +systemd-run --scope -p MemoryMax=5G --user -- jupyter notebook |
| 151 | +``` |
0 commit comments