Skip to content

Commit

Permalink
Merge pull request #8 from intake/add_repr
Browse files Browse the repository at this point in the history
Add _repr_html_ for DremioCatalog
  • Loading branch information
philippjfr authored Nov 26, 2021
2 parents 2497c82 + c025328 commit 0cf1018
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ include README.md
global-exclude *.py[co]
global-exclude *~
global-exclude *.ipynb_checkpoints/*
include intake_dremio/static/style.css
59 changes: 58 additions & 1 deletion intake_dremio/dremio_cat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import uuid
import pkg_resources

from functools import lru_cache

from intake.catalog.base import Catalog
from intake.catalog.local import LocalCatalogEntry

from . import __version__
from .intake_dremio import DremioSource

STATIC_FILES = ("static/css/style.css",)

@lru_cache(None)
def _load_static_files():
"""Lazily load the resource files into memory the first time they are needed"""
return [
pkg_resources.resource_string("intake_dremio", fname).decode("utf8")
for fname in STATIC_FILES
]


class DremioCatalog(Catalog):

Expand Down Expand Up @@ -31,11 +46,53 @@ def _create_entry(self, row):
description = f'Dremio {row.TABLE_TYPE} {name} from {self._source._hostname}'
args = dict(self._source._init_args, sql_expr=f'SELECT * FROM {name}')
e = LocalCatalogEntry(name, description, 'dremio', True,
args, {}, {}, {}, "", getenv=False,
args, {}, {}, {}, "", getenv=True,
getshell=False)
e._plugin = [DremioSource]
self._entries[name] = e

def _repr_html_(self):
(css_style,) = _load_static_files()
uid = str(uuid.uuid4())
info = self._source._init_args
entries = []
for e in self:
euid = str(uuid.uuid4())
erepr = repr(self[e])
entry = f"""
<li class="dr-entry-item">
<div class="dr-entry-name"><span>{e}</span></div>
<input id="entry-{euid}" class="dr-entry-data-in" type="checkbox"></input>
<label for="entry-{euid}" class="dr-entry-data-label"><b>+</b></label>
<div class="dr-entry-data">
<pre>{erepr}</pre>
<br>
</div>
</li>
"""
entries.append(entry)
entry_html = '\n'.join(entries)
return f"""
<div class="dr-wrap">
<style>{css_style}</style>
<div class="dr-header">
<div class="dr-obj-type">DremioCatalog: {info['uri']}</div>
</div>
<ul class="dr-sections">
<li class="dr-section-item">
<input id="section-{uid}" class="dr-section-summary-in" type="checkbox"></input>
<label for="section-{uid}" class="dr-section-summary">Entries</label>
<div class="dr-section-inline-details">{len(self)}</div>
<div class="dr-section-details">
<ul class="dr-entry-list">
{entry_html}
</ul>
</div>
</li>
</ul>
</div>
"""

def create_source(self, sql_expr):
"""
Create a DremioSource from the provided sql_expr with the
Expand Down
181 changes: 181 additions & 0 deletions intake_dremio/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
:root {
--dr-font-color0: var(--jp-content-font-color0, rgba(0, 0, 0, 1));
--dr-font-color2: var(--jp-content-font-color2, rgba(0, 0, 0, 0.54));
--dr-font-color3: var(--jp-content-font-color3, rgba(0, 0, 0, 0.38));
--dr-border-color: var(--jp-border-color2, #e0e0e0);
--dr-disabled-color: var(--jp-layout-color3, #bdbdbd);
--dr-background-color: var(--jp-layout-color0, white);
--dr-background-color-row-even: var(--jp-layout-color1, white);
--dr-background-color-row-odd: var(--jp-layout-color2, #eeeeee);
}

html[theme=dark],
body.vscode-dark {
--dr-font-color0: rgba(255, 255, 255, 1);
--dr-font-color2: rgba(255, 255, 255, 0.54);
--dr-font-color3: rgba(255, 255, 255, 0.38);
--dr-border-color: #1F1F1F;
--dr-disabled-color: #515151;
--dr-background-color: #111111;
--dr-background-color-row-even: #111111;
--dr-background-color-row-odd: #313131;
}

.dr-wrap {
display: block;
min-width: 300px;
max-width: 700px;
}

.dr-text-repr-fallback {
/* fallback to plain text repr when CSS is not injected (untrusted notebook) */
display: none;
}

.dr-header {
padding-top: 6px;
padding-bottom: 6px;
margin-bottom: 4px;
border-bottom: solid 1px var(--dr-border-color);
}

.dr-header > div,
.dr-header > ul {
display: inline;
margin-top: 0;
margin-bottom: 0;
}

.dr-obj-type,
.dr-array-name {
margin-left: 2px;
margin-right: 10px;
}

.dr-obj-type {
color: var(--dr-font-color2);
}

.dr-sections {
padding-left: 0 !important;
display: grid;
grid-template-columns: 400px auto auto 1fr 20px 20px;
}

.dr-section-item {
display: contents;
}

.dr-section-item input {
display: none;
}

.dr-section-item input + label {
color: var(--dr-disabled-color);
}

.dr-section-item input:enabled + label {
cursor: pointer;
color: var(--dr-font-color2);
}

.dr-section-item input:enabled + label:hover {
color: var(--dr-font-color0);
}

.dr-section-summary {
grid-column: 1;
color: var(--dr-font-color2);
font-weight: 500;
}

.dr-section-summary > span {
display: inline-block;
padding-left: 0.5em;
}

.dr-section-summary-in:disabled + label {
color: var(--dr-font-color2);
}

.dr-section-summary-in + label:before {
display: inline-block;
content: '►';
font-size: 11px;
width: 15px;
text-align: center;
}

.dr-section-summary-in:disabled + label:before {
color: var(--dr-disabled-color);
}

.dr-section-summary-in:checked + label:before {
content: '▼';
}

.dr-section-summary-in:checked + label > span {
display: none;
}

.dr-section-summary,
.dr-section-inline-details {
padding-top: 4px;
padding-bottom: 4px;
}

.dr-section-inline-details {
grid-column: 2 / -1;
}

.dr-section-details {
display: none;
grid-column: 1 / -1;
margin-bottom: 5px;
}

.dr-section-summary-in:checked ~ .dr-section-details {
display: contents;
}

.dr-entry-list {
padding-left: 0 !important;
}

.dr-entry-item {
display: contents
}

.dr-entry-list {
display: contents
}

.dr-entry-list > li:nth-child(odd) > div,
.dr-entry-list > li:nth-child(odd) > label,
.dr-entry-list > li:nth-child(odd) > .dr-entry-name span {
background-color: var(--dr-background-color-row-odd);
}

.dr-entry-name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: 10px;
grid-column: 1;
}

.dr-entry-data {
display: none;
background-color: var(--dr-background-color) !important;
padding-bottom: 5px !important;
}

.dr-entry-data-in:checked ~ .dr-entry-data {
display: inline-block;
}

.dr-entry-data {
padding-left: 25px;
grid-column: 1 / -1;
background-color: var(--dr-background-color) !important;
}

0 comments on commit 0cf1018

Please sign in to comment.