Skip to content

[UI] Allow importing the Python script #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions ui/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ function CanvasImpl() {
const autoLayoutROOT = useStore(store, (state) => state.autoLayoutROOT);

const addNode = useStore(store, (state) => state.addNode);
const importIpynb = useStore(store, (state) => state.importIpynb);
const importLocalCode = useStore(store, (state) => state.importLocalCode);
const reactFlowInstance = useReactFlow();

const project = useCallback(
Expand Down Expand Up @@ -780,21 +780,33 @@ function CanvasImpl() {
const handleFileInputChange = async (e: ChangeEvent<HTMLInputElement>) => {
if (!e.target.files || e.target.files.length === 0) return;
const fileName = e.target.files[0].name;
console.log("Import Jupyter Notebook: ", fileName);
console.log("Import Jupyter Notebook or Python scripts: ", fileName);
const fileReader = new FileReader();
fileReader.onload = (e) => {
const fileContent =
typeof e.target!.result === "string"
? e.target!.result
: Buffer.from(e.target!.result!).toString();
let cellList: any[] = [];
let importScopeName = "";
switch (fileName.split(".").pop()) {
case "ipynb":
cellList = JSON.parse(String(fileContent)).cells.map((cell) => ({
cellType: cell.cell_type,
cellSource: cell.source.join(""),
}));
importScopeName = fileName.substring(0, fileName.length - 6);
break;
case "py":
cellList = [{ cellType: "code", cellSource: String(fileContent) }];
break;
default:
return;
}

const cellList = JSON.parse(String(fileContent)).cells.map((cell) => ({
cellType: cell.cell_type,
cellSource: cell.source.join(""),
}));
importIpynb(
importLocalCode(
project({ x: client.x, y: client.y }),
fileName.substring(0, fileName.length - 6),
importScopeName,
cellList
);
setAutoLayoutOnce(true);
Expand All @@ -803,7 +815,7 @@ function CanvasImpl() {
};

useEffect(() => {
// A BIG HACK: we run autolayout once at SOME point after ImportIpynb to
// A BIG HACK: we run autolayout once at SOME point after ImportLocalCode to
// let reactflow calculate the height of pods, then layout them properly.
if (
autoLayoutOnce &&
Expand Down Expand Up @@ -938,7 +950,7 @@ function CanvasImpl() {
</ReactFlow>
<input
type="file"
accept=".ipynb"
accept=".ipynb, .py"
ref={fileInputRef}
style={{ display: "none" }}
onChange={(e) => handleFileInputChange(e)}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/CanvasContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function CanvasContextMenu(props) {
<ListItemIcon sx={{ color: "inherit" }}>
<FileUploadTwoToneIcon />
</ListItemIcon>
<ListItemText>Import Jupyter Notebook</ListItemText>
<ListItemText>Import Code</ListItemText>
</MenuItem>
)}
</MenuList>
Expand Down
10 changes: 5 additions & 5 deletions ui/src/lib/store/canvasSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ export interface CanvasSlice {
parent: string
) => void;

importIpynb: (
importLocalCode: (
position: XYPosition,
repoName: string,
importScopeName: string,
cellList: any[]
) => void;

Expand Down Expand Up @@ -478,8 +478,8 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
}
},

importIpynb: (position, repoName, cellList) => {
console.log("Sync imported Jupyter notebook cells.");
importLocalCode: (position, importScopeName, cellList) => {
console.log("Sync imported Jupyter notebook or Python scripts");
let nodesMap = get().ydoc.getMap<Node>("pods");
let scopeNode = createNewNode("SCOPE", position);
// parent could be "ROOT" or a SCOPE node
Expand All @@ -498,7 +498,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
podParent = parent.id;
}

scopeNode.data.name = repoName;
scopeNode.data.name = importScopeName;
nodesMap.set(scopeNode.id, scopeNode);

get().addPod({
Expand Down