datagrok-api 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from datagrok_api.datagrok_client import DatagrokClient
2
+
3
+ '''Datagrok Public API Python wrapper'''
@@ -0,0 +1,184 @@
1
+ import requests
2
+ import pandas as pd
3
+ import json
4
+ from io import StringIO
5
+
6
+ class DatagrokClient:
7
+ def __init__(self, api_key, base_url):
8
+ '''
9
+ Create a datagrok api client.
10
+
11
+ Parameters
12
+ ----------
13
+ api_key: str
14
+ Api key for a client. Can be obtained from user profile page.
15
+ Starts with "Bearer".
16
+ base_url: str
17
+ Url of datagrok API. For example, "https://public.datagrok.ai/api"
18
+
19
+ Returns
20
+ -------
21
+ DatagrokClient
22
+ New instance of Datagrok Client
23
+ '''
24
+ self.api_key = api_key
25
+ self.base_url = base_url
26
+ self.headers = {
27
+ "Authorization": self.api_key
28
+ }
29
+
30
+ def download_table(self, name):
31
+ '''
32
+ Downloads a table from Datagrok.
33
+
34
+ Parameters
35
+ ----------
36
+ name: str
37
+ Identifier of a table. Can be accessed from context panel.
38
+ Several options supppored: UUID, Grok names (e.g. Namespace.Project.Table)
39
+
40
+ Returns
41
+ -------
42
+ pandas.DataFrame
43
+ Dataframe with table data
44
+ '''
45
+ endpoint = f"/public/v1/tables/{name}"
46
+ response = self._request('GET', endpoint, content_type="text/plain")
47
+ return pd.read_csv(StringIO(response.text))
48
+
49
+ def upload_table(self, name, dataframe):
50
+ '''
51
+ Uploads a table to Datagrok.
52
+
53
+ Parameters
54
+ ----------
55
+ name: str
56
+ Name for new table.
57
+ If in grok format, can also specify project and namespace.
58
+ dataframe: pandas.Dataframe
59
+ table data to save
60
+
61
+ Returns
62
+ -------
63
+ object
64
+ set of identifiers for the uploaded table
65
+ '''
66
+ endpoint = f"/public/v1/tables/{name}"
67
+ csv_data = dataframe.to_csv(index=False)
68
+ response = self._request('POST', endpoint, content_type="text/csv", data=csv_data)
69
+ return response.json()
70
+
71
+ def download_file(self, connector, path):
72
+ '''
73
+ Download a file from Datagrok.
74
+
75
+ Parameters
76
+ ----------
77
+ connector: str
78
+ Identifier of a connector: ID or Grok name.
79
+ path: str
80
+ Path to file in a connector
81
+
82
+ Returns
83
+ -------
84
+ object
85
+ If requested file is csv, returns corresponding pd.Datafrme.
86
+ Otherwise returns list of bytes with file content.
87
+ '''
88
+ endpoint = f"/public/v1/files/{connector}/{path}"
89
+ response = self._request('GET', endpoint, content_type="application/octet-stream")
90
+ if path.endswith('.csv'):
91
+ return pd.read_csv(StringIO(response.content.decode('utf-8')))
92
+ return response.content
93
+
94
+ def upload_file(self, connector, path, file_path):
95
+ '''
96
+ Uploads a file to Datagrok.
97
+
98
+ Parameters
99
+ ----------
100
+ connector: str
101
+ Identifier of a connector: ID or Grok name.
102
+ path: str
103
+ Path to file in a connector
104
+ file_path: str
105
+ Path to local file that needs to be uploaded
106
+ '''
107
+ endpoint = f"/public/v1/files/{connector}/{path}"
108
+ with open(file_path, 'rb') as file:
109
+ self._request('POST', endpoint, content_type="application/octet-stream", data=file)
110
+
111
+ def share_dashboard(self, id, groups, access="View"):
112
+ '''
113
+ Shares a dashboard.
114
+
115
+ Parameters
116
+ ----------
117
+ id: str
118
+ Identifier of a project: ID or Grok name.
119
+ groups: str
120
+ Comma-separated list of group/user names
121
+ access: str
122
+ Either 'View' or 'Edit'
123
+ '''
124
+ endpoint = f"/public/v1/dashboards/{id}/shares"
125
+ params = {
126
+ 'groups': groups,
127
+ 'access': access
128
+ }
129
+ self._request('GET', endpoint, params=params)
130
+
131
+ def create_dashboard(self, name, table_ids, layout_filename=None):
132
+ '''
133
+ Shares a dashboard.
134
+
135
+ Parameters
136
+ ----------
137
+ name: str
138
+ Name for new dashboard.
139
+ table_ids: str
140
+ Comma-separated list of table ids
141
+ layout_filename: str
142
+ Optional. Filename for a project layout.
143
+
144
+ Returns
145
+ -------
146
+ object
147
+ Identifiers of a project.
148
+ '''
149
+ endpoint = f"/public/v1/dashboards/{name}/{table_ids}"
150
+ if layout_filename:
151
+ with open(layout_filename, 'r') as layout_file:
152
+ layout = json.load(layout_file)
153
+ response = self._request('POST', endpoint, json=layout, content_type="application/json")
154
+ else:
155
+ response = self._request('POST', endpoint, content_type="application/json")
156
+ return response.json()
157
+
158
+ def call_function(self, name, invocation_parameters):
159
+ '''
160
+ Performs a call of datagrok function.
161
+
162
+ Parameters
163
+ ----------
164
+ name: str
165
+ Function name.
166
+ invocation_parameters: dict
167
+ Dict with parameter values for function call.
168
+
169
+ Returns
170
+ -------
171
+ object
172
+ Result of invocation: either a single value or a list or outputs.
173
+ '''
174
+ endpoint = f"/public/v1/{name}/call"
175
+ response = self._request('POST', endpoint, json=invocation_parameters, content_type="application/json")
176
+ return response.json()
177
+
178
+ def _request(self, method, endpoint, content_type="application/json", **kwargs):
179
+ url = f"{self.base_url}{endpoint}"
180
+ headers = self.headers.copy()
181
+ headers["Content-Type"] = content_type
182
+ response = requests.request(method, url, headers=headers, **kwargs)
183
+ response.raise_for_status()
184
+ return response
@@ -0,0 +1,77 @@
1
+ Metadata-Version: 2.3
2
+ Name: datagrok-api
3
+ Version: 0.0.1
4
+ Summary: Client library for integration with datagrok API
5
+ Project-URL: Homepage, https://github.com/datagrok-ai/public
6
+ Project-URL: Issues, https://github.com/datagrok-ai/public/issues
7
+ Author-email: Andrew Skalkin <askalkin@datagrok.ai>, Amelichev Konstantin <kamelichev@datagrok.ai>
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Requires-Python: >=3.5
12
+ Description-Content-Type: text/markdown
13
+
14
+ # Datagrok python client library
15
+
16
+ This library can be used for integration with datagrok. It is a python wrapper for public API, that has OpenAPI specification available [here](http://public.datagrok.ai/api/public/api.yaml).
17
+
18
+ ## Installation
19
+
20
+ To install package, use [pip](https://pypi.org/project/pip/).
21
+
22
+ ```shell
23
+ pip install datagrok-api
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ To use API client, import DatagrokClient to your project:
29
+
30
+ ```python
31
+ from datagrok_api import DatagrokClient
32
+
33
+ api = DatagrokClient('your token', 'datagrok url')
34
+ ```
35
+
36
+ ## License
37
+
38
+ See [License.md](https://github.com/datagrok-ai/public/blob/master/LICENSE.md).
39
+
40
+ ## Examples
41
+
42
+ The package uses Pandas for representation of tables and dataframes.
43
+
44
+ ```python
45
+ from datagrok_api import DatagrokClient
46
+ import pandas as pd
47
+ from sklearn.datasets import load_iris
48
+
49
+ api = DatagrokClient('your token', 'datagrok url')
50
+
51
+ # Calls a Datagrok function
52
+ # Dataframes, columns and primitive data types are supported
53
+ api.call_function('Abs', {
54
+ 'x': -3
55
+ })
56
+
57
+ # Downloads file from a given connection
58
+ res = api.download_file('system.demofiles', 'demog.csv')
59
+ print(res.head())
60
+
61
+ # Uploads iris dataset as a table
62
+ iris_id = api.upload_table('iris', pd.DataFrame(load_iris()['data']))
63
+
64
+ # Iris also can be uploaded as a file
65
+ # api.upload_file('system.demofiles', 'iris.csv', 'iris.csv')
66
+
67
+ # Fetches freshly uploaded Iris table
68
+ res = api.download_table(iris_id)
69
+ print(res.head())
70
+
71
+ # Creates dashboard from iris table with layout uploaded from file "iris.layout"
72
+ dashboard_id = api.create_dashboard('python-test', iris_id, layout_filename='iris.layout')
73
+
74
+ # Shares dashboard with admin
75
+ api.share_dashboard(dashboard_id, 'Test')
76
+
77
+ ```
@@ -0,0 +1,5 @@
1
+ datagrok_api/__init__.py,sha256=JULs6mWeyF7kRcn_J1SXvhYjtpt3jZkAWXCJiwAY754,98
2
+ datagrok_api/datagrok_client.py,sha256=QHCUTT_wNbcOwr1tvl3-MlwsUUIsFZPVBnWbLno8hDE,5704
3
+ datagrok_api-0.0.1.dist-info/METADATA,sha256=PlGsdMuCYG-ceAB8pXCYlh-DCJIJicN9NrYJtEA7jgc,2203
4
+ datagrok_api-0.0.1.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
5
+ datagrok_api-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.24.2
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any