Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update Matplotlib plugin to register widget view #189

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
663 changes: 648 additions & 15 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions plugins/matplotlib/src/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"react": "^17.0.2"
},
"dependencies": {
"@deephaven/components": "^0.40.0",
"@deephaven/dashboard": "^0.40.0",
"@deephaven/jsapi-bootstrap": "^0.40.0",
"@deephaven/jsapi-types": "^0.40.0",
"@deephaven/log": "^0.40.0",
"@deephaven/components": "^0.58.0",
"@deephaven/dashboard": "^0.58.0",
"@deephaven/icons": "^0.58.0",
"@deephaven/jsapi-bootstrap": "^0.58.0",
"@deephaven/jsapi-types": "^0.58.0",
"@deephaven/log": "^0.58.0",
"@deephaven/plugin": "^0.58.0",
"shortid": "^2.2.16"
},
"publishConfig": {
Expand Down
81 changes: 0 additions & 81 deletions plugins/matplotlib/src/js/src/DashboardPlugin.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions plugins/matplotlib/src/js/src/MatPlotLibPanel.scss

This file was deleted.

13 changes: 13 additions & 0 deletions plugins/matplotlib/src/js/src/MatplotlibPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type WidgetPlugin, PluginType } from '@deephaven/plugin';
import { vsGraph } from '@deephaven/icons';
import { MatplotlibView } from './MatplotlibView';

export const MatplotlibPlugin: WidgetPlugin = {
name: '@deephaven/js-plugin-matplotlib',
type: PluginType.WIDGET_PLUGIN,
supportedTypes: 'matplotlib.figure.Figure',
component: MatplotlibView,
icon: vsGraph,
};

export default MatplotlibPlugin;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useEffect, useState } from 'react';
import React, { CSSProperties, useEffect, useState } from 'react';
import { useApi } from '@deephaven/jsapi-bootstrap';
import type { Table } from '@deephaven/jsapi-types';
import type { Table, ViewportData } from '@deephaven/jsapi-types';
import Log from '@deephaven/log';
import { PanelProps } from '@deephaven/dashboard';
import { WidgetComponentProps } from '@deephaven/plugin';

const log = Log.module('@deephaven/js-plugin-matplotlib.MatPlotLibPanel');
const log = Log.module('@deephaven/js-plugin-matplotlib.MatplotlibView');

enum InputColumn {
key = 'key',
Expand All @@ -15,28 +15,22 @@ enum InputKey {
revision = 'revision',
}

export type MatPlotLibExportedObject = {
fetch: () => unknown;
export const MatplotlibViewStyle: CSSProperties = {
height: '100%',
width: '100%',
display: 'contents',
};

export type MatPlotLibWidget = {
type: string;
getDataAsBase64: () => string;
exportedObjects: MatPlotLibExportedObject[];
};

export type MatPlotLibPanelProps = {
fetch: () => Promise<MatPlotLibWidget>;
} & PanelProps;

export type MatPlotLibPanelState = {
imageData?: string;
export const MatplotlibViewImageStyle: CSSProperties = {
height: '100%',
width: '100%',
objectFit: 'contain',
};

/**
* Displays a rendered matplotlib from the server
*/
export function MatPlotLibPanel(props: MatPlotLibPanelProps): React.ReactNode {
export function MatplotlibView(props: WidgetComponentProps): JSX.Element {
const { fetch } = props;
const [imageSrc, setImageSrc] = useState<string>();
const [inputTable, setInputTable] = useState<Table>();
Expand All @@ -59,11 +53,14 @@ export function MatPlotLibPanel(props: MatPlotLibPanelProps): React.ReactNode {
table.applyFilter([
keyColumn.filter().eq(dh.FilterValue.ofString(InputKey.revision)),
]);
table.addEventListener(dh.Table.EVENT_UPDATED, ({ detail: data }) => {
const newRevision = data.rows[0].get(valueColumn);
log.debug('New revision', newRevision);
setRevision(newRevision);
});
table.addEventListener(
dh.Table.EVENT_UPDATED,
({ detail: data }: CustomEvent<ViewportData>) => {
const newRevision = data.rows[0].get(valueColumn);
log.debug('New revision', newRevision);
setRevision(newRevision);
}
);
table.setViewport(0, 0, [valueColumn]);
}
openTable();
Expand Down Expand Up @@ -96,12 +93,16 @@ export function MatPlotLibPanel(props: MatPlotLibPanelProps): React.ReactNode {
);

return (
<div className="mat-plot-lib-panel">
{imageSrc !== undefined && <img src={imageSrc} alt="MatPlotLib render" />}
<div className="matplotlib-view" style={MatplotlibViewStyle}>
{imageSrc !== undefined && (
<img
src={imageSrc}
alt="Matplotlib render"
style={MatplotlibViewImageStyle}
/>
)}
</div>
);
}

MatPlotLibPanel.COMPONENT = 'MatPlotLibPanel';

export default MatPlotLibPanel;
export default MatplotlibView;
5 changes: 3 additions & 2 deletions plugins/matplotlib/src/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './MatPlotLibPanel';
export * from './DashboardPlugin';
import { MatplotlibPlugin } from './MatplotlibPlugin';

export default MatplotlibPlugin;
11 changes: 10 additions & 1 deletion plugins/matplotlib/src/js/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ export default defineConfig(({ mode }) => ({
formats: ['cjs'],
},
rollupOptions: {
external: ['react', 'react-dom', '@deephaven/jsapi-bootstrap'],
external: [
'react',
'react-dom',
'@deephaven/components',
'@deephaven/dashboard',
'@deephaven/icons',
'@deephaven/jsapi-bootstrap',
'@deephaven/log',
'@deephaven/plugin',
],
},
},
define:
Expand Down