Wynum Database Client

Wynum gRPC API

WynumDbClient is an API to access wynum stage data to perform CRUD operations(create, read, update and delete). It is a wrapper for the wynum gRPC client. For more info on gRPC please refer https://grpc.io/docs/guides/​

Create client

from wynumdbclient import WynumDbClient
​
# the secret for the project
secret = '575f99cb435b63dc8a1dd33809a59fe5iw'
​
client = WynumDbClient(secret)

Post Data

To post data to a particular stage just call client.post_data. The post_data method takes following arguments.

  • stage_name: str the name of the stage to which you want to post the data

  • data: dict the dictionary containing the data record for the stage. The format for the dict is simple. {'var_name': value}. Where var_name is the name of the variable. Please remember that if you are posting data for a date variable then the format for date should be dd/mm/yyyy. Otherwise the post request will fail.

The post_data method returns a response object. You can check response.error to check if the request failed or was successful . response.error_message gives the error description.

There is no need to pass the unique identifier of the project. If you don't pass the unique identifier it will be created for you automatically. But if you pass the unique identifier and if that identifier already exists then that particular record will be updated rather than creating new record.

data = {'Patient_name': 'patient1', 'Birth date': '26/12/2018', 'state': 'UK'}
res = client.post_data('Patients',data)
print(res.error)

Update Data

Call client.update_data to update a record. The call to update_data is exactly similar to post_data. Including unique identifier in update request is mandatory for update_data otherwise the request will fail.

data = {'Patient_name': 'patient1', 'state': 'MH'}
res = client.post_data('Patients',data)
print(res.error)

In this example Patient_name is the unique id. The above example updates the state of the record with id patient1.

Delete Data

To delete a data record of particular id or multiple ids call client.delete_data. delete_data takes following arguments.

  • stage_name: str the name of the stage.

  • ids : list the list of unique ids of which you want to delete data. If stage_name is None or absent then data of the whole project will be deleted. Also if stage_name is present and if the ids argument id None or empty then all the data of that stage is deleted.

The delete_data method returns a response object. You can check response.error to check if the request failed or was successful . response.error_message gives the error description.

res = client.delete_data('Patients', ids=['patient1'])
print(res.error)

Send Message

To send a message to wynum use client.send_message method.

  • sender_email : str The sender email id as string

  • receiver_email: str The receiver email id as string

  • message:str The message text

  • api_token: str The api token to which you want to send the message

  • components_url: str Use this argument if you to send form message. The component url for the form.

  • post_url : str Use this argument if you to send form message. The post url of the form

  • submission_id: str Use this argument if you to send form message. The submission id to use while submitting the form.

  • stage_name: str The stage name to which the form belongs

  • response : str The markdown string for rich text messages.

  • div : str Use this argument if you want to send chart. The html div element as str

  • src: str Use this argument if you want to send chart. The html src element.

Client.send_mesage(sender_email = 'hello@wynum.com',
receiver_email = input['user_id'],
message = "Prediction Result " + str(output['prediction']) ,
api_token = '3257293',
submission_id = input['id'],
stage_name = 'Prediction',
response = ' '
)

Get data

The call to get_data is a bit complicated for now. We are actively working on it to improve the API.

To get the data call client.get_data. The get_data method takes only one argument filters which is a dict. The format of the dict is explained below.

  • variables: list If you want to get the data of particular variables then assign the list of variables to this key.

  • stages: dict or list The stages can be a dict or a list of stage names. If you pass the list of stage names then the data of that stages will be returned. If you want to apply filters to data then stages needs to be a dict with keys being the stage names. For ex. If Patients is the stage and you want to apply filters to it then use the following format. json filters = { 'stages': { 'Patients': { 'filters': [ {'var': 'Height', 'op': '<=', 'value': 60}, ] } } } a stage can have multiple filters so filters is a list. The format for a filter is as follows json { 'var': 'var_name', 'op': 'operation', 'value': value} the ops can be one of the following

  • > greater than

  • >= greater than equal to

  • < less than

  • <=: less than equal to

  • == equal to

  • != not equal to

You can also add multiple filters for one stage as follows. If you include multiple operators then you'll need to provide the logical operator (and, or) to join the filters.

filters = {
'stages': {
'Patients': {
'filters': [
{'var': 'Height', 'op': '<=', 'value': 60},
{'var': 'Weight', 'op': '<=', 'value': 60}
],
'operator': 'and'
}
}
}
  • limit: int limit specifies the number of records to fetch.

  • sortBy: str It can be either 'asc' or 'desc'. This key provides the sorting order. Default value is 'asc'

  • orderBy: str use orderBy to specify the variable name on which to sort the data. Default value is 'created_at'

examples

  • ### Get all data of all stages python res = client.get_data(filters=None) print(res)

res is a dict with following format

{
'stage_name': list of data dicts for the stage
}

for ex below is a sample response

filters = {
'stages': ['Patients', 'Records'],
'limit': 5,
}
res = client.get_data(filters=filters)
print(res)

response

{'Patients': [{'Height': 157, 'Weight': 50, 'node': 'Patients'},
{'Height': 163, 'Weight': 87, 'node': 'Patients'},
{'Height': 165, 'Weight': 86, 'node': 'Patients'}],
'Records': [{'node': 'Records'}, {'node': 'Records'}]}

We are working on adding more elaborate examples. Also in future we will add QueryBuilder to simplify the specifications of filters. Till then you'll need to write filters in above mentioned format. We are sorry for the inconvenience.