Skip to content

Commit

Permalink
Merge pull request #357 from XpressAI/fahreza/compile-from-filebrowser
Browse files Browse the repository at this point in the history
✨ Add Compile Xircuits to Context Menu
  • Loading branch information
MFA-X-AI authored Oct 3, 2024
2 parents e6ecc46 + d80407d commit 46c8ba1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/commands/CommandIDs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
87 changes: 67 additions & 20 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,35 +248,44 @@ const xircuits: JupyterFrontEndPlugin<void> = {
}
}

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) => {
Expand Down Expand Up @@ -343,6 +352,29 @@ const xircuits: JupyterFrontEndPlugin<void> = {
},
});

// 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')) {
await compileXircuitsFile(xircuitsFile.path);
}
}
}
});


app.commands.addCommand(commandIDs.copyXircuitsToRoot, {
label: 'Copy To Root Directory',
isVisible: () => [...browserFactory.tracker.currentWidget.selectedItems()].length > 0,
Expand Down Expand Up @@ -376,9 +408,24 @@ const xircuits: JupyterFrontEndPlugin<void> = {
}
});

// 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, {
Expand Down

0 comments on commit 46c8ba1

Please sign in to comment.