Skip to content

Commit

Permalink
refactor compile, push xircuits context menu options to top
Browse files Browse the repository at this point in the history
  • Loading branch information
MFA-X-AI committed Oct 2, 2024
1 parent 06be42d commit d80407d
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 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 @@ -359,36 +368,12 @@ const xircuits: JupyterFrontEndPlugin<void> = {
// 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',
Expand Down Expand Up @@ -423,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 d80407d

Please sign in to comment.