Skip to content
Snippets Groups Projects
Commit 0c0c7113 authored by Swen Vermeul's avatar Swen Vermeul
Browse files

return sample/experiment attributes and properties when asked for datasets

parent ca1e3eba
No related branches found
No related tags found
No related merge requests found
from notebook.base.handlers import IPythonHandler
import numpy as np
import os
from .connection import openbis_connections
class SampleHandler(IPythonHandler):
"""Handle the requests for /openbis/sample/connection/permId"""
def get_datasets(self, conn, permId):
if not conn.is_session_active():
try:
conn.login()
except Exception as exc:
self.write({
"reason" : 'connection to {} could not be established: {}'.format(conn.name, exc)
})
def get_entity_for_identifier(conn, identifier):
entity = None
try:
entity = conn.openbis.get_sample(identifier)
except Exception as exc:
pass
sample = None
if entity is None:
try:
sample = conn.openbis.get_sample(permId)
entity = conn.openbis.get_experiment(identifier)
except Exception as exc:
self.set_status(404)
self.write({
"reason" : 'No such sample: {}'.format(permId)
})
if sample is None:
return
pass
return entity
def get_datasets(entity):
datasets = sample.get_datasets().df
datasets.replace({np.nan:None}, inplace=True) # replace NaN with None, otherwise we cannot convert it correctly
return datasets.to_dict(orient='records') # is too stupid to handle NaN
datasets = entity.get_datasets().df
datasets.replace({np.nan:None}, inplace=True) # replace NaN with None, otherwise we cannot convert it correctly
datasets_dict = datasets.to_dict(orient='records') # is too stupid to handle NaN
return datasets_dict
class SampleHandler(IPythonHandler):
"""Handle the requests for /openbis/sample/connection/permId"""
def get(self, **params):
"""Handle a request to /openbis/sample/connection_name/permId
download the data and return a message
download the dataset list and return a message
"""
try:
conn = openbis_connections[params['connection_name']]
except KeyError:
self.set_status(500)
self.write({
"reason" : 'connection {} was not found'.format(
params['connection_name']
......@@ -44,11 +46,31 @@ class SampleHandler(IPythonHandler):
})
return
datasets = self.get_datasets(conn, params['permId'])
if not conn.is_session_active():
try:
conn.login()
except Exception as exc:
self.set_status(500)
self.write({
"reason" : 'connection to {} could not be established: {}'.format(conn.name, exc)
})
return
entity = get_entity_for_identifier(conn, params['identifier'])
if entity is None:
self.set_status(404)
self.write({
"reason" : 'No such Sample or Experiment: {}'.format(identifier)
})
return None
datasets = get_datasets(entity)
if datasets is not None:
self.set_status(200)
self.write({
"dataSets": datasets
"dataSets": datasets,
"entity_attrs": entity.attrs.all(),
"entity_props": entity.props.all(),
"cwd": os.getcwd()
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment