Skip to content

Commit

Permalink
Merge pull request #123 from XpressAI/adry/fix-component-parser
Browse files Browse the repository at this point in the history
🐛Skip category when error occurred during components parsing
  • Loading branch information
MFA-X-AI authored Mar 24, 2022
2 parents 9d5c23e + 88103d9 commit 1fd6965
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 38 deletions.
28 changes: 7 additions & 21 deletions src/components/xircuitBodyWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface BodyWidgetProps {
commands: any;
widgetId?: string;
serviceManager: ServiceManager;
fetchComponentsSignal: Signal<XPipePanel, any>;
saveXircuitSignal: Signal<XPipePanel, any>;
compileXircuitSignal: Signal<XPipePanel, any>;
runXircuitSignal: Signal<XPipePanel, any>;
Expand Down Expand Up @@ -123,6 +124,7 @@ export const BodyWidget: FC<BodyWidgetProps> = ({
commands,
widgetId,
serviceManager,
fetchComponentsSignal,
saveXircuitSignal,
compileXircuitSignal,
runXircuitSignal,
Expand Down Expand Up @@ -1516,14 +1518,11 @@ export const BodyWidget: FC<BodyWidgetProps> = ({

signalConnections.forEach(connectSignal);

const fetchComponentList = async () => {
const response = await ComponentList(serviceManager);

if (response.length > 0) {
setComponentList([]);
}
setComponentList(response);
}
useEffect(() => {
fetchComponentsSignal.connect((_, args) => {
setComponentList(args)
});
}, [fetchComponentsSignal])

useEffect(() => {
let runType;
Expand All @@ -1540,19 +1539,6 @@ export const BodyWidget: FC<BodyWidgetProps> = ({
});
}, [debugMode, inDebugMode])

useEffect(() => {
if (!runOnce) {
fetchComponentList();
}
}, []);

useEffect(() => {
const intervalId = setInterval(() => {
fetchComponentList();
}, 5000);
return () => clearInterval(intervalId);
}, [componentList]);

const dialogFuncMap = {
'displayDebug': setDisplayDebug,
'displayHyperparameter': setDisplayHyperparameter,
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ const xircuits: JupyterFrontEndPlugin<void> = {
changeFavicon(xircuitsFaviconLink);

// Creating the sidebar widget for the xai components
const sidebarWidget = ReactWidget.create(<Sidebar lab={app}/>);
const sidebarWidget = ReactWidget.create(<Sidebar lab={app} factory={widgetFactory}/>);
sidebarWidget.id = 'xircuits-component-sidebar';
sidebarWidget.title.icon = componentLibIcon;
sidebarWidget.title.caption = "Xircuits Component Library";
Expand Down
14 changes: 0 additions & 14 deletions src/tray_library/Component.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/tray_library/Component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ServiceManager } from '@jupyterlab/services';
import { showDialog, Dialog } from '@jupyterlab/apputils';

import {requestAPI} from "../server/handler";
import React from 'react';

async function get_all_components_method() {
const response = await requestAPI<any>('components/');
const components = response["components"];
const error_msg = response["error_msg"];

if(error_msg){
showDialog({
title: 'Parse Component Failed',
body: (
<pre>{error_msg}</pre>
),
buttons: [Dialog.warnButton({ label: 'OK' })]
});
}
return components;
}

export default async function ComponentList(serviceManager: ServiceManager) {
let component_list_result: string[] = await get_all_components_method();

return component_list_result;
}
9 changes: 9 additions & 0 deletions src/tray_library/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "react-accessible-accordion";

import { requestAPI } from '../server/handler';
import { XircuitFactory } from '../xircuitFactory';

export const Body = styled.div`
flex-grow: 1;
Expand Down Expand Up @@ -72,6 +73,7 @@ const colorList_general = [

export interface SidebarProps {
lab: JupyterFrontEnd;
factory: XircuitFactory;
}

async function fetchComponent(componentList: string[]) {
Expand Down Expand Up @@ -173,6 +175,13 @@ export default function Sidebar(props: SidebarProps) {
return () => clearInterval(intervalId);
}, [category, componentList]);

useEffect(() => {
const intervalId = setInterval(async () => {
await props.factory.fetchComponentsSignal.emit(componentList);
}, 300); // Send component list to canvas once render or when refresh
return () => clearInterval(intervalId);
},[componentList, handleRefreshOnClick]);

return (
<Body>
<Content>
Expand Down
3 changes: 3 additions & 0 deletions src/xircuitFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class XircuitFactory extends ABCWidgetFactory<DocumentWidget> {
shell: ILabShell;
commands: any;
serviceManager: ServiceManager;
fetchComponentsSignal: Signal<this, any>;
saveXircuitSignal: Signal<this, any>;
compileXircuitSignal: Signal<this, any>;
runXircuitSignal: Signal<this, any>;
Expand All @@ -57,6 +58,7 @@ export class XircuitFactory extends ABCWidgetFactory<DocumentWidget> {
this.shell = options.shell;
this.commands = options.commands;
this.serviceManager = options.serviceManager;
this.fetchComponentsSignal = new Signal<this, any>(this);
this.saveXircuitSignal = new Signal<this, any>(this);
this.compileXircuitSignal = new Signal<this, any>(this);
this.runXircuitSignal = new Signal<this, any>(this);
Expand Down Expand Up @@ -84,6 +86,7 @@ export class XircuitFactory extends ABCWidgetFactory<DocumentWidget> {
commands: this.commands,
context: context,
serviceManager: this.serviceManager,
fetchComponentsSignal: this.fetchComponentsSignal,
saveXircuitSignal: this.saveXircuitSignal,
compileXircuitSignal: this.compileXircuitSignal,
runXircuitSignal: this.runXircuitSignal,
Expand Down
3 changes: 3 additions & 0 deletions src/xircuitWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class XPipePanel extends ReactWidget {
context: Context;
xircuitsApp: XircuitsApplication;
serviceManager: ServiceManager;
fetchComponentsSignal: Signal<this,any>;
saveXircuitSignal: Signal<this, any>;
compileXircuitSignal: Signal<this, any>;
runXircuitSignal: Signal<this, any>;
Expand All @@ -43,6 +44,7 @@ export class XPipePanel extends ReactWidget {
this.commands = options.commands;
this.context = options.context;
this.serviceManager = options.serviceManager;
this.fetchComponentsSignal = options.fetchComponentsSignal;
this.saveXircuitSignal = options.saveXircuitSignal;
this.compileXircuitSignal = options.compileXircuitSignal;
this.runXircuitSignal = options.runXircuitSignal;
Expand Down Expand Up @@ -104,6 +106,7 @@ export class XPipePanel extends ReactWidget {
commands={this.commands}
widgetId={this.parent?.id}
serviceManager={this.serviceManager}
fetchComponentsSignal={this.fetchComponentsSignal}
saveXircuitSignal={this.saveXircuitSignal}
compileXircuitSignal={this.compileXircuitSignal}
runXircuitSignal={this.runXircuitSignal}
Expand Down
15 changes: 13 additions & 2 deletions xircuits/handlers/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import ast
from itertools import chain
import traceback

import tornado
from jupyter_server.base.handlers import APIHandler
Expand Down Expand Up @@ -68,6 +69,7 @@ class ComponentsRouteHandler(APIHandler):
@tornado.web.authenticated
def get(self):
components = []
error_msg = ""

for id, c in DEFAULT_COMPONENTS.items():
components.append({
Expand Down Expand Up @@ -95,7 +97,13 @@ def get(self):
if python_path.parent in default_paths:
python_path = None

components.extend(chain.from_iterable(self.extract_components(f, directory, python_path) for f in python_files))
try:
components.extend(chain.from_iterable(self.extract_components(f, directory, python_path) for f in python_files))
except Exception:
error_msg = traceback.format_exc()
pass
finally:
components.extend(chain.from_iterable(self.extract_components(f, directory, python_path) for f in python_files))


components = list({(c["header"], c["task"]): c for c in components}.values())
Expand All @@ -105,7 +113,10 @@ def get(self):
if c.get("color") is None:
c["color"] = COLOR_PALETTE[idx % len(COLOR_PALETTE)]

self.finish(json.dumps(components))
data = {"components": components,
"error_msg" : error_msg}

self.finish(json.dumps(data))

def get_component_directories(self):
paths = list(DEFAULT_COMPONENTS_PATHS)
Expand Down

0 comments on commit 1fd6965

Please sign in to comment.