From 9a6314bd16209e4e4443dbe226f56e8f1607bd7d Mon Sep 17 00:00:00 2001 From: Fabian Sinz Date: Tue, 27 Oct 2015 10:32:45 -0500 Subject: [PATCH] add _repr_html_ for ipython notebook output --- datajoint/relational_operand.py | 36 ++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/datajoint/relational_operand.py b/datajoint/relational_operand.py index f3e25b0d2..cd2f180d9 100644 --- a/datajoint/relational_operand.py +++ b/datajoint/relational_operand.py @@ -4,7 +4,7 @@ import re from copy import copy import logging -from . import DataJointError +from . import DataJointError, config from .fetch import Fetch, Fetch1 @@ -17,6 +17,7 @@ class AndList(Sequence): Each restriction can be a list or set or a relation whose elements are ORed. But the elements that are lists can contain """ + def __init__(self, heading): self.heading = heading self._list = [] @@ -41,6 +42,7 @@ def where_clause(self): """ convert to a WHERE clause string """ + def make_condition(arg, _negate=False): if isinstance(arg, (str, AndList)): return str(arg), _negate @@ -51,12 +53,12 @@ def make_condition(arg, _negate=False): if not common_attributes: condition = 'FALSE' if negate else 'TRUE' else: - common_attributes = '`'+'`,`'.join(common_attributes)+'`' + common_attributes = '`' + '`,`'.join(common_attributes) + '`' condition = '({fields}) {not_}in ({subquery})'.format( fields=common_attributes, not_="not " if negate else "", subquery=arg.make_select(common_attributes)) - return condition, False # negate is cleared + return condition, False # negate is cleared # mappings are turned into ANDed equality conditions if isinstance(arg, Mapping): @@ -244,6 +246,33 @@ def __repr__(self): ret += ' & %r' % self._restrictions return ret + def _repr_html_(self): + limit = config['display.limit'] + rel = self.project(*self.heading.non_blobs) # project out blobs + columns = rel.heading.names + content = dict( + head=''.join(columns), + body=''.join( + ['\n'.join(['%s' % column for column in tup]) for tup in rel.fetch(limit=limit)]), + tuples=len(rel) + ) + + + return """
\n + \n + \n + \n + + \n + + + %(body)s + + \n
+ %(head)s +
\n

%(tuples)i tuples

\n
+ """ % content + def make_select(self, select_fields=None): return 'SELECT {fields} FROM {from_}{where}{group}'.format( fields=select_fields if select_fields else self.select_fields, @@ -298,6 +327,7 @@ class Not: """ invert restriction """ + def __init__(self, restriction): self.restriction = restriction