Skip to content

Commit

Permalink
feat(Outputs): Add support for Vega/Vega-Lite outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Aug 25, 2021
1 parent a939675 commit c69d24f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Most development tasks can be run directly from `npm` or via `make` wrapper reci
| Run tests with coverage | `npm run cover` | `make cover` |
| Build documentation | `npm run docs` | `make docs` |

Note that the some of the tests require `python3` and the Python packages `matplotlib` and `Pillow` to be installed.

## 🙏 Acknowledgments

Many thanks to the nteract community for [`kernelspecs`](https://github.com/nteract/kernelspecs) and
Expand Down
22 changes: 21 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ import sys; sys.stderr.write('An error!')
expect(chunk.errors).toEqual([
schema.codeError({ errorMessage: 'An error!' }),
])
expect(chunk.outputs).toEqual([])
expect(chunk.outputs).toEqual([
9, // The write() returns the number of bytes written
])
})

test('errors', async () => {
Expand Down Expand Up @@ -269,6 +271,24 @@ describe('unbundle', () => {
}
})

test('vega', () => {
const image = jupita.unbundle({
'application/vnd.vegalite.v4+json': {},
})
expect(schema.isA('ImageObject', image)).toBe(true)
if (schema.isA('ImageObject', image)) {
expect(image.content).toEqual(
expect.arrayContaining([
expect.objectContaining({
mediaType: 'application/vnd.vegalite.v4+json',
spec: {},
}),
])
)
expect(image.contentUrl).toMatch(`https://via.placeholder.com`)
}
})

test.each([['image/png'], ['image/jpeg'], ['image/gif']])(
'%s',
(mediaType) => {
Expand Down
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,27 @@ export class Jupita extends Listener {
})
}

for (const mediaType of [
'application/vnd.vega.v1+json',
'application/vnd.vega.v2+json',
'application/vnd.vega.v3+json',
'application/vnd.vega.v4+json',
'application/vnd.vega.v5+json',
'application/vnd.vegalite.v1+json',
'application/vnd.vegalite.v2+json',
'application/vnd.vegalite.v3+json',
'application/vnd.vegalite.v4+json',
]) {
const vega = bundle[mediaType]
if (vega !== undefined) {
return schema.imageObject({
content: [{ mediaType, spec: vega }],
contentUrl:
'https://via.placeholder.com/400x60?text=Unable%20to%20render%20Vega%20output',
})
}
}

for (const mediaType of ['image/png', 'image/jpeg', 'image/gif']) {
const image = bundle[mediaType]
if (image !== undefined) {
Expand Down

0 comments on commit c69d24f

Please sign in to comment.