Skip to content

Commit 5228bc9

Browse files
committed
add endpoint to fetch examples to current working dir
1 parent 24f39cd commit 5228bc9

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

src/commands/CommandIDs.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ export const commandIDs = {
4444
createNewXircuitInCurrentDir: "Xircuit-filebrowser:create-new-in-current-dir",
4545
helpOpenResource: "xircuits-help:open-resource",
4646
openXircuitsConfiguration: "xircuits-config:open-config",
47-
fetchRemoteRunConfig: "xircuits-config:fetch-remote-config"
47+
fetchRemoteRunConfig: "xircuits-config:fetch-remote-config",
48+
fetchExamples: "xircuits-examples:fetch-xircuits-examples"
4849
};

src/index.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,13 @@ const xircuits: JupyterFrontEndPlugin<void> = {
591591
return new Set();
592592
}
593593
}
594+
app.commands.addCommand(commandIDs.fetchExamples, {
595+
label: 'Fetch Example Workflows',
596+
caption: 'Fetch example workflows into the examples directory',
597+
execute: async () => {
598+
await requestAPI('examples', { method: 'POST' });
599+
}
600+
});
594601

595602
function registerTemplateButton(
596603
id: string,
@@ -624,16 +631,16 @@ const xircuits: JupyterFrontEndPlugin<void> = {
624631
installedLibs.add(lib);
625632
}
626633

634+
// Currently the templates are stored at the `examples` dir
635+
await app.commands.execute(commandIDs.fetchExamples);
636+
627637
const model = await app.serviceManager.contents.copy(
628638
examplePath,
629-
currentPath || ''
639+
currentPath || ''
630640
);
631641

632642
const finalPath = model.path;
633643

634-
// small delay to avoid the empty-canvas race
635-
await new Promise(res => setTimeout(res, 200));
636-
637644
// Open the file and refresh the component list
638645
await app.commands.execute('docmanager:open', { path: finalPath });
639646
await app.commands.execute(commandIDs.refreshComponentList);

xircuits/handlers/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .config import RunConfigRouteHandler, SplitModeConfigHandler
66
from .debugger import DebuggerRouteHandler
77
from .request_library import InstallLibraryRouteHandler, FetchLibraryRouteHandler, UninstallLibraryRouteHandler, GetLibraryDirectoryRouteHandler, GetLibraryReadmeRouteHandler, GetLibraryExampleRouteHandler, ReloadComponentLibraryConfigHandler, GetComponentLibraryConfigHandler, CreateNewLibraryHandler
8-
8+
from .request_examples import FetchExamplesRouteHandler
99

1010
def setup_handlers(web_app, url_path):
1111
host_pattern = ".*$"
@@ -29,6 +29,10 @@ def setup_handlers(web_app, url_path):
2929
url_path_join(base_url, url_path, "components/"),
3030
ComponentsRouteHandler
3131
),
32+
(
33+
url_path_join(base_url, url_path, "examples/"),
34+
FetchExamplesRouteHandler
35+
),
3236
(
3337
url_path_join(base_url, url_path, "file/compile"),
3438
CompileXircuitsFileRouteHandler
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
import json
3+
import traceback
4+
import tornado
5+
from http import HTTPStatus
6+
from pathlib import Path
7+
from jupyter_server.base.handlers import APIHandler
8+
from xircuits.utils import is_empty, copy_from_installed_wheel
9+
from xircuits.start_xircuits import find_xircuits_working_dir, init_xircuits
10+
11+
class FetchExamplesRouteHandler(APIHandler):
12+
"""
13+
API Handler to fetch example workflows into the Xircuits working directory.
14+
"""
15+
@tornado.web.authenticated
16+
def post(self):
17+
try:
18+
# Determine the Xircuits working directory (where xai_components exists)
19+
working_dir = find_xircuits_working_dir()
20+
if working_dir is None:
21+
self.set_status(HTTPStatus.BAD_REQUEST)
22+
return self.finish(json.dumps({
23+
"error": "Xircuits is not initialized. Please run 'xircuits init' or set XIRCUITS_INIT."}))
24+
25+
examples_dir = working_dir / "examples"
26+
# Only copy if directory is missing or empty
27+
if not examples_dir.exists() or is_empty(str(examples_dir)):
28+
# Copy into the working directory
29+
copy_from_installed_wheel('examples', resource='', dest_path=str(examples_dir))
30+
message = "Examples have been fetched and saved to the 'examples' folder."
31+
else:
32+
message = "Examples folder already exists and is not empty."
33+
34+
self.finish(json.dumps({"status": "OK", "message": message}))
35+
except Exception as e:
36+
self.set_status(HTTPStatus.INTERNAL_SERVER_ERROR)
37+
error_msg = f"An error occurred while fetching examples: {traceback.format_exc()}"
38+
self.finish(json.dumps({"error": error_msg}))

0 commit comments

Comments
 (0)