forked from datajoint/datajoint-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.py
More file actions
153 lines (146 loc) · 4.69 KB
/
preview.py
File metadata and controls
153 lines (146 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""methods for generating previews of query expression results in python command line and Jupyter"""
from .settings import config
def preview(query_expression, limit, width):
heading = query_expression.heading
rel = query_expression.proj(*heading.non_blobs)
if limit is None:
limit = config["display.limit"]
if width is None:
width = config["display.width"]
tuples = rel.fetch(limit=limit + 1, format="array")
has_more = len(tuples) > limit
tuples = tuples[:limit]
columns = heading.names
widths = {
f: min(
max(
[len(f)] + [len(str(e)) for e in tuples[f]]
if f in tuples.dtype.names
else [len("=BLOB=")]
)
+ 4,
width,
)
for f in columns
}
templates = {f: "%%-%d.%ds" % (widths[f], widths[f]) for f in columns}
return (
" ".join(
[templates[f] % ("*" + f if f in rel.primary_key else f) for f in columns]
)
+ "\n"
+ " ".join(["+" + "-" * (widths[column] - 2) + "+" for column in columns])
+ "\n"
+ "\n".join(
" ".join(
templates[f] % (tup[f] if f in tup.dtype.names else "=BLOB=")
for f in columns
)
for tup in tuples
)
+ ("\n ...\n" if has_more else "\n")
+ (" (Total: %d)\n" % len(rel) if config["display.show_tuple_count"] else "")
)
def repr_html(query_expression):
heading = query_expression.heading
rel = query_expression.proj(*heading.non_blobs)
info = heading.table_status
tuples = rel.fetch(limit=config["display.limit"] + 1, format="array")
has_more = len(tuples) > config["display.limit"]
tuples = tuples[0 : config["display.limit"]]
css = """
<style type="text/css">
.Table{
border-collapse:collapse;
}
.Table th{
background: #A0A0A0; color: #ffffff; padding:4px; border:#f0e0e0 1px solid;
font-weight: normal; font-family: monospace; font-size: 100%;
}
.Table td{
padding:4px; border:#f0e0e0 1px solid; font-size:100%;
}
.Table tr:nth-child(odd){
background: #ffffff;
color: #000000;
}
.Table tr:nth-child(even){
background: #f3f1ff;
color: #000000;
}
/* Tooltip container */
.djtooltip {
}
/* Tooltip text */
.djtooltip .djtooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Position the tooltip text - see examples below! */
position: absolute;
z-index: 1;
}
#primary {
font-weight: bold;
color: black;
}
#nonprimary {
font-weight: normal;
color: white;
}
/* Show the tooltip text when you mouse over the tooltip container */
.djtooltip:hover .djtooltiptext {
visibility: visible;
}
</style>
"""
head_template = """<div class="djtooltip">
<p id="{primary}">{column}</p>
<span class="djtooltiptext">{comment}</span>
</div>"""
return """
{css}
{title}
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="Table">
<thead> <tr style="text-align: right;"> <th> {head} </th> </tr> </thead>
<tbody> <tr> {body} </tr> </tbody>
</table>
{ellipsis}
{count}</div>
""".format(
css=css,
title="" if info is None else "<b>%s</b>" % info["comment"],
head="</th><th>".join(
head_template.format(
column=c,
comment=heading.attributes[c].comment,
primary=(
"primary" if c in query_expression.primary_key else "nonprimary"
),
)
for c in heading.names
),
ellipsis="<p>...</p>" if has_more else "",
body="</tr><tr>".join(
[
"\n".join(
[
"<td>%s</td>"
% (tup[name] if name in tup.dtype.names else "=BLOB=")
for name in heading.names
]
)
for tup in tuples
]
),
count=(
("<p>Total: %d</p>" % len(rel))
if config["display.show_tuple_count"]
else ""
),
)