7

How can I visually model items in a database using python?

I have a Django project that currently models my home network in the admin views. It currently describes what devices there are and what they are connected to. For example:

Devices:
    Computer1
    Computer2
    Laptop1
    Mobile1
    Router1
    ROuter2

ConnectionTypes:
    Wireless24ghz
    Wireless5ghz
    cat5
    cat5e
    cat6

Connections:
    host1:
        src:Computer1
        dst:Router1
        con_type:cat5e
    trunk1:
        src:Router1
        dst:Router2
        con_type:cat6
    host2:
        src:Mobile1
        dst:Router1
        con_type:Wireless24ghz

The database is a bit more complex than this, however I'm keeping it simple for now as it's not that important.

What I am wondering is, how I can graphically model my network using python code to look at the database tables? By graphically model I mean something similar to a Visio diagram in that I can see it and (not necessary but a HUGE bonus) interact with it, either via webpage or application.

Are there any existing python libraries that provide this sort of functionality? I understand JavaScript is good for this kind of modelling but I'm completely unsure of how I would go about doing it.

It's worth noting I'm not after anything fancy, simply drawing devices as rectangles and connections as lines going between rectangles is good enough.

JamoBox
  • 764
  • 9
  • 23
  • Possible duplicate of [Django - Model graphic representation (ERD)](http://stackoverflow.com/questions/6776592/django-model-graphic-representation-erd) – Celeo Oct 23 '15 at 20:45

4 Answers4

3

I am pretty sure there is no ready solution for this. Look at the graphviz library and make a management command to create a DOT graph. Here is a graphviz tutorial article http://matthiaseisen.com/articles/graphviz/

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
  • Thanks, that looks like a neat tool. I don't mind writing the code for it (in fact it sounds rather fun) so that might be ideal. – JamoBox Oct 23 '15 at 20:48
3

Since you're asking about Django in particular, django-extensions has already done the heavy lifting for you. Have a look at this:

http://django-extensions.readthedocs.org/en/latest/graph_models.html

Good luck!

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • 1
    Awesome, a nice addition to Mad Wombat's answer. I'll definitely have a look at this one, thanks – JamoBox Oct 23 '15 at 22:33
2

If you are using Django and want something simple and static, you can draw the graph into an image using the ImageDraw module offered by Pillow (the PIL replacement) and then include that image in the web page your app delivers to the client browser.

Also, of course, you might have heard of matplotlib

Pynchia
  • 10,996
  • 5
  • 34
  • 43
0

As an alternative, I have been using the nwdiag library from blockdiag to create network diagrams in code.

http://blockdiag.com/en/nwdiag/

The cool thing about this is that blockdiag has lots of other useful libraries for this kind of task, such as packet and rack diagrams.

JamoBox
  • 764
  • 9
  • 23