Skip to content
Snippets Groups Projects
sample.py 1.71 KiB
Newer Older
  • Learn to ignore specific revisions
  • from notebook.base.handlers import IPythonHandler
    import numpy as np
    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)
                    })
    
            sample = None
            try:
                sample = conn.openbis.get_sample(permId)
            except Exception as exc:
                self.set_status(404)
                self.write({
                    "reason" : 'No such sample: {}'.format(permId)
                })
            if sample is None:
                return
    
            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
    
    
        def get(self, **params):
            """Handle a request to /openbis/sample/connection_name/permId
            download the data and return a message
            """
            try:
                conn = openbis_connections[params['connection_name']]
            except KeyError:
                self.write({
                    "reason" : 'connection {} was not found'.format(
                        params['connection_name']
                    )
                })
                return
    
            datasets = self.get_datasets(conn, params['permId'])
            if datasets is not None:
                self.set_status(200)
                self.write({
                    "dataSets": datasets
                })