-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Jupyter.tsx
109 lines (103 loc) 路 3.42 KB
/
Jupyter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2022-2023 Datalayer Inc. All rights reserved.
*
* MIT License
*/
import React, { useMemo } from 'react';
import { ThemeProvider, BaseStyles, Box } from "@primer/react";
import { ErrorBoundary } from 'react-error-boundary';
import { JupyterContextProvider } from './JupyterContext';
import JupyterLabCss from './lab/JupyterLabCss';
import { getJupyterServerHttpUrl, getJupyterServerWsUrl, loadJupyterConfig } from './JupyterConfig';
import defaultInjectableStore, { InjectableStore } from '../state/redux/Store';
import { JupyterLabTheme } from "./lab/JupyterLabTheme";
import {jupyterTheme} from './theme';
/**
* Definition of the properties that can be passed
* when creating a Jupyter context.
*/
export type JupyterProps = {
children?: React.ReactNode;
collaborative?: boolean;
defaultKernelName: string;
disableCssLoading?: boolean;
injectableStore?: InjectableStore;
jupyterServerHttpUrl?: string;
jupyterServerWsUrl?: string;
jupyterToken?: string;
lite?: boolean;
startDefaultKernel: boolean;
terminals?: boolean;
theme: JupyterLabTheme;
useRunningKernelId?: string;
useRunningKernelIndex?: number;
}
/**
* The component to be used as fallback in case of error.
*/
const ErrorFallback = ({error, resetErrorBoundary}: any) => {
return (
<div role="alert">
<p>Oops, something went wrong.</p>
<pre>{error.message}</pre>
<div style={{ visibility: "hidden" }}>
<button onClick={resetErrorBoundary}>Try again</button>
</div>
</div>
)
}
/**
* The Jupyter context. This handles the needed initialization
* and ensure the Redux and the Material UI theme providers
* are available.
*/
export const Jupyter = (props: JupyterProps) => {
const {
lite, collaborative, startDefaultKernel, defaultKernelName, injectableStore, theme,
useRunningKernelId, useRunningKernelIndex, children, disableCssLoading,
} = props;
const config = useMemo(() => {
return loadJupyterConfig(props);
}, [props]);
return (
<ErrorBoundary
FallbackComponent={ErrorFallback}
onReset={() => { console.log('Error Boundary reset has been invoked...'); }}
>
<ThemeProvider theme={jupyterTheme} colorMode={theme === 'light' ? 'day' : 'night'} dayScheme='light' nightScheme='dark'>
<BaseStyles>
<Box color="fg.default" bg="canvas.default">
{ !config.insideJupyterLab && !disableCssLoading && <JupyterLabCss theme={theme}/> }
<JupyterContextProvider
baseUrl={getJupyterServerHttpUrl()}
collaborative={collaborative}
defaultKernelName={defaultKernelName}
disableCssLoading={disableCssLoading}
injectableStore={injectableStore || defaultInjectableStore}
lite={lite}
startDefaultKernel={startDefaultKernel}
theme={theme}
useRunningKernelId={useRunningKernelId}
useRunningKernelIndex={useRunningKernelIndex ?? -1}
variant="default"
wsUrl={getJupyterServerWsUrl()}
>
{ children }
</JupyterContextProvider>
</Box>
</BaseStyles>
</ThemeProvider>
</ErrorBoundary>
)
}
Jupyter.defaultProps = {
collaborative: false,
defaultKernelName: 'python',
disableCssLoading: false,
lite: false,
startDefaultKernel: true,
terminals: false,
theme: 'light',
useRunningKernelIndex: -1,
}
export default Jupyter;