Skip to content

Commit

Permalink
only one output type is rendered, resolves #28
Browse files Browse the repository at this point in the history
  • Loading branch information
kokes committed Feb 10, 2019
1 parent 36c745d commit 255fc3e
Showing 1 changed file with 68 additions and 61 deletions.
129 changes: 68 additions & 61 deletions lib/nbv.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,74 +184,81 @@ var nbv = (function() {
var el = d.createElement('div');
el.style.minHeight = '1em';

// individual outputs
var fmts = Object.keys(dt.data);
for (var j=0; j < fmts.length; j++) {
var fmt = fmts[j];
var dm = d.createElement('div');
switch (fmt) {
case 'text/plain':
// text/plain might be just a fallback here
// fixed #10
if (fmts.includes('text/html')) continue;

dm = d.createElement('pre');
dm.style.margin = 0;
dm.style.whiteSpace = 'pre-wrap';
dm.textContent = typeof dt.data[fmt] === 'string' ? dt.data[fmt] : dt.data[fmt].join('');
break;
// taken from https://github.com/jupyter/nbconvert/blob/master/nbconvert/utils/base.py
// var format_priority = ['text/html', 'application/pdf', 'text/latex', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/markdown', 'text/plain'];
// filtering only those that are currently supported (excluding pdf, latex, markdown)
var format_priority = ['text/html', 'image/svg+xml', 'image/png', 'image/jpeg', 'text/plain'];

var fmt = null;
for (var tfmt of format_priority) {
if (dt.data.hasOwnProperty(tfmt)) {
fmt = tfmt;
break;
}
}
if (!fmt) {
console.error('No valid data format: ' + Object.keys(dt.data));
return d.createElement('div'); // empty div, just so that it can be appended
}

case 'text/html':
dm.innerHTML = dt.data[fmt].join('');
var dm = d.createElement('div');
switch (fmt) {
case 'text/plain':
dm = d.createElement('pre');
dm.style.margin = 0;
dm.style.whiteSpace = 'pre-wrap';
dm.textContent = typeof dt.data[fmt] === 'string' ? dt.data[fmt] : dt.data[fmt].join('');
break;

case 'text/html':
dm.innerHTML = dt.data[fmt].join('');

// we may have generated some HTML tables we need to style
var dfs = dm.getElementsByClassName('dataframe');
for (var k = 0; k < dfs.length; k++) {
if (dfs[k].classList.contains('dataframe')) {
continue; // we style dataframes separately
}
dfs[k].setAttribute('style', [
'border-collapse: collapse',
'text-align: left'
// 'margin-top: 1em'
].join(';'));

// let's style individual cells as well
var cl = dfs[k].querySelectorAll('td, th');
for (var l=0; l < cl.length; l++) {
cl[l].style.padding = '3px';
}

// we may have generated some HTML tables we need to style
var dfs = dm.getElementsByClassName('dataframe');
for (var k = 0; k < dfs.length; k++) {
if (dfs[k].classList.contains('dataframe')) {
continue; // we style dataframes separately
}
break;

case 'image/svg+xml':
dm.innerHTML = dt.data[fmt].join('');
break;

default:
if (fmt.startsWith('image/')) {
dm = d.createElement('img');
dm.setAttribute('src', 'data:' + fmt + ';base64,' +
((typeof dt.data[fmt]) == 'string' ? dt.data[fmt] : dt.data[fmt].join('')));
dm.setAttribute('style', 'max-width: 100%'); // avoid overflow
// use width and height attributes supplied in metadata
if (fmt in dt.metadata) {
var metadata = dt.metadata[fmt];
if ('width' in metadata) {
dm.setAttribute('width', metadata.width);
}
dfs[k].setAttribute('style', [
'border-collapse: collapse',
'text-align: left'
// 'margin-top: 1em'
].join(';'));

// let's style individual cells as well
var cl = dfs[k].querySelectorAll('td, th');
for (var l=0; l < cl.length; l++) {
cl[l].style.padding = '3px';
if ('height' in metadata) {
dm.setAttribute('height', metadata.height);
}

}
break;

case 'image/svg+xml':
dm.innerHTML = dt.data[fmt].join('');
break;

default:
if (fmt.startsWith('image/')) {
dm = d.createElement('img');
dm.setAttribute('src', 'data:' + fmt + ';base64,' +
((typeof dt.data[fmt]) == 'string' ? dt.data[fmt] : dt.data[fmt].join('')));
dm.setAttribute('style', 'max-width: 100%'); // avoid overflow
// use width and height attributes supplied in metadata
if (fmt in dt.metadata) {
var metadata = dt.metadata[fmt];
if ('width' in metadata) {
dm.setAttribute('width', metadata.width);
}
if ('height' in metadata) {
dm.setAttribute('height', metadata.height);
}
}
break;
}
console.error('unexpected format: ' + fmt);
}
el.appendChild(dm);

}
console.error('Valid, but unsupported format: ' + fmt);
}
el.appendChild(dm);

return el;
}
Expand Down

0 comments on commit 255fc3e

Please sign in to comment.