Skip to main content

Automagic Python Bindings for the Open Graph Drawing Framework written in C++

Project description

Original repository (GitHub) - Bugtracker and issues (GitHub) - PyPi package (PyPi ogdf-python) - Documentation (Read The Docs).

Official OGDF website (ogdf.net) - Public OGDF repository (GitHub) - Internal OGDF repository (GitLab) - OGDF Documentation (GitHub / Doxygen) - cppyy Documentation (Read The Docs).

ogdf-python uses the black magic of the awesome cppyy library to automagically generate python bindings for the C++ Open Graph Drawing Framework (OGDF). It is available for Python>=3.6 and is Apache2 licensed. There are no binding definitions files, no stuff that needs extra compiling, it just works™, believe me. Templates, namespaces, cross-language callbacks and inheritance, pythonic iterators and generators, it’s all there. If you want to learn more about the magic behind the curtains, read this article.

Quickstart

First, install the package ogdf-python package. Please note that building cppyy from sources may take a while. If you didn’t install OGDF globally on your system, either set the OGDF_INSTALL_DIR to the prefix you configured in cmake, or set OGDF_BUILD_DIR to the subdirectory of your copy of the OGDF repo where your out-of-source build lives.

$ pip install ogdf-python
$ OGDF_BUILD_DIR=~/ogdf/build-debug python3

Alternatively, ogdf-python works very well with Jupyter:

%env OGDF_BUILD_DIR=~/ogdf/build-debug

from ogdf_python import ogdf, cppinclude
cppinclude("ogdf/basic/graph_generators/randomized.h")
cppinclude("ogdf/layered/SugiyamaLayout.h")

G = ogdf.Graph()
ogdf.setSeed(1)
ogdf.randomPlanarTriconnectedGraph(G, 20, 40)
GA = ogdf.GraphAttributes(G, ogdf.GraphAttributes.all)

for n in G.nodes:
    GA.label[n] = "N%s" % n.index()

SL = ogdf.SugiyamaLayout()
SL.call(GA)
GA
SugiyamaLayouted Graph

Read the pitfalls section and check out docs/examples/pitfalls.ipynb for the more advanced Sugiyama example from the OGDF docs. There is also a bigger example in docs/examples/ogdf-includes.ipynb. If anything is unclear, check out the python help help(ogdf.Graph) and read the corresponding OGDF documentation.

Pitfalls

See also docs/examples/pitfalls.ipynb for full examples.

OGDF sometimes takes ownership of objects (usually when they are passed as modules), which may conflict with the automatic cppyy garbage collection. Set __python_owns__ = False on those objects to tell cppyy that those objects don’t need to be garbage collected, but will be cleaned up from the C++ side.

SL = ogdf.SugiyamaLayout()
ohl = ogdf.OptimalHierarchyLayout()
ohl.__python_owns__ = False
SL.setLayout(ohl)

When you overwrite a python variable pointing to a C++ object (and it is the only python variable pointing to that object), the C++ object will usually be immediately deleted. This might be a problem if another C++ objects depends on that old object, e.g. a GraphAttributes instance depending on a Graph instance. Now the other C++ object has a pointer to a deleted and now invalid location, which will usually cause issues down the road (e.g. when the dependant object is deleted and wants to deregister from its no longer alive parent). This overwriting might easily happen if you run a Jupyter cell multiple times or some code in a for-loop. Please ensure that you always overwrite or delete dependent C++ variables in the reverse order of their initialization.

for i in range(5):
    # clean-up all variables
    CGA = CG = G = None # note that order is different from C++, CGA will be deleted first, G last
    # now we can re-use them
    G = ogdf.Graph()
    CG = ogdf.ClusterGraph(G)
    CGA = ogdf.ClusterGraphAttributes(CG, ogdf.ClusterGraphAttributes.all)

    # alternatively manually clean up in the right order
    del CGA
    del CG
    del G

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ogdf-python-0.1.2.dev0.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ogdf_python-0.1.2.dev0-py3-none-any.whl (1.8 MB view details)

Uploaded Python 3

File details

Details for the file ogdf-python-0.1.2.dev0.tar.gz.

File metadata

  • Download URL: ogdf-python-0.1.2.dev0.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.7 Linux/5.13.13-200.fc34.x86_64

File hashes

Hashes for ogdf-python-0.1.2.dev0.tar.gz
Algorithm Hash digest
SHA256 4a48bd7fb3c64fd0a2d43875f5229181e72a9a095f6e34d6b559b1c3f92a6016
MD5 7fee7b38bfb2c082cec85be7a93c6e38
BLAKE2b-256 4402159245358ce1e9c1b1c3818bd8d39825e2526ec67221a50a3946ba95473b

See more details on using hashes here.

File details

Details for the file ogdf_python-0.1.2.dev0-py3-none-any.whl.

File metadata

  • Download URL: ogdf_python-0.1.2.dev0-py3-none-any.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.7 Linux/5.13.13-200.fc34.x86_64

File hashes

Hashes for ogdf_python-0.1.2.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 a000cacc7951594ac24d0e24830148b04d508dd4ea3210c85b9c5eb69a4a3274
MD5 8e554c12c0e3dab5b856298308d785fc
BLAKE2b-256 4c408f72c63ecb1159f7ff5cb24e33bc2a9f94b8ce78099b9dc67b5621bef587

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page