Skip to content

Commit c37e097

Browse files
committed
Converted the python examples to using requests for json requests
1 parent cd2d299 commit c37e097

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

python/giv-client-marks.py

100644100755
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
giv.
55
"""
66

7-
import httplib
7+
import requests
88
import random
99
import math
10+
import json
1011

11-
conn = httplib.HTTPConnection('localhost:8448')
12-
headers = {"Content-type": "application/x-www-form-urlencoded",
13-
"Accept": "text/plain",
14-
}
1512

1613
for i in range(1024):
1714
giv_string = ("$marks fcircle\n"
@@ -22,11 +19,11 @@
2219
y = 256+100*math.sin((x+phase) / 512.0 * 2*3.1459)
2320
giv_string += "%f %f\n"%(x,y)
2421

25-
request = ('{"jsonrpc": "2.0", '
26-
'"method": "giv_string", '
27-
'"params": [\"'+giv_string+'"], '
28-
'"id": %d}'%(random.randint(1,100)))
22+
# Build a hash and then json.dumps() it
23+
data = {
24+
'jsonrpc' : '2.0',
25+
'method' : 'giv_string',
26+
'params' : [ giv_string ],
27+
'id': random.randint(1,100)}
2928

30-
conn.request("POST", url="", body= request, headers=headers)
31-
response = conn.getresponse()
32-
print response.read()
29+
r = requests.post(f'http://localhost:8448', json.dumps(data))

python/giv-client-pick-coordinate.py

100644100755
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
#!/usr/bin/python
22

3-
import httplib,json
3+
######################################################################
4+
# An example of how to pick a coordinate with giv.
5+
#
6+
# 2023-11-26 Sun
7+
# Dov Grobgeld <[email protected]>
8+
######################################################################
49

5-
conn = httplib.HTTPConnection('localhost:8448')
10+
import requests,json
611

7-
request = {'method':"pick_coordinate",
8-
'params':[]}
12+
def get_giv_coordinate(port=8448):
13+
'''Returns x,y,button,modifiers'''
14+
data = {'method':"pick_coordinate",
15+
'params':[]}
16+
17+
r = requests.post(f'http://localhost:{port}', json.dumps(data))
18+
res = json.loads(r.text)
19+
return res['result']
920

10-
conn.request("POST", url='', body= json.dumps(request))
11-
response = json.loads(conn.getresponse().read())
12-
if 'error' in response:
13-
print response['error']['message']
14-
else:
15-
print response['result']
21+
x,y,button,modifiers = get_giv_coordinate()
22+
print(f'{x=} {y=} {button=} {modifiers=}')
1623

python/giv-client.py

100644100755
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/python
2+
######################################################################
3+
# A minimal json client
4+
#
5+
# 2023-11-26 Sun
6+
# Dov Grobgeld <[email protected]>
7+
######################################################################
8+
import requests,json
9+
10+
data = {'method':"load_file",
11+
'params':['/home/dov/github/giv/examples/maja.pgm']} # Full path!
12+
r = requests.post(f'http://localhost:8448', json.dumps(data))
213

3-
import httplib,json
4-
5-
conn = httplib.HTTPConnection('localhost:8448')
6-
7-
request = {'method':"load_file",
8-
'params':['/home/dov/github/giv/examples/lena.pgm']} # Full path!
9-
10-
conn.request("POST", url='', body= json.dumps(request))
11-
response = json.loads(conn.getresponse().read())['result']
12-
print response

python/giv.py

100644100755
File mode changed.

python/setup.py

100644100755
File mode changed.

src/giv-win.gob

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ cb_button_press_event (GtkWidget * widget,
17171717

17181718
giv_widget_popdown_balloon(GIV_WIDGET(selfp->w_imgv));
17191719

1720-
if (button!= 2 && selfp->do_pick_coordinate) {
1720+
if (event->button!= 2 && selfp->do_pick_coordinate) {
17211721
double world_x, world_y;
17221722
gtk_image_viewer_canv_coord_to_img_coord(GTK_IMAGE_VIEWER(selfp->w_imgv),
17231723
event->x, event->y,

0 commit comments

Comments
 (0)