phenomedb.base_view

class phenomedb.base_view.PhenomeDBBaseView

The base view for all PhenomeDB views, for common methods and db_session usage. Extends the flask_appbuilder.BaseView.

attribute_dict(orm_object)

get an ORM object”s (eg a phenomedb.models.Project) attributes and return as dict

Parameters:

orm_object (phenomedb.models.*) – The model object.

Returns:

The dictionary version of the model.

Return type:

dict

configure_logging(identifier='phenomedb', log_file='phenomedb.log', level=10)

Setup a logger.

Parameters:
  • identifier (str, optional) – an identifier for your messages in the log, defaults to ‘phenomedb’.

  • log_file (str, optional) – file to log to at location specified in config.ini; (will create this dir if necessary), defaults to ‘phenomedb.log’.

  • level (int, optional) – log level, logging.INFO, logging.ERROR, logging.WARNING, defaults to logging.DEBUG.

Returns:

the logger.

Return type:

logging.logger

delete_entity(table_name, id)

Delete a row from a table.

Parameters:
  • table_name (str) – The name of the table.

  • id (int) – The id of the row to delete.

delete_rows_by_id(table_class, ids)

Delete rows from a table by id.

Parameters:
  • table_class (str) – The table name.

  • ids (list) – A list of ids to delete.

execute_sql(sql, params={})

Execute an SQL statement

Parameters:
  • sql (str) – The SQL statement.

  • params (dict, optional) – The SQL parameters, defaults to {}.

Returns:

List of result row dictionaries.

Return type:

list

flask_form_to_ORM_dict(form)

Takes a request.form object and returns a dictionary, having removed the csrf_token.

Parameters:

form (Flask.request.Form) – the flask request.form object.

Returns:

The form key/value pairs as a dictionary.

Return type:

dict

foreign_keys(cl, key_name=None)

Finds the foreign key columns in an ORM class.

Parameters:
  • cl (phenomedb.models.*) – The model class.

  • key_name (str, optional) – optionally specify the key to find, defaults to None

Returns:

The column name/value pairs as a dictionary.

Return type:

dict

get_all_by_model(table_name)

Get all records by table_name.

Parameters:

table_name (str) – The name of the table to query.

Returns:

The list of model records.

Return type:

list

get_class_by_tablename(table_name)

Get model class by table name.

Parameters:

table_name (str) – The name of the table.

Returns:

The related model class reference.

Return type:

phenomedb.models.*

get_entities_as_dicts(entity_list)

Convert a SQLAlchemy result list of objects into a list of dictionaries for ease of use in the web interface - note no related entities

Parameters:

entity_list (list) – Results list of query.

Returns:

list of dictionaries.

Return type:

list

get_entity_as_df(table_name, id)

Fetch a single row from a table.

Parameters:
  • table_name (str) – The name of the table.

  • id (int) – The entity id.

Returns:

Pandas dataframe of results.

Return type:

pandas.DataFrame

get_entity_as_dict(table_name, entity_id, with_relations=False)

Represent a SQLAlchemy mapped table object and optionally its related table entries as a dictionary of column:value mappings

Parameters:
  • table_name (str) – The name of the table.

  • id (int) – The entity id.

  • with_relations (bool, optional) – add top-level related entities to result, defaults to False

Returns:

Results as dictionary

Return type:

dict

get_project_metadata_fields(project_id)

Get the metadata fields for a project.

Parameters:

project_id (int) – The id of the project.

Returns:

A list of dictionary metadata_fields for the project.

Return type:

list

get_relations_by_fk(table_name, key_name, key_value, with_relations=False)

Get the table rows where the key==value

Parameters:
  • table_name (str) – The table name.

  • key_name (str) – The id field to link on, eg “compound_id”

  • key_value (int) – a value for the key id

  • with_relations (bool, optional) – include the first level related entities in the result, defaults to False

Returns:

The list of results.

Return type:

list

get_table_columns(table_name)

Get the columns of a table.

Parameters:

table_name (str) – The table name.

Returns:

Dictionary of attribute names and types.

Return type:

dict

get_table_names()

Get a list of all tables in database

Returns:

list of table names.

Return type:

list

handle_json_error(e)

Handle a json error.

Parameters:

e (dict) – The error message dictionary.

Returns:

The response 400.

Return type:

flask.response

insert_entity(table_name, value_dict)

Create a new row in a table.

Parameters:
  • table_name (str) – The name of the table.

  • value_dict (dict) – A dictionary containing correctly mapped values.

Returns:

The newly inserted id.

Return type:

int

relationship_dict(orm_object)

Get an ORM object”s related entities and return as dict

Parameters:

orm_object (phenomedb.models.*) – The model object.

Returns:

The attribute dictionary plus related entities.

Return type:

dict

set_db(db_env)

Set the db using the db_env

Parameters:

db_env (str) – “PROD”, “BETA”, or “TEST”, default “PROD”

set_db_session(request)

Set the db_session. If db_env is in the get params, set it.

Parameters:

request (flask.request) – The flask request object.

sql_to_dataframe(sql, params={})

Execute an sql statement and return as pandas dataframe.

Parameters:
  • sql (str) – The SQL statement.

  • params (dict, optional) – The SQL parameters, defaults to {}.

Returns:

Pandas dataframe of results.

Return type:

pandas.DataFrame

update_entity(table_name, update_dict)

Update a single row in a table.

Parameters:
  • table_name (str) – The name of the table.

  • update_dict (dict) – The dictionary of the new values, including the id.