Skip to content

Commit

Permalink
fix(jupyter): fix the condition when execution btn show tip
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk committed Aug 15, 2024
1 parent e6ad472 commit e0b6cf1
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 135 deletions.
33 changes: 33 additions & 0 deletions .changeset/good-waves-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
'@difizen/libro-jupyter': patch
'@difizen/libro-docs': patch
'@difizen/libro-code-cell': patch
'@difizen/libro-code-editor': patch
'@difizen/libro-codemirror': patch
'@difizen/libro-cofine-editor': patch
'@difizen/libro-cofine-editor-contribution': patch
'@difizen/libro-cofine-editor-core': patch
'@difizen/libro-cofine-textmate': patch
'@difizen/libro-common': patch
'@difizen/libro-core': patch
'@difizen/libro-kernel': patch
'@difizen/libro-l10n': patch
'@difizen/libro-lab': patch
'@difizen/libro-language-client': patch
'@difizen/libro-lsp': patch
'@difizen/libro-markdown': patch
'@difizen/libro-markdown-cell': patch
'@difizen/libro-output': patch
'@difizen/libro-prompt-cell': patch
'@difizen/libro-raw-cell': patch
'@difizen/libro-rendermime': patch
'@difizen/libro-search': patch
'@difizen/libro-search-code-cell': patch
'@difizen/libro-shared-model': patch
'@difizen/libro-terminal': patch
'@difizen/libro-toc': patch
'@difizen/libro-virtualized': patch
'@difizen/libro-widget': patch
---

fix(jupyter): fix the condition when execution btn show tip
94 changes: 94 additions & 0 deletions packages/libro-jupyter/src/libro-jupyter-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { blue, gold, green, red } from '@ant-design/colors';
import type {
ICellMetadata,
ICodeCellMetadata,
Expand All @@ -7,6 +8,7 @@ import type {
import { ExecutableCellModel } from '@difizen/libro-core';
import type { IContentsModel } from '@difizen/libro-kernel';
import type { Event as ManaEvent, Emitter } from '@difizen/mana-app';
import { l10n } from '@difizen/mana-l10n';

export interface ExecutionMeta extends PartialJSONObject {
'shell.execute_reply.started': string; // Kernel 开始执行任务时间在 metadata 中的 key
Expand Down Expand Up @@ -59,3 +61,95 @@ export interface ServerLaunchManager {
}

export const libroArgsMimetype = 'application/vnd.libro.args+json';

export interface ServerStatus {
category: string;
color: string;
text: string;
text_zh: string;
}

export const statusToColor = {
canRunImmediate: green[5],
canRun: blue[5],
blocking: gold[5],
error: red[4],
};

export const jupyterServiceStatus: Record<string, ServerStatus> = {
loading: {
category: 'JupyterService',
color: statusToColor.blocking,
text: 'loading',
text_zh: l10n.t('加载中'),
},
failed: {
category: 'JupyterService',
color: statusToColor.error,
text: 'failed',
text_zh: l10n.t('加载失败'),
},
loaded: {
category: 'JupyterService',
color: statusToColor.canRunImmediate,
text: 'loaded',
text_zh: l10n.t('加载完成'),
},
};

export const kernelStatus: Record<string, ServerStatus> = {
connecting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'connecting',
text_zh: l10n.t('正在连接'),
},
unknown: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'unknown',
text_zh: l10n.t('未知'),
},
starting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'starting',
text_zh: l10n.t('启动中'),
},
idle: {
category: 'Kernel',
color: statusToColor.canRunImmediate,
text: 'idle',
text_zh: l10n.t('空闲'),
},
busy: {
category: 'Kernel',
color: statusToColor.canRun,
text: 'busy',
text_zh: l10n.t('忙碌'),
},
terminating: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'terminating',
text_zh: l10n.t('终止中'),
},
restarting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'restarting',
text_zh: l10n.t('重启中'),
},
autorestarting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'autorestarting',
text_zh: l10n.t('自动重启中'),
},
dead: {
category: 'Kernel',
color: statusToColor.error,
text: 'dead',
text_zh: l10n.t('死亡'),
},
};
128 changes: 4 additions & 124 deletions packages/libro-jupyter/src/toolbar/kernel-status-and-selector.tsx
Original file line number Diff line number Diff line change
@@ -1,137 +1,17 @@
import { red, green, gold, blue } from '@ant-design/colors';
import { LoadingOutlined, StopOutlined } from '@ant-design/icons';
import type { LibroView } from '@difizen/libro-core';
import { ServerManager } from '@difizen/libro-kernel';
import { useInject, ViewInstance } from '@difizen/mana-app';
import { l10n } from '@difizen/mana-l10n';
import { Badge } from 'antd';

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

import { KernelSelector } from './kernel-selector-dropdown.js';
import type { LibroJupyterModel } from '../libro-jupyter-model.js';

import './index.less';
import { kernelStatus } from '../libro-jupyter-protocol.js';
import { getServiceStatusInfo } from '../utils/index.js';

export interface ServerStatus {
category: string;
color: string;
text: string;
text_zh: string;
}

export const statusToColor = {
canRunImmediate: green[5],
canRun: blue[5],
blocking: gold[5],
error: red[4],
};

export const jupyterServiceStatus: Record<string, ServerStatus> = {
loading: {
category: 'JupyterService',
color: statusToColor.blocking,
text: 'loading',
text_zh: l10n.t('加载中'),
},
failed: {
category: 'JupyterService',
color: statusToColor.error,
text: 'failed',
text_zh: l10n.t('加载失败'),
},
loaded: {
category: 'JupyterService',
color: statusToColor.canRunImmediate,
text: 'loaded',
text_zh: l10n.t('加载完成'),
},
};

export const kernelStatus: Record<string, ServerStatus> = {
connecting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'connecting',
text_zh: l10n.t('正在连接'),
},
unknown: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'unknown',
text_zh: l10n.t('未知'),
},
starting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'starting',
text_zh: l10n.t('启动中'),
},
idle: {
category: 'Kernel',
color: statusToColor.canRunImmediate,
text: 'idle',
text_zh: l10n.t('空闲'),
},
busy: {
category: 'Kernel',
color: statusToColor.canRun,
text: 'busy',
text_zh: l10n.t('忙碌'),
},
terminating: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'terminating',
text_zh: l10n.t('终止中'),
},
restarting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'restarting',
text_zh: l10n.t('重启中'),
},
autorestarting: {
category: 'Kernel',
color: statusToColor.blocking,
text: 'autorestarting',
text_zh: l10n.t('自动重启中'),
},
dead: {
category: 'Kernel',
color: statusToColor.error,
text: 'dead',
text_zh: l10n.t('死亡'),
},
};

const getServiceStatusInfo = (
serverManager: ServerManager | undefined,
libroModel: LibroJupyterModel | undefined,
): ServerStatus => {
if (!serverManager || serverManager.launching) {
return jupyterServiceStatus['loading'];
}

if (
!libroModel ||
!(libroModel instanceof LibroJupyterModel) ||
libroModel.kernelConnecting === true ||
libroModel.kernelConnecting === undefined
) {
return kernelStatus['connecting'];
}

if (!libroModel.kernelConnection) {
return {
color: statusToColor.blocking,
text: 'no kernel',
category: 'Kernel',
text_zh: l10n.t('无内核'),
};
}

return kernelStatus[libroModel.kernelConnection.status];
};
import { KernelSelector } from './kernel-selector-dropdown.js';

export const KernelStatusSelector: React.FC = () => {
const libroView = useInject<LibroView>(ViewInstance);
Expand Down
13 changes: 8 additions & 5 deletions packages/libro-jupyter/src/toolbar/run-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ import { Menu, Dropdown, Tooltip } from 'antd';
import type { MenuProps } from 'antd';
import { useEffect, useState } from 'react';

import { LibroJupyterConfiguration } from '../index.js';
import { LibroJupyterConfiguration, ServerManager } from '../index.js';
import type { LibroJupyterModel } from '../libro-jupyter-model.js';
import { kernelPrepared } from '../utils/index.js';

export const RunSelector: React.FC = () => {
const libroView = useInject<LibroView>(ViewInstance);
const libroModel = libroView ? libroView.model : undefined;
const toolbar = useInject<Toolbar>(ToolbarInstance);
const libroServerManager = useInject(ServerManager);
const data = toolbar.currentArgs as LibroToolbarArags;
const command = useInject(CommandRegistry);
const curCell = data?.[0];
const configService = useInject<ConfigurationService>(ConfigurationService);
const isRunVisible =
ExecutableCellModel.is(curCell?.model) && !curCell?.model.executing ? true : false;
const isKernelIdle = libroModel
? (libroModel as LibroJupyterModel).isKernelIdle
: false;
const isKernelPrepared = kernelPrepared(
libroServerManager,
libroModel as LibroJupyterModel,
);

const [kernelUnreadyBtnText, setKernelUnreadyBtnText] =
useState<string>('kernel准备中,无法执行');
Expand Down Expand Up @@ -109,7 +112,7 @@ export const RunSelector: React.FC = () => {
/>
);

if (isKernelIdle) {
if (isKernelPrepared) {
return (
<Dropdown overlay={menu} placement="bottomLeft">
<PlayCircleOutlined />
Expand Down
12 changes: 8 additions & 4 deletions packages/libro-jupyter/src/toolbar/side-toolbar-run-selector.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { PlayCircleOutlined } from '@ant-design/icons';
import type { LibroSideToolbarMenuItemType, LibroView } from '@difizen/libro-core';
import { NotebookCommands, LibroSideToolbarMenu } from '@difizen/libro-core';
import { ServerManager } from '@difizen/libro-kernel';
import { ConfigurationService, useInject, ViewInstance } from '@difizen/mana-app';
import { l10n } from '@difizen/mana-l10n';
import { Popover, Tooltip } from 'antd';
import { useEffect, useState } from 'react';

import { LibroJupyterConfiguration } from '../index.js';
import type { LibroJupyterModel } from '../libro-jupyter-model.js';
import { kernelPrepared } from '../utils/index.js';

const items: LibroSideToolbarMenuItemType[] = [
{
Expand Down Expand Up @@ -49,11 +51,13 @@ const items: LibroSideToolbarMenuItemType[] = [

export const SideToolbarRunSelector: React.FC = () => {
const libroView = useInject<LibroView>(ViewInstance);
const libroServerManager = useInject(ServerManager);
const configService = useInject<ConfigurationService>(ConfigurationService);
const libroModel = libroView ? libroView.model : undefined;
const isKernelIdle = libroModel
? (libroModel as LibroJupyterModel).isKernelIdle
: false;
const isKernelPrepared = kernelPrepared(
libroServerManager,
libroModel as LibroJupyterModel,
);

const [kernelUnreadyBtnText, setKernelUnreadyBtnText] =
useState<string>('kernel准备中,无法执行');
Expand All @@ -70,7 +74,7 @@ export const SideToolbarRunSelector: React.FC = () => {
});
});

if (isKernelIdle) {
if (isKernelPrepared) {
return (
<Popover
placement="leftTop"
Expand Down
Loading

0 comments on commit e0b6cf1

Please sign in to comment.