Skip to content

Commit

Permalink
Auto format arquero tables into HTML (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored Sep 6, 2021
1 parent 0bd5908 commit 6cb9d33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/extension/kernel/problems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { IDisposable } from '../types';
import { disposeAllDisposables } from '../utils';
import { parse as parseStack } from 'error-stack-parser';
import type ErrorStackParser from 'error-stack-parser';
import { Compiler } from './compiler';

const diagnosticsCollection = languages.createDiagnosticCollection('Typscript Notebook');
Expand All @@ -35,7 +36,14 @@ export class CellDiagnosticsProvider {
if (!ex || !ex?.stack) {
return;
}
const stacks = parseStack(ex as Error);
let stacks: ErrorStackParser.StackFrame[] = [];
try {
stacks = parseStack(ex as Error);
} catch (ex) {
// TODO: Seems to fail in windows.
console.error('Failed to parse Stack trace', ex);
return;
}
if (stacks.length === 0) {
return;
}
Expand Down
11 changes: 11 additions & 0 deletions src/extension/server/extensions/arqueroFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
export class ArqueroFormatter {
private static arqueroTable?: any;
public static isArqueroTable(table: any) {
try {
const proto = table.__proto__ || table.prototype;
return proto && ArqueroFormatter.arqueroTable === proto;
} catch {
//
}
return false;
}
public static initialize(arquero: typeof import('arquero')) {
var table = arquero.table({ colA: ['a', 'b', 'c'], colB: [3, 4, 5] });
const proto = (table as any).__proto__ || (table as any).protptype;
if (!proto) {
return;
}
ArqueroFormatter.arqueroTable = proto;
proto.print = function (opts) {
const { display } = require('node-kernel');
display.html(this.toHTML(opts));
Expand Down
7 changes: 7 additions & 0 deletions src/extension/server/extensions/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { logMessage } from '../logger';
import { DisplayData } from '../types';
import { DanfoJsFormatter } from './danfoFormatter';
import { formatTensor, isTensor } from './tensorFormatter';
import { ArqueroFormatter } from './arqueroFormatter';

export function isBase64OrSvg(value: string) {
return value.startsWith('data:image/') || (value.endsWith('</svg>') && value.includes('<svg'));
Expand Down Expand Up @@ -37,6 +38,12 @@ export async function formatImage(
export async function formatValue(value: unknown, requestId: string): Promise<DisplayData | undefined> {
if (typeof value === undefined) {
return;
} else if (ArqueroFormatter.isArqueroTable(value)) {
return {
type: 'html',
requestId,
value: (value as any).toHTML()
};
} else if (typeof value === 'string' && value.startsWith('data:image/')) {
return {
type: 'multi-mime',
Expand Down

0 comments on commit 6cb9d33

Please sign in to comment.