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

chore: change notebook dir to workspace #23

Merged
merged 3 commits into from
Feb 4, 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
4 changes: 3 additions & 1 deletion docker/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ENV CI=true
USER root
WORKDIR /repo

COPY --from=source-tree /repo/pnpm-lock.yaml /repo/pnpm-lock.yaml
COPY --from=source-tree /repo/pnpm-lock.yaml /repo/.npmrc /repo/

RUN pnpm fetch

Expand All @@ -40,6 +40,8 @@ WORKDIR /root

COPY ./docker/app/root/scripts /root/scripts

RUN mkdir workspace
tonywu6 marked this conversation as resolved.
Show resolved Hide resolved

ENV SELF_PARTY=alice
ENV ALL_PARTIES=alice

Expand Down
2 changes: 1 addition & 1 deletion docker/app/root/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ ray start \
--disable-usage-stats \
--resources="{\"$SELF_PARTY\": 4}"

secretnote --allow-root --no-browser
secretnote --allow-root --no-browser --notebook-dir=/root/workspace
2 changes: 2 additions & 0 deletions packages/secretnote/src/components/dropdown-menu/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
}

.ant-upload {
padding-left: 30px;
margin-left: -30px;
color: var(--mana-secretnote-text-color);
font-size: 12px;
}
Expand Down
147 changes: 66 additions & 81 deletions packages/secretnote/src/modules/editor/cell/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
KernelError,
ILSPDocumentConnectionManager,
CodeEditorManager,
CodeEditorSettings,
} from '@difizen/libro-jupyter';
import {
getOrigin,
Expand All @@ -25,6 +24,7 @@ import {
} from '@difizen/mana-app';
import { l10n } from '@difizen/mana-l10n';
import { message } from 'antd';
import { isUndefined } from 'lodash-es';
import { forwardRef } from 'react';

import { Ribbon } from '@/components/ribbon';
Expand All @@ -36,15 +36,15 @@ import type { SecretNoteModel } from '../model';

const SecretNoteCodeCellComponent = forwardRef<HTMLDivElement>((props, ref) => {
const instance = useInject<SecretNoteCodeCellView>(ViewInstance);
const { allExecutionParty, executionParty } = instance;
const { partyList, parties } = instance;

return (
<div className={instance.className} ref={ref} tabIndex={10} onBlur={instance.blur}>
<Ribbon
items={allExecutionParty}
value={executionParty}
items={partyList.map((name) => ({ label: name, key: name }))}
value={parties}
onChange={(val) => {
instance.changeExecutionParty(val);
instance.onPartiesChange(val);
}}
>
<CellEditorMemo />
Expand All @@ -54,6 +54,8 @@ const SecretNoteCodeCellComponent = forwardRef<HTMLDivElement>((props, ref) => {
});
SecretNoteCodeCellComponent.displayName = 'SecretNoteCodeCellComponent';

let lastParties: string[] = []; // store last parties for new cell

@transient()
@view('secretnote-code-cell-view')
export class SecretNoteCodeCellView extends JupyterCodeCellView {
Expand All @@ -63,13 +65,10 @@ export class SecretNoteCodeCellView extends JupyterCodeCellView {
view = SecretNoteCodeCellComponent;

@prop()
executionParty: string[] = [];
parties: string[] = [];

get allExecutionParty() {
return this.serverManager.servers.map((server) => ({
key: server.id,
label: server.name,
}));
get partyList() {
return this.serverManager.servers.map((server) => server.name);
}

constructor(
Expand All @@ -80,89 +79,72 @@ export class SecretNoteCodeCellView extends JupyterCodeCellView {
@inject(SecretNoteKernelManager) kernelManager: SecretNoteKernelManager,
@inject(ILSPDocumentConnectionManager) lsp: ILSPDocumentConnectionManager,
@inject(CodeEditorManager) codeEditorManager: CodeEditorManager,
@inject(CodeEditorSettings) codeEditorSettings: CodeEditorSettings,
) {
super(
options,
cellService,
viewManager,
lsp,
codeEditorManager,
codeEditorSettings,
);
super(options, cellService, viewManager, lsp, codeEditorManager);
this.serverManager = serverManager;
this.kernelManager = kernelManager;
this.executionParty =
this.getExecutionParty() || this.allExecutionParty.map((item) => item.key);
this.parties = this.getInitializedParties();
}

async run() {
getUsableConnections() {
const libroModel = this.parent.model as SecretNoteModel;
const cellModel = this.model;

if (!libroModel) {
return false;
return [];
}

let kernelConnections = getOrigin(libroModel.kernelConnections);

// 没有可用的 Kernel 连接
if (kernelConnections.length === 0) {
message.info(l10n.t('没有可用的 Kernel 连接'));
return false;
}
const kernelConnections = getOrigin(libroModel.kernelConnections);

kernelConnections = kernelConnections.filter((connection) => {
return kernelConnections.filter((connection) => {
if (connection.isDisposed) {
return false;
}
const server = this.kernelManager.getServerByKernelConnection(connection);
return (
server && server.status === 'running' && this.executionParty.includes(server.id)
server && server.status === 'running' && this.parties.includes(server.name)
);
});
if (kernelConnections.length === 0) {
message.info(l10n.t('请选择一个执行节点'));
return false;
}
if (kernelConnections.length !== this.executionParty.length) {
message.info(l10n.t('有的 Kernel 连接不可用'));
return false;
}
}

const hasDisposedConnection = kernelConnections.some((item) => {
return item.isDisposed;
});
if (hasDisposedConnection) {
message.error(l10n.t('有的 Kernel 连接已经被销毁'));
async run() {
const cellModel = this.model;
const kernelConnections = this.getUsableConnections();

if (kernelConnections.length === 0) {
message.info(l10n.t('No available node to execute'));
return false;
}

this.clearExecution();
this.setExecutionStatus({ executing: true });
this.setExecutionTime({ start: '', end: '', toExecute: new Date().toISOString() });
this.setExecutionParty();
this.updateExecutionStatus({ executing: true });
this.updateExecutionTime({
toExecute: new Date().toISOString(),
});
this.savePartiesToMeta();

try {
const list: Promise<KernelMessage.IExecuteReplyMsg>[] = [];
for (let i = 0, len = kernelConnections.length; i < len; i += 1) {
const connection = kernelConnections[i];

const future = connection.requestExecute({
code: cellModel.value,
});
const future = connection.requestExecute(
{
code: cellModel.value,
},
// Even after receiving a reply message, you can still receive other messages.
false,
);

future.onIOPub = (
msg: KernelMessage.IIOPubMessage<KernelMessage.IOPubMessageType>,
) => {
if (msg.header.msg_type === 'execute_input') {
this.updateExecutionStatus({ kernelExecuting: true });
this.updateExecutionTime({ start: msg.header.date });
}
cellModel.msgChangeEmitter.fire({
connection,
msg,
});
if (
cellModel.kernelExecuting === false &&
msg.header.msg_type === 'execute_input'
) {
this.setExecutionStatus({ kernelExecuting: true });
this.setExecutionTime({ start: msg.header.date });
}
};

future.onReply = (msg: KernelMessage.IExecuteReplyMsg) => {
Expand All @@ -176,8 +158,8 @@ export class SecretNoteCodeCellView extends JupyterCodeCellView {
}

const futureDoneList = await Promise.all(list);
this.setExecutionStatus({ executing: false, kernelExecuting: false });
this.setExecutionTime(this.parseMessageTime(futureDoneList));
this.updateExecutionStatus({ executing: false, kernelExecuting: false });
this.updateExecutionTime(this.parseMessageTime(futureDoneList));

const ok = futureDoneList.every((msg) => msg.content.status === 'ok');
if (ok) {
Expand All @@ -197,27 +179,27 @@ export class SecretNoteCodeCellView extends JupyterCodeCellView {
}
}

setExecutionStatus(status: { executing?: boolean; kernelExecuting?: boolean }) {
updateExecutionStatus(status: { executing?: boolean; kernelExecuting?: boolean }) {
const { executing, kernelExecuting } = status;
if (executing !== undefined) {
if (!isUndefined(executing)) {
this.model.executing = executing;
}
if (kernelExecuting !== undefined) {
if (!isUndefined(kernelExecuting)) {
this.model.kernelExecuting = kernelExecuting;
}
}

setExecutionTime(times: { start?: string; end?: string; toExecute?: string }) {
updateExecutionTime(times: { start?: string; end?: string; toExecute?: string }) {
const meta = this.model.metadata.execution as ExecutionMeta;
if (meta) {
const { start, end, toExecute } = times;
if (start !== undefined) {
if (!isUndefined(start)) {
meta['shell.execute_reply.started'] = start;
}
if (end !== undefined) {
if (!isUndefined(end)) {
meta['shell.execute_reply.end'] = end;
}
if (toExecute !== undefined) {
if (!isUndefined(toExecute)) {
meta.to_execute = toExecute;
}
}
Expand All @@ -240,29 +222,32 @@ export class SecretNoteCodeCellView extends JupyterCodeCellView {
return { start, end };
}

changeExecutionParty(party: string[]) {
this.executionParty = party;
this.setExecutionParty(party);
onPartiesChange(parties: string[]) {
this.parties = parties;
this.savePartiesToMeta(parties);
lastParties = parties;
}

getExecutionParty() {
getInitializedParties() {
const execution = this.model.metadata.execution as ExecutionMeta;
if (execution && execution.executionParty) {
if (execution && execution.parties) {
try {
const party: string[] = JSON.parse(execution.executionParty as string);
return party.filter((p) =>
this.allExecutionParty.some((item) => item.key === p),
);
const parties: string[] = JSON.parse(execution.parties as string);
return parties.filter((p) => this.partyList.includes(p));
} catch (e) {
return [];
}
} else if (lastParties.length > 0) {
// load parties from previous cell settings
return lastParties;
}
return this.partyList;
}

setExecutionParty(party: string[] = this.executionParty) {
savePartiesToMeta(parties: string[] = this.parties) {
const execution = this.model.metadata.execution as ExecutionMeta;
if (execution) {
execution.executionParty = JSON.stringify(party);
execution.parties = JSON.stringify(parties);
}
}
}
2 changes: 1 addition & 1 deletion packages/secretnote/src/modules/editor/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,5 @@ export class SecretNoteModel extends LibroModel {

autoSave = debounce(() => {
this.commandRegistry.executeCommand(DocumentCommands.Save.id);
}, 1000);
}, 500);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ const LogViewer = React.lazy(() => import('@/components/log-viewer'));
export class LogPreview implements FilePreviewContribution {
type = 'log';
render = (data: string) => {
return <LogViewer code={data} />;
return <LogViewer code={[data]} />;
};
}
Loading
Loading