From 06be42d7729b59cbf130936ceea6b40398bb6225 Mon Sep 17 00:00:00 2001 From: MFA-X-AI Date: Wed, 2 Oct 2024 23:25:56 +0900 Subject: [PATCH 1/2] enable compile from filebrowser --- src/commands/CommandIDs.tsx | 1 + src/index.tsx | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/commands/CommandIDs.tsx b/src/commands/CommandIDs.tsx index 474883b1..76064c16 100644 --- a/src/commands/CommandIDs.tsx +++ b/src/commands/CommandIDs.tsx @@ -30,6 +30,7 @@ export const commandIDs = { connectLinkToObviousPorts: "Xircuit-editor:connect-obvious-link", addCommentNode: "Xircuit-editor:add-comment-node", compileFile: "Xircuit-editor:compile-file", + compileWorkflowFromFileBrowser: "Xircuit-editor:compile-workflow-from-file-browser", nextNode: "Xircuit-editor:next-node", outputMsg: "Xircuit-log:logOutputMessage", executeToOutputPanel: "Xircuit-output-panel:execute", diff --git a/src/index.tsx b/src/index.tsx index b701551b..d0a4bdd6 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -343,6 +343,53 @@ const xircuits: JupyterFrontEndPlugin = { }, }); + // Add a command for compiling a xircuits file from the file browser context menu. + app.commands.addCommand(commandIDs.compileWorkflowFromFileBrowser, { + label: 'Compile Xircuits', + icon: xircuitsIcon, + isVisible: () => { + // Ensure that the command only shows for xircuits files + return [...browserFactory.tracker.currentWidget.selectedItems()] + .filter(item => item.type === 'file' && item.path.endsWith('.xircuits')) + .length > 0; + }, + execute: async () => { + const selectedItems = Array.from(browserFactory.tracker.currentWidget.selectedItems()); + + // Iterate through selected items and compile each one + for (const xircuitsFile of selectedItems) { + if (xircuitsFile.path.endsWith('.xircuits')) { + try { + // Retrieve the required compile parameters + const python_paths = {}; // Adjust this based on any additional needs + const request = await requestToGenerateCompileFile(xircuitsFile.path, python_paths); + + if (request["message"] == "completed") { + const modelPath = xircuitsFile.path.split(".xircuits")[0] + ".py"; + if (modelPath.startsWith("xai_components/")) { + console.info(`File ${modelPath} changed. Reloading components...`); + await app.commands.execute(commandIDs.refreshComponentList); + } + alert(`${modelPath} successfully compiled!`); + } else { + console.log(request["message"]); + alert("Failed to generate compiled code. Please check console logs for more details."); + } + } catch (err) { + console.error(`Error compiling Xircuits file: ${xircuitsFile.path}`, err); + alert(`Error compiling file: ${xircuitsFile.path}. Please check the console logs for more information.`); + } + } + } + } + }); + + // Add the compile command to the context menu of the file browser + app.contextMenu.addItem({ + command: commandIDs.compileWorkflowFromFileBrowser, + selector: '.jp-DirListing-item[data-file-type="xircuits"]', + }); + app.commands.addCommand(commandIDs.copyXircuitsToRoot, { label: 'Copy To Root Directory', isVisible: () => [...browserFactory.tracker.currentWidget.selectedItems()].length > 0, From d80407ddcefbd28156ef36a8971015cffe19ea33 Mon Sep 17 00:00:00 2001 From: MFA-X-AI Date: Thu, 3 Oct 2024 01:22:48 +0900 Subject: [PATCH 2/2] refactor compile, push xircuits context menu options to top --- src/index.tsx | 90 +++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index d0a4bdd6..4cce1340 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -248,35 +248,44 @@ const xircuits: JupyterFrontEndPlugin = { } } - app.commands.addCommand(commandIDs.compileFile, { - execute: async args => { - const path = tracker.currentWidget.context.path; - const showOutput = typeof args['showOutput'] === undefined ? false : (args['showOutput'] as boolean); - - const python_paths = {}; - (args['componentList'] === undefined ? [] : args['componentList'] as []).filter(it => it['python_path']).forEach(it => { - python_paths[it['name']] = it['python_path'] - }); - - const request = await requestToGenerateCompileFile(path, python_paths); - + async function compileXircuitsFile(path: string, pythonPaths: any = {}, showOutput: boolean = false) { + try { + const request = await requestToGenerateCompileFile(path, pythonPaths); if (request["message"] == "completed") { - const model_path = path.split(".xircuits")[0] + ".py"; - docmanager.closeFile(model_path); - + const modelPath = path.split(".xircuits")[0] + ".py"; + docmanager.closeFile(modelPath); + if (showOutput) { - alert(`${model_path} successfully compiled!`); + alert(`${modelPath} successfully compiled!`); } - if(model_path.startsWith("xai_components/")){ - console.info(`File ${model_path} changed. Reloading components...`); - await app.commands.execute(commandIDs.refreshComponentList); + if (modelPath.startsWith("xai_components/")) { + console.info(`File ${modelPath} changed. Reloading components...`); + await app.commands.execute(commandIDs.refreshComponentList); } } else { - console.log(request["message"]) + console.log(request["message"]); alert("Failed to generate compiled code. Please check console logs for more details."); } + } catch (err) { + console.error(`Error compiling Xircuits file: ${path}`, err); + alert(`Error compiling file: ${path}. Please check the console logs for more information.`); + } + } + + app.commands.addCommand(commandIDs.compileFile, { + execute: async args => { + const path = tracker.currentWidget.context.path; + const showOutput = args['showOutput'] !== undefined ? (args['showOutput'] as boolean) : false; + + const pythonPaths = {}; + (args['componentList'] === undefined ? [] : args['componentList'] as []).filter(it => it['python_path']).forEach(it => { + pythonPaths[it['name']] = it['python_path'] + }); + + await compileXircuitsFile(path, pythonPaths, showOutput); } }); + // Auto-reload components when a component file changes editorTracker.widgetAdded.connect((sender, widget) => { @@ -359,36 +368,12 @@ const xircuits: JupyterFrontEndPlugin = { // Iterate through selected items and compile each one for (const xircuitsFile of selectedItems) { if (xircuitsFile.path.endsWith('.xircuits')) { - try { - // Retrieve the required compile parameters - const python_paths = {}; // Adjust this based on any additional needs - const request = await requestToGenerateCompileFile(xircuitsFile.path, python_paths); - - if (request["message"] == "completed") { - const modelPath = xircuitsFile.path.split(".xircuits")[0] + ".py"; - if (modelPath.startsWith("xai_components/")) { - console.info(`File ${modelPath} changed. Reloading components...`); - await app.commands.execute(commandIDs.refreshComponentList); - } - alert(`${modelPath} successfully compiled!`); - } else { - console.log(request["message"]); - alert("Failed to generate compiled code. Please check console logs for more details."); - } - } catch (err) { - console.error(`Error compiling Xircuits file: ${xircuitsFile.path}`, err); - alert(`Error compiling file: ${xircuitsFile.path}. Please check the console logs for more information.`); - } + await compileXircuitsFile(xircuitsFile.path); } } } }); - // Add the compile command to the context menu of the file browser - app.contextMenu.addItem({ - command: commandIDs.compileWorkflowFromFileBrowser, - selector: '.jp-DirListing-item[data-file-type="xircuits"]', - }); app.commands.addCommand(commandIDs.copyXircuitsToRoot, { label: 'Copy To Root Directory', @@ -423,9 +408,24 @@ const xircuits: JupyterFrontEndPlugin = { } }); + // Add the compile command to the context menu of the file browser + app.contextMenu.addItem({ + command: commandIDs.compileWorkflowFromFileBrowser, + selector: '.jp-DirListing-item[data-file-type="xircuits"]', + rank: 0 + }); + app.contextMenu.addItem({ command: commandIDs.copyXircuitsToRoot, selector: '.jp-DirListing-item[data-file-type="xircuits"]', + rank: 0 + }); + + // Add a separator after Xircuits commands + app.contextMenu.addItem({ + type: 'separator', + selector: '.jp-DirListing-item[data-file-type="xircuits"]', + rank: 0 }); app.commands.addCommand(commandIDs.openXircuitsConfiguration, {