-
Notifications
You must be signed in to change notification settings - Fork 32
/
session.py
42 lines (32 loc) · 1.1 KB
/
session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import requests
import json
from visualization import Visualization
import webbrowser
class Session(object):
name = None
visualizations = []
def __init__(self, host=None, id=None, json=None):
self.host = host
self.id = id
if json:
self.id = json.get('id')
self.name = json.get('name')
def __str__(self):
if self.name:
return self.name
return str(self.id)
def create_visualization(self, data=None, images=None, type=None):
viz = Visualization.create(session=self, data=data, images=images, type=type)
self.visualizations.append(viz)
return viz
def open(self):
webbrowser.open(self.host + '/sessions/' + str(self.id) + '/feed/')
@classmethod
def create(cls, host, name=None):
url = host + '/sessions/'
payload = {}
if name:
payload = {'name': name}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
return cls(host=host, json=r.json())