|
4 | 4 |
|
5 | 5 | A python lib to communicate with Topogram |
6 | 6 |
|
| 7 | +## How it works |
7 | 8 |
|
8 | 9 |
|
9 | | -## How it works |
| 10 | +```python |
| 11 | +from topogram-python-client import TopogramAPIClient |
| 12 | + |
| 13 | +topogram = TopogramAPIClient("http://localhost:3000") |
| 14 | + |
| 15 | +# create a new network |
| 16 | +topogram.create_topogram("test") |
| 17 | +``` |
| 18 | + |
| 19 | +## Documentation |
10 | 20 |
|
11 | 21 | Read the docs at [http://topogram.readthedocs.io/](http://topogram.readthedocs.io/) |
12 | 22 |
|
13 | | - from topogram-python-client import RhiziAPIClient |
| 23 | +## Install |
| 24 | + |
| 25 | + git clone https://github.com/topogram/topogram-api-client |
| 26 | + cd topogram-api-client |
| 27 | + python setup.py install |
| 28 | + |
| 29 | +## Example |
| 30 | + |
| 31 | +```python |
| 32 | +#!/usr/bin/env python |
| 33 | +# -*- coding: utf-8 -*- |
| 34 | + |
| 35 | +from topogram_client import TopogramAPIClient |
| 36 | +from random import randint |
| 37 | + |
| 38 | +# credentials |
| 39 | +TOPOGRAM_URL = "https://app.topogram.io" # http://localhost:3000 |
| 40 | + |
| 41 | +PASSWORD = "password" |
| 42 | + |
| 43 | + |
| 44 | +# data |
| 45 | +NODES_COUNT = 5 |
| 46 | +EDGES_COUNT = 8 |
| 47 | +my_edges = [] |
| 48 | + |
| 49 | +my_nodes = [{ |
| 50 | + "id": i, |
| 51 | + "name" : "Node %s"%i |
| 52 | +} for i in range(0,NODES_COUNT)] |
| 53 | + |
| 54 | +for n in range(0,EDGES_COUNT): |
| 55 | + src = randint(0,NODES_COUNT) |
| 56 | + tgt = randint(0,NODES_COUNT) |
| 57 | + edge = { |
| 58 | + "source" : src, |
| 59 | + "target" : tgt, |
| 60 | + "weight" : 5, |
| 61 | + "name" : "Edge from %s to %s"%(src, tgt) |
| 62 | + } |
| 63 | + my_edges.append(edge) |
| 64 | + |
| 65 | +# connect to the topogram instance |
| 66 | +topogram = TopogramAPIClient(TOPOGRAM_URL) |
| 67 | + |
| 68 | +# create a new user |
| 69 | +topogram.create_user(USER, PASSWORD) |
| 70 | + |
| 71 | +# login a new user if needed |
| 72 | +topogram.user_login(USER, PASSWORD) |
| 73 | + |
| 74 | +def create_topogram(title, nodes, edges): |
| 75 | + |
| 76 | + print "Creating topogram '%s'"%title |
| 77 | + |
| 78 | + r = topogram.create_topogram(title) |
| 79 | + print r["message"] |
| 80 | + topogram_ID = r["data"][0]["_id"] |
| 81 | + |
| 82 | + # get and backup existing nodes and edges |
| 83 | + existing_nodes = topogram.get_nodes(topogram_ID)["data"] |
| 84 | + existing_edges = topogram.get_edges(topogram_ID)["data"] |
| 85 | + |
| 86 | + # clear existing graph |
| 87 | + topogram.delete_nodes([n["_id"] for n in existing_nodes]) |
| 88 | + print "nodes deleted" |
| 89 | + topogram.delete_edges([n["_id"] for n in existing_edges]) |
| 90 | + print "edges deleted" |
| 91 | + |
| 92 | + r = topogram.create_nodes(topogram_ID, nodes) |
| 93 | + print "%s nodes created."%len(r["data"]) |
| 94 | + r = topogram.create_edges(topogram_ID, edges) |
| 95 | + print "%s edges created."%len(r["data"]) |
| 96 | + |
| 97 | + print "done. Topogram has been updated. Check it at %s/topograms/%s/view"%(TOPOGRAM_URL, topogram_ID) |
14 | 98 |
|
15 | | - topogram = TopogramAPIClient("http://localhost:3000") |
16 | 99 |
|
17 | | - # create a new network |
18 | | - topogram.create_topogram("test") |
| 100 | +create_topogram("Test", my_nodes, my_edges) |
| 101 | +``` |
19 | 102 |
|
20 | 103 | ## command-line |
21 | 104 |
|
|
0 commit comments