meshy / django-schema-graph

An interactive graph of your Django model structure
https://pypi.org/project/django-schema-graph/
MIT License
367 stars 10 forks source link

Export graph data #61

Open rotten opened 1 year ago

rotten commented 1 year ago

Gephi [ https://gephi.org ] is a handy graph visualization tool, as is Graphviz [ https://graphviz.org ]. It would be cool if it were easy to just export the raw graph data here, and then import it a popular graph visualization tool of choice to run graph algorithms on it and study it further.

rotten commented 1 year ago

FWIW, I was able to grab the raw graph data this tool generates by using the wget command found in issue #49: https://github.com/meshy/django-schema-graph/issues/49#issuecomment-1226013113 and then editing the index.html, and then post-processing it to a format gephi could use. It was more manual work than I had planned, which is why I thought it might make a nice feature.

meshy commented 1 year ago

It should be possible to create a view that returns the data in a format you like by taking inspiration from the existing view: https://github.com/meshy/django-schema-graph/blob/f0b982cca73975168b921d737d6854c2e9c24df3/schema_graph/views.py#L29-L33

In your case, I think you'll want something like:

from cattrs.preconf.json import make_converter as make_json_converter
from django.http import HttpResponse

from schema_graph.schema import get_schema

def schema_json(request):
     schema = get_schema()
     json_converter = make_json_converter()
     schema_json = json_converter.dumps(schema)
     return HttpResponse(schema_json, content_type="application/json")

This example returns the graph data as JSON, but if you want another format you should be able to do that in the view.