Skip to content

Commit 1cfe3f7

Browse files
authored
Merge pull request #5 from topogram/v2
Merge changes for Topogram V1
2 parents ef8a70f + e72026c commit 1cfe3f7

File tree

9 files changed

+434
-367
lines changed

9 files changed

+434
-367
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ARC5*
2+
13
venv
24
*.oldies.py
35

README.md

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ topogram.create_topogram("test")
2020

2121
Read the docs at [http://topogram.readthedocs.io/](http://topogram.readthedocs.io/)
2222

23+
2324
## Install
2425

2526
git clone https://github.com/topogram/topogram-api-client
@@ -29,76 +30,15 @@ Read the docs at [http://topogram.readthedocs.io/](http://topogram.readthedocs.i
2930
## Example
3031

3132
```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)
33+
from topogram-python-client import TopogramAPIClient
9834

35+
topogram = TopogramAPIClient("http://localhost:3000")
9936

37+
# create a new network
38+
topogram.create_topogram("My Topogram")
10039
create_topogram("Test", my_nodes, my_edges)
10140
```
41+
See a complete example in [examples](./examples) folder
10242

10343
## command-line
10444

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
You can adapt this file completely to your liking, but it should at least
44
contain the root `toctree` directive.
55
6-
Python API Client
7-
===============================================
6+
Python API Client
7+
===============================================
88
99
.. only:: html
1010

examples/data/test_data_edges.csv

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
source,target,weight
2+
1,9,4
3+
5,8,3
4+
7,3,1
5+
9,9,2
6+
9,3,2
7+
2,10,2
8+
10,8,1
9+
3,7,2
10+
10,8,2
11+
8,10,4
12+
10,5,2
13+
9,7,2
14+
7,8,2
15+
5,4,3
16+
10,6,4
17+
6,2,4
18+
7,10,1
19+
2,1,3
20+
2,7,1
21+
2,2,2

examples/data/test_data_nodes.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id,name,lat,lng,weight,year_start,year_stop
2+
1,Stephanie,-6.7589,105.8671,20,2005,2008
3+
2,Teresa,47.7675,21.24,14,2006,2009
4+
3,Theresa,29.28065,105.70568,11,2005,2008
5+
4,Phyllis,-37.46025,-63.58537,11,2006,2010
6+
5,Ann,-6.23,106.0752,19,2006,2011
7+
6,Jennifer,-6.5465,105.8728,10,2007,2012
8+
7,Jacqueline,15.31667,-91.61667,11,2009,2010
9+
8,Julie,61.79682,25.70457,12,2008,2010
10+
9,Louise,61.19472,62.86889,10,2007,2009
11+
10,Wanda,-31.38333,-57.96667,19,2009,2012

examples/topogram.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from topogram_client import TopogramAPIClient
5+
from random import randint
6+
from csv import DictReader
7+
8+
# credentials
9+
TOPOGRAM_URL = "http://localhost:3000" # "https://app.topogram.io"
10+
11+
PASSWORD = "password"
12+
13+
# data
14+
my_nodes = []
15+
my_edges = []
16+
17+
with open('data/test_data_nodes.csv') as f :
18+
reader = DictReader(f)
19+
for n in reader :
20+
node = {
21+
"id" : n["id"],
22+
"name" : n["name"],
23+
"lat" : float(n["lat"]),
24+
"lng" : float(n["lng"]),
25+
"weight" : float(n["weight"]),
26+
"start" : n["year_start"],
27+
"end" : n["year_stop"]
28+
}
29+
my_nodes.append({ "data" : node })
30+
31+
print my_nodes
32+
33+
with open('data/test_data_edges.csv') as f :
34+
reader = DictReader(f)
35+
for e in reader :
36+
edge = {
37+
"source" : e["source"],
38+
"target" : e["target"],
39+
"weight" : float(e["weight"])
40+
}
41+
my_edges.append({ "data" : edge })
42+
43+
# print my_edges
44+
45+
def create_topogram(title, nodes, edges):
46+
47+
print "Creating topogram '%s'"%title
48+
49+
try :
50+
r = topogram.create_topogram(title)
51+
except ValueError:
52+
print '> Topogram already exists'
53+
r = topogram.get_topogram_by_name(title)
54+
55+
topogram_ID = r["data"]["_id"]
56+
print "topogram ID : %s"%topogram_ID
57+
58+
# get and backup existing nodes and edges
59+
existing_nodes = topogram.get_nodes(topogram_ID)["data"]
60+
existing_edges = topogram.get_edges(topogram_ID)["data"]
61+
62+
# clear existing graph
63+
if len(existing_nodes):
64+
topogram.delete_nodes([n["_id"] for n in existing_nodes])
65+
print "%s nodes deleted"%len(existing_nodes)
66+
if len(existing_edges):
67+
topogram.delete_edges([n["_id"] for n in existing_edges])
68+
print "%s edges deleted"%len(existing_edges)
69+
70+
r = topogram.create_nodes(topogram_ID, nodes)
71+
print r
72+
print "%s nodes created."%len(r["data"])
73+
r = topogram.create_edges(topogram_ID, edges)
74+
print "%s edges created."%len(r["data"])
75+
76+
print "done. Topogram has been updated. Check it at %s/topograms/%s"%(TOPOGRAM_URL, topogram_ID)
77+
78+
# connect to the topogram instance (pass debug=True params for more info )
79+
topogram = TopogramAPIClient(TOPOGRAM_URL) #, debug=True)
80+
81+
# create a new user
82+
try :
83+
topogram.create_user(USER, PASSWORD)
84+
except ValueError:
85+
print "> User has already been created."
86+
87+
# login a new user if needed
88+
resp_user_login = topogram.user_login(USER, PASSWORD)
89+
print resp_user_login
90+
91+
assert(resp_user_login["status"] == "success")
92+
assert(resp_user_login["status_code"] == 200)
93+
94+
create_topogram("Geo-time network", my_nodes, my_edges)

0 commit comments

Comments
 (0)