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

libro-jupyter: integrate jupyter-widgets by default #112

Merged
merged 2 commits into from
Apr 17, 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
33 changes: 33 additions & 0 deletions .changeset/tiny-cobras-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
"@difizen/libro-cofine-editor-contribution": patch
"@difizen/libro-cofine-editor-core": patch
"@difizen/libro-search-code-cell": patch
"@difizen/libro-cofine-textmate": patch
"@difizen/libro-language-client": patch
"@difizen/libro-cofine-editor": patch
"@difizen/libro-markdown-cell": patch
"@difizen/libro-shared-model": patch
"@difizen/libro-code-editor": patch
"@difizen/libro-prompt-cell": patch
"@difizen/libro-virtualized": patch
"@difizen/libro-codemirror": patch
"@difizen/libro-rendermime": patch
"@difizen/libro-code-cell": patch
"@difizen/libro-markdown": patch
"@difizen/libro-raw-cell": patch
"@difizen/libro-terminal": patch
"@difizen/libro-jupyter": patch
"@difizen/libro-common": patch
"@difizen/libro-kernel": patch
"@difizen/libro-output": patch
"@difizen/libro-search": patch
"@difizen/libro-widget": patch
"@difizen/libro-core": patch
"@difizen/libro-l10n": patch
"@difizen/libro-lab": patch
"@difizen/libro-lsp": patch
"@difizen/libro-toc": patch
"@difizen/libro-docs": patch
---

Jupyter: integrate jupyter-widgets
6 changes: 3 additions & 3 deletions packages/libro-code-cell/src/code-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
LibroExecutableCellView,
LibroOutputArea,
VirtualizedManagerHelper,
LirboContextKey,
LibroContextKey,
} from '@difizen/libro-core';
import type { ViewSize } from '@difizen/mana-app';
import { Disposable } from '@difizen/mana-app';
Expand Down Expand Up @@ -103,7 +103,7 @@ const CodeEditorViewComponent = forwardRef<HTMLDivElement>(
@view('code-editor-cell-view')
export class LibroCodeCellView extends LibroExecutableCellView {
protected toDisposeOnEditor = new DisposableCollection();
@inject(LirboContextKey) protected readonly lirboContextKey: LirboContextKey;
@inject(LibroContextKey) protected readonly libroContextKey: LibroContextKey;
override view = CodeEditorViewComponent;

viewManager: ViewManager;
Expand Down Expand Up @@ -292,7 +292,7 @@ export class LibroCodeCellView extends LibroExecutableCellView {
this.editorView.editorStatus === 'ready' &&
this.parent.model.active?.id === this.id &&
!this.parent.model.commandMode &&
this.lirboContextKey.commandModeEnabled === true && // 排除弹窗等情况
this.libroContextKey.commandModeEnabled === true && // 排除弹窗等情况
this.parent.model.readOnly === false
) {
this.editorView?.editor.setOption('styleActiveLine', true);
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-core/src/command/libro-command-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { v4 } from 'uuid';

import { LibroCellView, ExecutableCellModel, EditorCellView } from '../cell/index.js';
import type { LibroEditorCellView } from '../cell/index.js';
import { LirboContextKey } from '../libro-context-key.js';
import { LibroContextKey } from '../libro-context-key.js';
import type { CellView, NotebookView } from '../libro-protocol.js';
import { LibroToolbarArea } from '../libro-protocol.js';
import { LibroService } from '../libro-service.js';
Expand All @@ -29,7 +29,7 @@ export class LibroCommandContribution implements CommandContribution {
@inject(ModalService) protected readonly modalService: ModalService;
@inject(LibroCommandRegister) protected readonly libroCommand: LibroCommandRegister;
@inject(LibroService) protected readonly libroService: LibroService;
@inject(LirboContextKey) protected readonly lirboContextKey: LirboContextKey;
@inject(LibroContextKey) protected readonly libroContextKey: LibroContextKey;

registerCommands(command: CommandRegistry): void {
this.libroCommand.registerLibroCommand(command, NotebookCommands['EnterEditMode'], {
Expand Down
2 changes: 1 addition & 1 deletion packages/libro-core/src/libro-context-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { LibroContextKeys } from './libro-protocol.js';
import { LibroService } from './libro-service.js';

@singleton()
export class LirboContextKey {
export class LibroContextKey {
protected readonly contextKeyService: ContextKeyService;
protected readonly libroService: LibroService;
protected toDisposeOnActiveChanged?: Disposable;
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-core/src/libro-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import {
DndList,
} from './components/index.js';
import { LibroViewHeader } from './components/libro-view-header.js';
import { LirboContextKey } from './libro-context-key.js';
import { LibroContextKey } from './libro-context-key.js';
import { LibroModel } from './libro-model.js';
import { NotebookService, notebookViewFactoryId } from './libro-protocol.js';
import type {
Expand Down Expand Up @@ -295,7 +295,7 @@ export class LibroView extends BaseView implements NotebookView {
@inject(CellService) cellService: CellService;
@inject(LibroService) libroService: LibroService;
@inject(LibroSlotManager) libroSlotManager: LibroSlotManager;
@inject(LirboContextKey) contextKey: LirboContextKey;
@inject(LibroContextKey) contextKey: LibroContextKey;

@inject(ViewManager) protected viewManager: ViewManager;
@inject(ConfigurationService) protected configurationService: ConfigurationService;
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-core/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './components/index.js';
import { LibroContentModule } from './content/index.js';
import { LibroFormatterModule } from './formatter/index.js';
import { LirboContextKey } from './libro-context-key.js';
import { LibroContextKey } from './libro-context-key.js';
import { LibroModel } from './libro-model.js';
import {
ModelFactory,
Expand All @@ -46,7 +46,7 @@ export const LibroModule = ManaModule.create()
BaseWorkspaceService,
VirtualizedManager,
LibroService,
LirboContextKey,
LibroContextKey,
LibroModel,
LibroView,
LibroSettingContribution,
Expand Down
1 change: 1 addition & 0 deletions packages/libro-jupyter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@difizen/libro-markdown-cell": "^0.1.30",
"@difizen/libro-raw-cell": "^0.1.30",
"@difizen/libro-language-client": "^0.1.30",
"@difizen/libro-widget": "^0.1.30",
"@difizen/mana-app": "latest",
"@difizen/mana-l10n": "latest",
"@ant-design/colors": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LirboContextKey } from '@difizen/libro-core';
import { LibroContextKey } from '@difizen/libro-core';
import { inject, singleton } from '@difizen/mana-app';
import { getOrigin, prop, useInject } from '@difizen/mana-app';
import type { ModalItem, ModalItemProps } from '@difizen/mana-app';
Expand Down Expand Up @@ -49,7 +49,7 @@ const getSearchResult = (

@singleton()
export class KeybindInstrutionsService implements Disposable {
@inject(LirboContextKey) contextKey: LirboContextKey;
@inject(LibroContextKey) contextKey: LibroContextKey;
isKeybindInstructionsMask = false;
@prop() searchCommandModeData: DataType[] = [];
@prop() searchEditModeData: DataType[] = [];
Expand Down
4 changes: 4 additions & 0 deletions packages/libro-jupyter/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { RawCellModule } from '@difizen/libro-raw-cell';
import { LibroSearchModule } from '@difizen/libro-search';
import { SearchCodeCellModule } from '@difizen/libro-search-code-cell';
import { WidgetModule } from '@difizen/libro-widget';
import { ManaModule } from '@difizen/mana-app';

import { LibroBetweenCellModule } from './add-between-cell/index.js';
Expand Down Expand Up @@ -56,6 +57,7 @@ import {
LibroJupyterToolbarContribution,
SaveFileErrorContribution,
} from './toolbar/index.js';
import { LibroWidgetMimeContribution } from './widget/index.js';

export const LibroJupyterModule = ManaModule.create()
.register(
Expand All @@ -74,6 +76,7 @@ export const LibroJupyterModule = ManaModule.create()
LibroJupyterSettingContribution,
JupyterServerLaunchManager,
LibroJupyterView,
LibroWidgetMimeContribution,
{
token: CellExecutionTimeProvider,
useValue: CellExecutionTip,
Expand Down Expand Up @@ -114,4 +117,5 @@ export const LibroJupyterModule = ManaModule.create()
PlotlyModule,
LibroJupyterFileModule,
LibroLanguageClientModule,
WidgetModule,
);
7 changes: 7 additions & 0 deletions packages/libro-jupyter/src/widget/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.libro-widget-render {
margin: 10px 24px;
}

.libro-widget-render:empty {
margin: unset;
}
2 changes: 2 additions & 0 deletions packages/libro-jupyter/src/widget/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './widget-render.js';
export * from './widget-rendermime-contribution.js';
47 changes: 47 additions & 0 deletions packages/libro-jupyter/src/widget/widget-render.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { BaseOutputView } from '@difizen/libro-core';
import { RenderMimeRegistry } from '@difizen/libro-rendermime';
import type { IRenderMimeRegistry } from '@difizen/libro-rendermime';
import { LibroWidgetManager } from '@difizen/libro-widget';
import { getOrigin, useInject, ViewRender } from '@difizen/mana-app';
import React from 'react';
import './index.less';

import { LibroJupyterModel } from '../libro-jupyter-model.js';

export const WidgetRender: React.FC<{ model: BaseOutputView }> = (props: {
model: BaseOutputView;
}) => {
const { model } = props;

const widgetManager = useInject(LibroWidgetManager);
const defaultRenderMime = useInject<IRenderMimeRegistry>(RenderMimeRegistry);
const libro = model.cell.parent;
if (!(libro.model instanceof LibroJupyterModel) || !libro.model.kernelConnection) {
return null;
}
const widgets = widgetManager.getOrCreateWidgets(
getOrigin(libro.model.kernelConnection),
);
const mimeType = defaultRenderMime.preferredMimeType(model);
if (mimeType) {
const model_id = JSON.parse(JSON.stringify(model.data[mimeType])).model_id;
if (model_id) {
const widgetView = widgets.getModel(model_id);
if (widgetView.isCommClosed) {
return null;
}
return (
<div className="libro-widget-render-container">
<div className="libro-widget-render">
<ViewRender view={widgetView} />
</div>
</div>
);
}
}
return (
<div className="libro-widget-render-container">
<div className="libro-widget-render" />
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { BaseOutputView } from '@difizen/libro-core';
import { RenderMimeContribution } from '@difizen/libro-rendermime';
import { LibroWidgetManager } from '@difizen/libro-widget';
import { inject, singleton } from '@difizen/mana-app';

import { LibroJupyterModel } from '../libro-jupyter-model.js';

import { WidgetRender } from './widget-render.js';

@singleton({ contrib: RenderMimeContribution })
export class LibroWidgetMimeContribution implements RenderMimeContribution {
@inject(LibroWidgetManager) libroWidgetManager: LibroWidgetManager;
canHandle = (model: BaseOutputView) => {
const libroModel = model.cell.parent.model;
let rank = 0;
if (libroModel instanceof LibroJupyterModel && libroModel.kernelConnection) {
const kc = libroModel.kernelConnection;
const widget = this.libroWidgetManager.getOrCreateWidgets(kc);
this.mimeTypes.forEach((mimeType) => {
const mimeData = model.data[mimeType];
if (mimeData && mimeData !== null) {
const data = JSON.parse(JSON.stringify(mimeData)).model_id;
if (Object.keys(model.data).includes(mimeType) && widget.hasModel(data)) {
rank = 100;
}
}
});
}
return rank;
};
renderType = 'widgetRenderer';
safe = true;
mimeTypes = ['application/vnd.jupyter.widget-view+json'];
render = WidgetRender;
}
2 changes: 1 addition & 1 deletion packages/libro-kernel/src/kernel/kernel-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ export class KernelConnection implements IKernelConnection {
// Close the comm asynchronously. We cannot block message processing on
// kernel messages to wait for another kernel message.
comm.close();
console.error('Exception opening new comm');
console.error('Exception opening new comm', e);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EditFilled } from '@ant-design/icons';
import { LirboContextKey } from '@difizen/libro-core';
import { LibroContextKey } from '@difizen/libro-core';
import { useInject } from '@difizen/mana-app';
import { Select, Tag } from 'antd';
import classNames from 'classnames';
Expand All @@ -25,7 +25,7 @@ export const ChatRecordInput: FC<ChatRecordInputProps> = (
const selectRef = useRef<BaseSelectRef>(null);
const [selecting, setSelecting] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const contextKey = useInject(LirboContextKey);
const contextKey = useInject(LibroContextKey);

useEffect(() => {
if (selecting) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EditFilled } from '@ant-design/icons';
import { LirboContextKey } from '@difizen/libro-core';
import { LibroContextKey } from '@difizen/libro-core';
import { useInject } from '@difizen/mana-app';
import { l10n } from '@difizen/mana-l10n';
import { Input, Popover } from 'antd';
Expand Down Expand Up @@ -80,7 +80,7 @@ export const VariableNameInput: FC<VariableNameInputProps> = (
) => {
const { value } = props;
const [popoverVisible, setPopoverVisible] = useState(false);
const contextKey = useInject(LirboContextKey);
const contextKey = useInject(LibroContextKey);
const ref = useRef<HTMLDivElement>(null);
return (
<div className={classNames(variableNameInputCls, props.classname)} ref={ref}>
Expand Down
6 changes: 3 additions & 3 deletions packages/libro-prompt-cell/src/prompt-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
LibroExecutableCellView,
LibroViewTracker,
EditorStatus,
LirboContextKey,
LibroContextKey,
} from '@difizen/libro-core';
import type { ExecutionMeta, KernelMessage } from '@difizen/libro-jupyter';
import { KernelError, LibroJupyterModel } from '@difizen/libro-jupyter';
Expand Down Expand Up @@ -214,7 +214,7 @@ const PropmtEditorViewComponent = React.forwardRef<HTMLDivElement>(
@transient()
@view('prompt-editor-cell-view')
export class LibroPromptCellView extends LibroExecutableCellView {
@inject(LirboContextKey) protected lirboContextKey: LirboContextKey;
@inject(LibroContextKey) protected libroContextKey: LibroContextKey;
override view = PropmtEditorViewComponent;

declare model: LibroPromptCellModel;
Expand Down Expand Up @@ -410,7 +410,7 @@ export class LibroPromptCellView extends LibroExecutableCellView {
this.editorView.editorStatus === 'ready' &&
this.parent.model.active?.id === this.id &&
!this.parent.model.commandMode &&
this.lirboContextKey.commandModeEnabled === true && // 排除弹窗等情况
this.libroContextKey.commandModeEnabled === true && // 排除弹窗等情况
this.parent.model.readOnly === false
) {
this.editorView?.editor.setOption('styleActiveLine', true);
Expand Down
6 changes: 3 additions & 3 deletions packages/libro-raw-cell/src/raw-cell-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { CodeEditorViewOptions, CodeEditorView } from '@difizen/libro-code-editor';
import { CodeEditorManager } from '@difizen/libro-code-editor';
import type { CellViewOptions } from '@difizen/libro-core';
import { CellService, LibroEditorCellView, LirboContextKey } from '@difizen/libro-core';
import { CellService, LibroEditorCellView, LibroContextKey } from '@difizen/libro-core';
import { getOrigin, prop, useInject, watch } from '@difizen/mana-app';
import {
view,
Expand Down Expand Up @@ -43,7 +43,7 @@ const CodeEditorViewComponent = React.forwardRef<HTMLDivElement>(
@transient()
@view('raw-cell-view')
export class LibroRawCellView extends LibroEditorCellView {
@inject(LirboContextKey) protected readonly lirboContextKey: LirboContextKey;
@inject(LibroContextKey) protected readonly libroContextKey: LibroContextKey;
declare model: LibroRawCellModel;
override view = CodeEditorViewComponent;

Expand Down Expand Up @@ -126,7 +126,7 @@ export class LibroRawCellView extends LibroEditorCellView {
this.editorView.editorStatus === 'ready' &&
this.parent.model.active?.id === this.id &&
!this.parent.model.commandMode &&
this.lirboContextKey.commandModeEnabled === true && // 排除弹窗等情况
this.libroContextKey.commandModeEnabled === true && // 排除弹窗等情况
this.parent.model.readOnly === false
) {
this.editorView?.editor.setOption('styleActiveLine', true);
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-search/src/libro-search-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
EditorCellView,
LibroView,
VirtualizedManagerHelper,
LirboContextKey,
LibroContextKey,
} from '@difizen/libro-core';
import { inject, prop, transient, equals } from '@difizen/mana-app';
import { Deferred, DisposableCollection } from '@difizen/mana-app';
Expand Down Expand Up @@ -40,7 +40,7 @@ export const LibroSearchProviderFactory = Symbol('LibroSearchProviderFactory');
*/
@transient()
export class LibroSearchProvider extends AbstractSearchProvider {
@inject(LirboContextKey) contextKey: LirboContextKey;
@inject(LibroContextKey) contextKey: LibroContextKey;
@inject(LibroCellSearchProvider) libroCellSearchProvider: LibroCellSearchProvider;
protected cellsChangeDeferred: Deferred<void> | undefined;

Expand Down
Loading
Loading