Skip to content
Snippets Groups Projects
sample.py 2.24 KiB
Newer Older
  • Learn to ignore specific revisions
  • from notebook.base.handlers import IPythonHandler
    import numpy as np
    
    from .connection import openbis_connections
    
    
    
    def get_entity_for_identifier(conn, identifier):
        entity = None
        try:
            entity = conn.openbis.get_sample(identifier)
        except Exception as exc:
            pass
    
                entity = conn.openbis.get_experiment(identifier)
    
            except Exception as exc:
    
        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 dataset list 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
    
    
            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,
                    "entity_attrs": entity.attrs.all(),
                    "entity_props": entity.props.all(),
                    "cwd": os.getcwd()