@difizen/libro-output 0.0.2-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/es/display-data-output/display-data-output-contribution.d.ts +11 -0
- package/es/display-data-output/display-data-output-contribution.d.ts.map +1 -0
- package/es/display-data-output/display-data-output-contribution.js +40 -0
- package/es/display-data-output/display-data-output-model.d.ts +22 -0
- package/es/display-data-output/display-data-output-model.d.ts.map +1 -0
- package/es/display-data-output/display-data-output-model.js +77 -0
- package/es/display-data-output/display-data-output-module.d.ts +3 -0
- package/es/display-data-output/display-data-output-module.d.ts.map +1 -0
- package/es/display-data-output/display-data-output-module.js +6 -0
- package/es/display-data-output/index.d.ts +4 -0
- package/es/display-data-output/index.d.ts.map +1 -0
- package/es/display-data-output/index.js +3 -0
- package/es/error-output/error-output-contribution.d.ts +11 -0
- package/es/error-output/error-output-contribution.d.ts.map +1 -0
- package/es/error-output/error-output-contribution.js +40 -0
- package/es/error-output/error-output-model.d.ts +16 -0
- package/es/error-output/error-output-model.d.ts.map +1 -0
- package/es/error-output/error-output-model.js +117 -0
- package/es/error-output/error-output-module.d.ts +3 -0
- package/es/error-output/error-output-module.d.ts.map +1 -0
- package/es/error-output/error-output-module.js +6 -0
- package/es/error-output/index.d.ts +4 -0
- package/es/error-output/index.d.ts.map +1 -0
- package/es/error-output/index.js +3 -0
- package/es/index.d.ts +4 -0
- package/es/index.d.ts.map +1 -0
- package/es/index.js +3 -0
- package/es/index.less +836 -0
- package/es/output-utils.d.ts +21 -0
- package/es/output-utils.d.ts.map +1 -0
- package/es/output-utils.js +68 -0
- package/es/stream-output/index.d.ts +4 -0
- package/es/stream-output/index.d.ts.map +1 -0
- package/es/stream-output/index.js +3 -0
- package/es/stream-output/stream-output-contribution.d.ts +11 -0
- package/es/stream-output/stream-output-contribution.d.ts.map +1 -0
- package/es/stream-output/stream-output-contribution.js +40 -0
- package/es/stream-output/stream-output-model.d.ts +18 -0
- package/es/stream-output/stream-output-model.d.ts.map +1 -0
- package/es/stream-output/stream-output-model.js +70 -0
- package/es/stream-output/stream-output-module.d.ts +3 -0
- package/es/stream-output/stream-output-module.d.ts.map +1 -0
- package/es/stream-output/stream-output-module.js +6 -0
- package/package.json +62 -0
- package/src/display-data-output/display-data-output-contribution.ts +24 -0
- package/src/display-data-output/display-data-output-model.tsx +67 -0
- package/src/display-data-output/display-data-output-module.ts +10 -0
- package/src/display-data-output/index.ts +3 -0
- package/src/error-output/error-output-contribution.ts +21 -0
- package/src/error-output/error-output-model.tsx +94 -0
- package/src/error-output/error-output-module.ts +10 -0
- package/src/error-output/index.ts +3 -0
- package/src/index.less +835 -0
- package/src/index.ts +3 -0
- package/src/output-utils.ts +84 -0
- package/src/stream-output/index.ts +3 -0
- package/src/stream-output/stream-output-contribution.ts +21 -0
- package/src/stream-output/stream-output-model.tsx +60 -0
- package/src/stream-output/stream-output-module.ts +10 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IExecuteResult,
|
|
3
|
+
IMimeBundle,
|
|
4
|
+
IOutput,
|
|
5
|
+
PartialJSONObject,
|
|
6
|
+
PartialJSONValue,
|
|
7
|
+
ReadonlyPartialJSONObject,
|
|
8
|
+
} from '@difizen/libro-common';
|
|
9
|
+
import {
|
|
10
|
+
isDisplayData,
|
|
11
|
+
isDisplayUpdate,
|
|
12
|
+
isError,
|
|
13
|
+
isExecuteResult,
|
|
14
|
+
isPrimitive,
|
|
15
|
+
isStream,
|
|
16
|
+
} from '@difizen/libro-common';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Get the data from a notebook output.
|
|
20
|
+
*/
|
|
21
|
+
export function getData(output: IOutput): PartialJSONObject {
|
|
22
|
+
let bundle: IMimeBundle = {};
|
|
23
|
+
if (isExecuteResult(output) || isDisplayData(output) || isDisplayUpdate(output)) {
|
|
24
|
+
bundle = (output as IExecuteResult).data;
|
|
25
|
+
} else if (isStream(output)) {
|
|
26
|
+
if (output.name === 'stderr') {
|
|
27
|
+
bundle['application/vnd.jupyter.stderr'] = output.text;
|
|
28
|
+
} else {
|
|
29
|
+
bundle['application/vnd.jupyter.stdout'] = output.text;
|
|
30
|
+
}
|
|
31
|
+
} else if (isError(output)) {
|
|
32
|
+
bundle['application/vnd.jupyter.error'] = output;
|
|
33
|
+
const traceback = output.traceback.join('\n');
|
|
34
|
+
bundle['application/vnd.jupyter.stderr'] =
|
|
35
|
+
traceback || `${output.ename}: ${output.evalue}`;
|
|
36
|
+
}
|
|
37
|
+
return convertBundle(bundle);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get the metadata from an output message.
|
|
42
|
+
*/
|
|
43
|
+
export function getMetadata(output: IOutput): PartialJSONObject {
|
|
44
|
+
const value: PartialJSONObject = Object.create(null);
|
|
45
|
+
if (isExecuteResult(output) || isDisplayData(output)) {
|
|
46
|
+
for (const key in output.metadata) {
|
|
47
|
+
value[key] = extract(output.metadata, key);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get the bundle options given output model options.
|
|
54
|
+
*/
|
|
55
|
+
export function getBundleOptions(options: IOutput) {
|
|
56
|
+
const data = getData(options) as IOutput;
|
|
57
|
+
const metadata = getMetadata(options);
|
|
58
|
+
return { data, metadata };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Extract a value from a JSONObject.
|
|
63
|
+
*/
|
|
64
|
+
export function extract(
|
|
65
|
+
value: ReadonlyPartialJSONObject,
|
|
66
|
+
key: string,
|
|
67
|
+
): PartialJSONValue | undefined {
|
|
68
|
+
const item = value[key];
|
|
69
|
+
if (item === undefined || isPrimitive(item)) {
|
|
70
|
+
return item;
|
|
71
|
+
}
|
|
72
|
+
return JSON.parse(JSON.stringify(item));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Convert a mime bundle to mime data.
|
|
77
|
+
*/
|
|
78
|
+
function convertBundle(bundle: IMimeBundle): PartialJSONObject {
|
|
79
|
+
const map: PartialJSONObject = Object.create(null);
|
|
80
|
+
for (const mimeType in bundle) {
|
|
81
|
+
map[mimeType] = extract(bundle, mimeType);
|
|
82
|
+
}
|
|
83
|
+
return map;
|
|
84
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IOutput } from '@difizen/libro-common';
|
|
2
|
+
import type { IOutputOptions } from '@difizen/libro-core';
|
|
3
|
+
import { OutputContribution } from '@difizen/libro-core';
|
|
4
|
+
import { inject, singleton } from '@difizen/mana-app';
|
|
5
|
+
import { ViewManager } from '@difizen/mana-app';
|
|
6
|
+
|
|
7
|
+
import { StreamOutputModel } from './stream-output-model.js';
|
|
8
|
+
|
|
9
|
+
@singleton({ contrib: OutputContribution })
|
|
10
|
+
export class StreamOutputContribution implements OutputContribution {
|
|
11
|
+
@inject(ViewManager) viewManager: ViewManager;
|
|
12
|
+
canHandle = (output: IOutput) => {
|
|
13
|
+
if (output.output_type === 'stream') {
|
|
14
|
+
return 100;
|
|
15
|
+
}
|
|
16
|
+
return 1;
|
|
17
|
+
};
|
|
18
|
+
factory(output: IOutputOptions) {
|
|
19
|
+
return this.viewManager.getOrCreateView(StreamOutputModel, output);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { JSONObject } from '@difizen/libro-common';
|
|
2
|
+
import { LibroOutputView } from '@difizen/libro-core';
|
|
3
|
+
import type { BaseOutputView, IOutputOptions } from '@difizen/libro-core';
|
|
4
|
+
import { RenderMimeRegistry } from '@difizen/libro-rendermime';
|
|
5
|
+
import type { IRenderMimeRegistry } from '@difizen/libro-rendermime';
|
|
6
|
+
import { inject, transient } from '@difizen/mana-app';
|
|
7
|
+
import {
|
|
8
|
+
getOrigin,
|
|
9
|
+
useInject,
|
|
10
|
+
view,
|
|
11
|
+
ViewInstance,
|
|
12
|
+
ViewOption,
|
|
13
|
+
} from '@difizen/mana-app';
|
|
14
|
+
import { forwardRef } from 'react';
|
|
15
|
+
|
|
16
|
+
import '../index.less';
|
|
17
|
+
|
|
18
|
+
import { getBundleOptions } from '../output-utils.js';
|
|
19
|
+
|
|
20
|
+
const StreamOutputModelRender = forwardRef<HTMLDivElement>(
|
|
21
|
+
function StreamOutputModelRender(_props, ref) {
|
|
22
|
+
const output = useInject<StreamOutputModel>(ViewInstance);
|
|
23
|
+
const model = getOrigin(output);
|
|
24
|
+
const defaultRenderMime = useInject<IRenderMimeRegistry>(RenderMimeRegistry);
|
|
25
|
+
const defaultRenderMimeType = defaultRenderMime.preferredMimeType(model);
|
|
26
|
+
if (defaultRenderMimeType) {
|
|
27
|
+
const OutputRender = defaultRenderMime.createRenderer(
|
|
28
|
+
defaultRenderMimeType,
|
|
29
|
+
model,
|
|
30
|
+
);
|
|
31
|
+
const children = <OutputRender model={model} />;
|
|
32
|
+
return (
|
|
33
|
+
<div ref={ref} className={'libro-stream-container'}>
|
|
34
|
+
{children}
|
|
35
|
+
</div>
|
|
36
|
+
);
|
|
37
|
+
} else {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
);
|
|
42
|
+
@transient()
|
|
43
|
+
@view('libro-stream-output-model')
|
|
44
|
+
export class StreamOutputModel extends LibroOutputView implements BaseOutputView {
|
|
45
|
+
constructor(@inject(ViewOption) options: IOutputOptions) {
|
|
46
|
+
super(options);
|
|
47
|
+
const { data, metadata } = getBundleOptions(options.output);
|
|
48
|
+
this.type = options.output.output_type;
|
|
49
|
+
this.data = data as JSONObject;
|
|
50
|
+
this.metadata = metadata;
|
|
51
|
+
}
|
|
52
|
+
override view = StreamOutputModelRender;
|
|
53
|
+
override toJSON() {
|
|
54
|
+
return {
|
|
55
|
+
output_type: this.raw.output_type,
|
|
56
|
+
name: this.raw.name,
|
|
57
|
+
text: this.raw.text,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { OutputModule } from '@difizen/libro-core';
|
|
2
|
+
import { LibroRenderMimeModule } from '@difizen/libro-rendermime';
|
|
3
|
+
import { ManaModule } from '@difizen/mana-app';
|
|
4
|
+
|
|
5
|
+
import { StreamOutputContribution } from './stream-output-contribution.js';
|
|
6
|
+
import { StreamOutputModel } from './stream-output-model.js';
|
|
7
|
+
|
|
8
|
+
export const StreamOutputModule = ManaModule.create()
|
|
9
|
+
.register(StreamOutputModel, StreamOutputContribution)
|
|
10
|
+
.dependOn(OutputModule, LibroRenderMimeModule);
|