Skip to content

Commit 2dbf96b

Browse files
committed
🧹 tidy up, upgrade demo
1 parent 9718c47 commit 2dbf96b

File tree

7 files changed

+48
-26
lines changed

7 files changed

+48
-26
lines changed

apps/demo-react/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@types/node": "^16.18.14",
1111
"@types/react": "^18.0.28",
1212
"@types/react-dom": "^18.0.11",
13+
"classnames": "^2.5.1",
1314
"localforage": "^1.10.0",
1415
"match-sorter": "^6.3.1",
1516
"react": "^18.2.0",

apps/demo-react/src/NotebookPage.tsx

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
import { ThebeSessionProvider, ThebeRenderMimeRegistryProvider, useThebeServer } from 'thebe-react';
1+
import {
2+
ThebeSessionProvider,
3+
ThebeRenderMimeRegistryProvider,
4+
useThebeServer,
5+
useThebeLoader,
6+
} from 'thebe-react';
27
import { ConnectionStatusTray } from './ConnectionStatusTray';
38
import { ConnectionErrorTray } from './ConnectionErrorTray';
49
import { NotebookStatusTray } from './NotebookStatusTray';
510
import { NotebookErrorTray } from './NotebookErrorTray';
611
import { AdminPanel } from './AdminPanel';
712

813
export function NotebookPage({ children }: React.PropsWithChildren) {
9-
const { connecting, ready, config, error } = useThebeServer();
14+
const { core } = useThebeLoader();
15+
const { connecting, config, ready, error } = useThebeServer();
16+
17+
if (!core) return null;
1018

11-
if (!connecting && !ready && !error) return null;
1219
return (
1320
<ThebeRenderMimeRegistryProvider>
1421
<ThebeSessionProvider start path={config?.kernels.path}>
1522
<>
16-
<ConnectionStatusTray />
17-
<ConnectionErrorTray />
18-
<NotebookStatusTray />
19-
<NotebookErrorTray />
20-
<AdminPanel />
23+
{(connecting || ready || error) && (
24+
<>
25+
<ConnectionStatusTray />
26+
<ConnectionErrorTray />
27+
<NotebookStatusTray />
28+
<NotebookErrorTray />
29+
<AdminPanel />
30+
</>
31+
)}
2132
{children}
2233
</>
2334
</ThebeSessionProvider>

apps/demo-react/src/WidgetsPage.tsx

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { useNotebook } from 'thebe-react';
22
import JupyterOutputDecoration from './JupyterOutputDecoration';
33
import { useParams } from 'react-router-dom';
4+
import classNames from 'classnames';
45

56
export function WidgetsPage() {
67
const { notebookName } = useParams<{ notebookName: string }>();
7-
const { ready, executing, executeAll, cellRefs, cellIds } = useNotebook(
8+
const { ready, executing, executeAll, cellRefs, cellIds, errors } = useNotebook(
89
notebookName ?? 'widget-test',
910
async (n) => {
1011
const url = `/${n}.ipynb`;
@@ -27,12 +28,22 @@ export function WidgetsPage() {
2728
<h4 className="text-sm">
2829
notebook: <code>{notebookName}.ipynb</code>
2930
</h4>
30-
<div className="inline-block px-4 py-2 mt-3 text-sm font-bold text-white bg-green-500 rounded-full">
31+
<div
32+
className={classNames(
33+
'inline-block px-4 py-2 mt-3 text-sm font-bold text-white rounded-full',
34+
{ 'bg-gray-500': !ready, 'bg-green-500': ready },
35+
)}
36+
>
3137
{ready ? 'ready' : 'not ready'}
3238
</div>
3339
<div className="mt-4">
3440
{!executing && (
35-
<button className="button" onClick={clickExecute}>
41+
<button
42+
className={classNames('button', {
43+
'text-gray-400 bg-gray-100 border-gray-300 cursor-not-allowed': !ready || errors,
44+
})}
45+
onClick={clickExecute}
46+
>
3647
run all
3748
</button>
3849
)}

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/src/passive.ts

-13
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ import { makeMathjaxOptions } from './options';
77
import { Widget } from '@lumino/widgets';
88
import { MessageLoop } from '@lumino/messaging';
99

10-
function assert(condition: any, msg?: string): asserts condition {
11-
if (!condition) {
12-
throw new Error(msg);
13-
}
14-
}
15-
1610
class PassiveCellRenderer implements IPassiveCell {
1711
readonly id: string;
1812
readonly rendermime: IRenderMimeRegistry;
@@ -29,8 +23,6 @@ class PassiveCellRenderer implements IPassiveCell {
2923
) {
3024
this.id = id;
3125
this.rendermime = rendermime ?? makeRenderMimeRegistry(mathjax ?? makeMathjaxOptions());
32-
assert(this.rendermime, 'no rendermime');
33-
console.log('rendermime', this.rendermime);
3426
this.model = new OutputAreaModel({ trusted: true });
3527
this.area = new OutputArea({
3628
model: this.model,
@@ -133,11 +125,6 @@ class PassiveCellRenderer implements IPassiveCell {
133125
*/
134126
render(outputs: nbformat.IOutput[]) {
135127
this.model.fromJSON(outputs);
136-
this.hydrate();
137-
}
138-
139-
hydrate() {
140-
console.log('maybe hydrate', this);
141128
}
142129
}
143130

packages/core/src/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export function placeholder(plainText?: string) {
3232
`;
3333
}
3434

35+
export function assert(condition: any, msg?: string): asserts condition {
36+
if (!condition) {
37+
throw new Error(msg);
38+
}
39+
}
40+
3541
export function stripWidgets(
3642
outputs: nbformat.IOutput[],
3743
hideWidgets = true,

packages/react/src/ThebeRenderMimeRegistryProvider.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { useThebeLoader } from './ThebeLoaderProvider';
44
import { useThebeConfig } from './ThebeServerProvider';
55

66
const RenderMimeRegistryContext = React.createContext<
7-
{ rendermime: IRenderMimeRegistry } | undefined
7+
{ rendermime: IRenderMimeRegistry | undefined } | undefined
88
>(undefined);
99

1010
/* future: could allow for renderer configuration here */
1111
export function ThebeRenderMimeRegistryProvider({ children }: React.PropsWithChildren) {
1212
const { core } = useThebeLoader();
1313
const { config } = useThebeConfig();
1414
const rendermime = React.useMemo(
15-
() => core?.makeRenderMimeRegistry(config?.mathjax) as IRenderMimeRegistry,
15+
() => core?.makeRenderMimeRegistry(config?.mathjax),
1616
[core, config],
1717
);
1818

0 commit comments

Comments
 (0)