-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🪓 hack module loading * ⏱ no need to pre-load * 🪝converting to hooks
- Loading branch information
1 parent
614a416
commit a04a70d
Showing
7 changed files
with
1,816 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
declare module 'jupyterlab-plotly/lib/plotly-renderer.js'; | ||
declare module 'jupyterlab-plotly/dist/index.js'; | ||
declare module 'plotly.js/dist/plotly'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect, useState } from 'react'; | ||
import type { IRenderMime, IRenderMimeRegistry } from '@jupyterlab/rendermime'; | ||
import type { IOutput } from '@jupyterlab/nbformat'; | ||
|
||
export function useLoadPlotly() { | ||
const [plotly, setPlotly] = useState< | ||
{ rendererFactory: IRenderMime.IRendererFactory } | undefined | ||
>(); | ||
|
||
useEffect(() => { | ||
if (plotly) return; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import('jupyterlab-plotly/lib/plotly-renderer.js').then( | ||
(module: { rendererFactory: IRenderMime.IRendererFactory }) => { | ||
console.debug('Jupyter: Adding plotly renderer factory to rendermime registry', { | ||
module, | ||
}); | ||
setPlotly(module); | ||
}, | ||
); | ||
}, [plotly]); | ||
|
||
return { plotly }; | ||
} | ||
|
||
const PLOTLY_MIMETYPE = 'application/vnd.plotly.v1+json'; | ||
|
||
export function isPlotly(outputs: IOutput[]) { | ||
return outputs.some((output) => Object.keys(output.data ?? []).includes(PLOTLY_MIMETYPE)); | ||
} | ||
|
||
export function usePlotlyPassively(rendermime: IRenderMimeRegistry, outputs: IOutput[]) { | ||
const [loaded, setLoaded] = useState(false); | ||
|
||
const isPlotlyOutput = isPlotly(outputs); | ||
|
||
useEffect(() => { | ||
if (loaded || !isPlotlyOutput) return; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import('jupyterlab-plotly/lib/plotly-renderer.js').then( | ||
(module: { rendererFactory: IRenderMime.IRendererFactory }) => { | ||
console.debug('Jupyter: Adding plotly renderer factory to rendermime registry', { | ||
module, | ||
}); | ||
rendermime.addFactory(module.rendererFactory, 41); | ||
setLoaded(true); | ||
}, | ||
); | ||
}, [loaded, isPlotlyOutput]); | ||
|
||
return { loaded }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
diff --git a/node_modules/jupyterlab-plotly/lib/plotly-renderer.js b/node_modules/jupyterlab-plotly/lib/plotly-renderer.js | ||
index 23baa49..43c857a 100644 | ||
--- a/node_modules/jupyterlab-plotly/lib/plotly-renderer.js | ||
+++ b/node_modules/jupyterlab-plotly/lib/plotly-renderer.js | ||
@@ -110,12 +110,15 @@ export class RenderedPlotly extends Widget { | ||
const loadPlotly = () => __awaiter(this, void 0, void 0, function* () { | ||
if (RenderedPlotly.Plotly === null) { | ||
RenderedPlotly.Plotly = yield import("plotly.js/dist/plotly"); | ||
+ RenderedPlotly.Plotly = RenderedPlotly.Plotly.default; | ||
RenderedPlotly._resolveLoadingPlotly(); | ||
} | ||
return RenderedPlotly.loadingPlotly; | ||
}); | ||
return loadPlotly() | ||
- .then(() => RenderedPlotly.Plotly.react(this.node, data, layout, config)) | ||
+ .then(() => { | ||
+ return RenderedPlotly.Plotly.react(this.node, data, layout, config) | ||
+ }) | ||
.then((plot) => { | ||
this.showGraph(); | ||
this.hideImage(); |