Skip to content

Commit 6cb9d33

Browse files
authored
Auto format arquero tables into HTML (#27)
1 parent 0bd5908 commit 6cb9d33

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/extension/kernel/problems.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { IDisposable } from '../types';
1111
import { disposeAllDisposables } from '../utils';
1212
import { parse as parseStack } from 'error-stack-parser';
13+
import type ErrorStackParser from 'error-stack-parser';
1314
import { Compiler } from './compiler';
1415

1516
const diagnosticsCollection = languages.createDiagnosticCollection('Typscript Notebook');
@@ -35,7 +36,14 @@ export class CellDiagnosticsProvider {
3536
if (!ex || !ex?.stack) {
3637
return;
3738
}
38-
const stacks = parseStack(ex as Error);
39+
let stacks: ErrorStackParser.StackFrame[] = [];
40+
try {
41+
stacks = parseStack(ex as Error);
42+
} catch (ex) {
43+
// TODO: Seems to fail in windows.
44+
console.error('Failed to parse Stack trace', ex);
45+
return;
46+
}
3947
if (stacks.length === 0) {
4048
return;
4149
}

src/extension/server/extensions/arqueroFormatter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
export class ArqueroFormatter {
2+
private static arqueroTable?: any;
3+
public static isArqueroTable(table: any) {
4+
try {
5+
const proto = table.__proto__ || table.prototype;
6+
return proto && ArqueroFormatter.arqueroTable === proto;
7+
} catch {
8+
//
9+
}
10+
return false;
11+
}
212
public static initialize(arquero: typeof import('arquero')) {
313
var table = arquero.table({ colA: ['a', 'b', 'c'], colB: [3, 4, 5] });
414
const proto = (table as any).__proto__ || (table as any).protptype;
515
if (!proto) {
616
return;
717
}
18+
ArqueroFormatter.arqueroTable = proto;
819
proto.print = function (opts) {
920
const { display } = require('node-kernel');
1021
display.html(this.toHTML(opts));

src/extension/server/extensions/format.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { logMessage } from '../logger';
66
import { DisplayData } from '../types';
77
import { DanfoJsFormatter } from './danfoFormatter';
88
import { formatTensor, isTensor } from './tensorFormatter';
9+
import { ArqueroFormatter } from './arqueroFormatter';
910

1011
export function isBase64OrSvg(value: string) {
1112
return value.startsWith('data:image/') || (value.endsWith('</svg>') && value.includes('<svg'));
@@ -37,6 +38,12 @@ export async function formatImage(
3738
export async function formatValue(value: unknown, requestId: string): Promise<DisplayData | undefined> {
3839
if (typeof value === undefined) {
3940
return;
41+
} else if (ArqueroFormatter.isArqueroTable(value)) {
42+
return {
43+
type: 'html',
44+
requestId,
45+
value: (value as any).toHTML()
46+
};
4047
} else if (typeof value === 'string' && value.startsWith('data:image/')) {
4148
return {
4249
type: 'multi-mime',

0 commit comments

Comments
 (0)