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

Notebook extensions #325

Merged
merged 12 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@datalayer/jupyter-react",
"version": "0.18.9",
"version": "0.18.11",
"description": "Jupyter React - React.js components 100% compatible with Jupyter.",
"license": "MIT",
"main": "lib/index.js",
Expand Down
68 changes: 33 additions & 35 deletions packages/react/src/app/JupyterReact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import { useState, useEffect } from 'react';
import { JupyterFrontEnd } from '@jupyterlab/application';
import { ThemeProvider, BaseStyles, Box } from '@primer/react';
import { Box } from '@primer/react';
import { UnderlineNav } from '@primer/react';
import { ReactJsIcon, RingedPlanetIcon } from '@datalayer/icons-react';
import { ServerConnection } from '@jupyterlab/services';
import { JupyterReactTheme } from '../theme';
import { requestAPI } from '../jupyter/JupyterHandlers';
import AboutTab from './tabs/AboutTab';
import ComponentsTab from './tabs/ComponentsTab';
import { requestAPI } from '../jupyter/JupyterHandlers';

export type JupyterFrontEndProps = {
app?: JupyterFrontEnd;
Expand All @@ -35,39 +36,36 @@ const JupyterReact = (props: JupyterFrontEndProps): JSX.Element => {
});
return (
<>
<ThemeProvider>
<BaseStyles>
<Box style={{ maxWidth: 700 }}>
<Box mb={3}>
<UnderlineNav aria-label="jupyter-react">
<UnderlineNav.Item
aria-current="page"
icon={RingedPlanetIcon}
onSelect={e => {
e.preventDefault();
setTab(1);
}}
>
Components
</UnderlineNav.Item>
<UnderlineNav.Item
icon={ReactJsIcon}
onSelect={e => {
e.preventDefault();
setTab(2);
}}
>
About
</UnderlineNav.Item>
</UnderlineNav>
</Box>
<Box m={3}>
{tab === 1 && <ComponentsTab app={app} />}
{tab === 2 && <AboutTab version={version} />}
</Box>
</Box>
</BaseStyles>
</ThemeProvider>
<JupyterReactTheme loadJupyterLabCss={false}>
<Box mb={3}>
<UnderlineNav aria-label="jupyter-react">
<UnderlineNav.Item
aria-current={tab === 1 ? "page" : undefined}
icon={RingedPlanetIcon}
onSelect={e => {
e.preventDefault();
setTab(1);
}}
>
Components
</UnderlineNav.Item>
<UnderlineNav.Item
aria-current={tab === 2 ? "page" : undefined}
icon={ReactJsIcon}
onSelect={e => {
e.preventDefault();
setTab(2);
}}
>
About
</UnderlineNav.Item>
</UnderlineNav>
</Box>
<Box m={3}>
{tab === 1 && <ComponentsTab app={app} />}
{tab === 2 && <AboutTab version={version} />}
</Box>
</JupyterReactTheme>
</>
);
};
Expand Down
24 changes: 12 additions & 12 deletions packages/react/src/app/tabs/AboutTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,12 @@ const AboutTab = (props: Props): JSX.Element => {
<Box>
<Text>React.js components 💯% compatible with 🪐 Jupyter.</Text>
</Box>
<Box mt={3} style={{ width: 350 }}>
{!egg ? (
<img
src="https://assets.datalayer.tech/releases/datalayer-0.3.0-black-snake_1024.png"
onClick={e => setEgg(true)}
/>
) : (
<ECharlesIcon size={300} onClick={e => setEgg(false)} />
)}
</Box>
<Box>
<Link
href="https://datalayer.tech/docs/releases/0.3.0-black-snake"
href="https://datalayer.tech/docs/releases/1.2.0-black-snake"
target="_blank"
>
<Text as="h4">Datalayer 0.3.0 Black Snake Release</Text>
<Text as="h4">Datalayer 1.2.0 Black Snake Release</Text>
</Link>
</Box>
<Box>
Expand All @@ -49,6 +39,16 @@ const AboutTab = (props: Props): JSX.Element => {
<Text as="h4">Source code</Text>
</Link>
</Box>
<Box mt={3}>
{!egg ? (
<img
src="https://assets.datalayer.tech/releases/datalayer-1.2.0-black-snake_1024.png"
onClick={e => setEgg(true)}
/>
) : (
<ECharlesIcon size={300} onClick={e => setEgg(false)} />
)}
</Box>
</>
);
};
Expand Down
13 changes: 6 additions & 7 deletions packages/react/src/app/tabs/ComponentsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import { useState } from 'react';
import { Box, NavList } from '@primer/react';
import { Jupyter } from '../../jupyter/Jupyter';
import { JupyterFrontEndProps } from '../JupyterReact';
import FileBrowserComponent from './components/FileBrowserComponent';
import CellComponent from './components/CellComponent';
import NotebookComponent from './components/NotebookComponent';
// import CellComponent from './components/CellComponent';
// import NotebookComponent from './components/NotebookComponent';
import IPyWidgetsComponent from './components/IPyWidgetsComponent';
import ViewerComponent from './components/ViewerComponent';

Expand Down Expand Up @@ -60,13 +59,13 @@ const MainTab = (props: JupyterFrontEndProps) => {
</NavList>
</Box>
<Box ml={3} sx={{ width: '100%' }}>
<Jupyter startDefaultKernel>
{nav === 1 && <FileBrowserComponent />}
{nav === 2 && <CellComponent />}
{nav === 3 && <NotebookComponent />}
{nav === 4 && <IPyWidgetsComponent />}
{nav === 5 && <ViewerComponent />}
</Jupyter>
{/*
{nav === 2 && <CellComponent />}
{nav === 3 && <NotebookComponent />}
*/}
</Box>
</Box>
</>
Expand Down
8 changes: 3 additions & 5 deletions packages/react/src/app/tabs/components/NotebookComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@
* MIT License
*/

// import Notebook from '../../../components/notebook/Notebook';
// import CellSidebarNew from './../../../components/notebook/cell/sidebar/CellSidebarButton';
import Notebook from '../../../components/notebook/Notebook';
import CellSidebarNew from './../../../components/notebook/cell/sidebar/CellSidebarButton';

// import nbformat from './../../..//examples/notebooks/IPyWidgetsExample.ipynb.json';
import nbformat from './../../..//examples/notebooks/IPyWidgetsExample.ipynb.json';

const NotebookComponent = () => {
return (
<>
{/*
<Notebook
nbformat={nbformat}
id="notebook-id"
cellSidebarMargin={60}
height="calc(100vh - 2.6rem)" // (Height - Toolbar Height).
CellSidebar={CellSidebarNew}
/>
*/}
</>
);
};
Expand Down
13 changes: 2 additions & 11 deletions packages/react/src/components/cell/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import { useState, useEffect } from 'react';
import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
import { Box } from '@primer/react';
import Lumino from '../lumino/Lumino';
import { useJupyter } from './../../jupyter/JupyterContext';
import { newUuid } from '../../utils';
import { Lumino } from '../lumino';
import CellAdapter from './CellAdapter';
import useCellsStore from './CellState';
import { newUuid } from '../../utils';

export type ICellProps = {
/**
Expand Down Expand Up @@ -44,42 +44,35 @@ export const Cell = (props: ICellProps) => {
type='code',
} = props;
const { defaultKernel, serverSettings } = useJupyter();

const [id] = useState(props.id || newUuid());
const [adapter, setAdapter] = useState<CellAdapter>();

const cellsStore = useCellsStore();

const handleCellInitEvents = (adapter: CellAdapter) => {
adapter.cell.model.contentChanged.connect(
(cellModel, changedArgs) => {
cellsStore.setSource(id, cellModel.sharedModel.getSource());
}
);

if (adapter.cell instanceof CodeCell) {
adapter.cell.outputArea.outputLengthChanged?.connect(
(outputArea, outputsCount) => {
cellsStore.setOutputsCount(id, outputsCount);
}
);
}

adapter.sessionContext.initialize().then(() => {
if (autoStart && adapter.cell.model) {
// Perform auto-start for code or markdown cells.
adapter.execute();
}
});

adapter.sessionContext.kernelChanged.connect(() => {
void adapter.sessionContext.session?.kernel?.info.then(info => {
// Set that session/kernel is ready for this cell when the kernel is guaranteed to be connected
cellsStore.setKernelSessionAvailable(id, true);
})
});
}

useEffect(() => {
if (id && defaultKernel && serverSettings) {
defaultKernel.ready.then(() => {
Expand All @@ -95,7 +88,6 @@ export const Cell = (props: ICellProps) => {
cellsStore.setSource(id, source);
handleCellInitEvents(adapter);
setAdapter(adapter);

const handleDblClick = (event: Event) => {
let target = event.target as HTMLElement;
/**
Expand All @@ -109,7 +101,6 @@ export const Cell = (props: ICellProps) => {
(adapter.cell as MarkdownCell).rendered = false;
}
};

// Adds the event for double click and the removal on component's destroy
document.addEventListener('dblclick', handleDblClick);
return () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/cell/CellAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
RenderMimeRegistry,
standardRendererFactories as initialFactories,
} from '@jupyterlab/rendermime';
import { execute as executeOutput } from './../output/OutputExecutor';
import {
Session,
ServerConnection,
Expand All @@ -50,6 +49,7 @@ import {
WIDGET_MIMETYPE,
WidgetRenderer,
} from '@jupyter-widgets/html-manager/lib/output_renderers';
import { execute as executeOutput } from './../output/OutputExecutor';
import { requireLoader as loader } from '../../jupyter/ipywidgets/libembed-amd';
import ClassicWidgetManager from '../../jupyter/ipywidgets/classic/manager';
import Kernel from '../../jupyter/kernel/Kernel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import { useState, useRef, useEffect } from 'react';
import { basicSetup } from 'codemirror';
import { EditorState } from '@codemirror/state';
// import { Compartment } from '@codemirror/state';
import { Compartment } from '@codemirror/state';
import { keymap, EditorView, ViewUpdate } from '@codemirror/view';
// import { python } from '@codemirror/lang-python';
import { python } from '@codemirror/lang-python';
import Kernel from '../../jupyter/kernel/Kernel';
import codeMirrorTheme from './CodeMirrorTheme';
import OutputAdapter from '../output/OutputAdapter';
import CodeMirrorOutputToolbar from './CodeMirrorOutputToolbar';
import useOutputsStore from '../output/OutputState';

export const CodeMirrorEditor = (props: {
export const CodeMirrorDatalayerEditor = (props: {
code: string;
codePre?: string;
outputAdapter: OutputAdapter;
Expand Down Expand Up @@ -80,7 +80,7 @@ export const CodeMirrorEditor = (props: {
};
useEffect(() => {
outputStore.setInput(sourceId, code);
// const language = new Compartment();
const language = new Compartment();
const keyBinding = [
{
key: 'Shift-Enter',
Expand All @@ -92,7 +92,7 @@ export const CodeMirrorEditor = (props: {
doc: code,
extensions: [
basicSetup,
// language.of(python()), // TODO CodeMirrorEditor imported by Output breaks the JupyterLab extension loading. https://github.com/datalayer/jupyter-ui/issues/170
language.of(python()),
EditorView.lineWrapping,
keymap.of([...keyBinding]),
codeMirrorTheme,
Expand Down Expand Up @@ -147,4 +147,4 @@ export const CodeMirrorEditor = (props: {
);
};

export default CodeMirrorEditor;
export default CodeMirrorDatalayerEditor;
2 changes: 1 addition & 1 deletion packages/react/src/components/codemirror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* MIT License
*/

export * from './CodeMirrorEditor';
export * from './CodeMirrorDatalayerEditor';
export * from './CodeMirrorOutputToolbar';
export * from './CodeMirrorTheme';
Loading