Skip to content

Commit 5a67db4

Browse files
save state of python editor
1 parent 706536f commit 5a67db4

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

src/App.jsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ const DnDFlow = () => {
8181
// Global variables state
8282
const [globalVariables, setGlobalVariables] = useState([]);
8383
const [events, setEvents] = useState([]);
84+
85+
// Python code editor state
86+
const [pythonCode, setPythonCode] = useState("# Define your Python variables and functions here\n# Example:\n# my_variable = 42\n# def my_function(x):\n# return x * 2\n");
87+
8488
const [defaultValues, setDefaultValues] = useState({});
8589
const [isEditingLabel, setIsEditingLabel] = useState(false);
8690
const [tempLabel, setTempLabel] = useState('');
@@ -223,7 +227,8 @@ const DnDFlow = () => {
223227
nodeCounter,
224228
solverParams,
225229
globalVariables,
226-
events
230+
events,
231+
pythonCode
227232
};
228233

229234
// Check if File System Access API is supported
@@ -303,7 +308,15 @@ const DnDFlow = () => {
303308
}
304309

305310
// Load the graph data
306-
const { nodes: loadedNodes, edges: loadedEdges, nodeCounter: loadedNodeCounter, solverParams: loadedSolverParams, globalVariables: loadedGlobalVariables, events: loadedEvents } = graphData;
311+
const {
312+
nodes: loadedNodes,
313+
edges: loadedEdges,
314+
nodeCounter: loadedNodeCounter,
315+
solverParams: loadedSolverParams,
316+
globalVariables: loadedGlobalVariables,
317+
events: loadedEvents,
318+
pythonCode: loadedPythonCode
319+
} = graphData;
307320
setNodes(loadedNodes || []);
308321
setEdges(loadedEdges || []);
309322
setSelectedNode(null);
@@ -321,6 +334,7 @@ const DnDFlow = () => {
321334
});
322335
setGlobalVariables(loadedGlobalVariables ?? []);
323336
setEvents(loadedEvents ?? []);
337+
setPythonCode(loadedPythonCode ?? "# Define your Python variables and functions here\n# Example:\n# my_variable = 42\n# def my_function(x):\n# return x * 2\n");
324338

325339
alert('Graph loaded successfully!');
326340
} catch (error) {
@@ -355,7 +369,15 @@ const DnDFlow = () => {
355369
return;
356370
}
357371

358-
const { nodes: loadedNodes, edges: loadedEdges, nodeCounter: loadedNodeCounter, solverParams: loadedSolverParams, globalVariables: loadedGlobalVariables, events: loadedEvents } = graphData;
372+
const {
373+
nodes: loadedNodes,
374+
edges: loadedEdges,
375+
nodeCounter: loadedNodeCounter,
376+
solverParams: loadedSolverParams,
377+
globalVariables: loadedGlobalVariables,
378+
events: loadedEvents,
379+
pythonCode: loadedPythonCode
380+
} = graphData;
359381
setNodes(loadedNodes || []);
360382
setEdges(loadedEdges || []);
361383
setSelectedNode(null);
@@ -373,6 +395,7 @@ const DnDFlow = () => {
373395
});
374396
setGlobalVariables(loadedGlobalVariables ?? []);
375397
setEvents(loadedEvents ?? []);
398+
setPythonCode(loadedPythonCode ?? "# Define your Python variables and functions here\n# Example:\n# my_variable = 42\n# def my_function(x):\n# return x * 2\n");
376399

377400
alert('Graph loaded successfully!');
378401
} catch (error) {
@@ -1620,6 +1643,8 @@ const DnDFlow = () => {
16201643
globalVariables={globalVariables}
16211644
setGlobalVariables={setGlobalVariables}
16221645
setActiveTab={setActiveTab}
1646+
pythonCode={pythonCode}
1647+
setPythonCode={setPythonCode}
16231648
/>
16241649
)}
16251650

src/GlobalVariablesTab.jsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import React from 'react';
21
import { isValidPythonIdentifier } from './utils.js';
32

43

5-
import { CodeiumEditor } from "@codeium/react-code-editor";
64
import PythonCodeEditor from './PythonCodeEditor';
75
import './PythonCodeEditor.css';
86

9-
export const IdeWithAutocomplete = () => {
7+
export const IdeWithAutocomplete = ({ pythonCode, setPythonCode }) => {
108
const handleCodeExecution = (result) => {
119
if (result.success) {
1210
console.log('Code executed successfully:', result);
@@ -19,6 +17,8 @@ export const IdeWithAutocomplete = () => {
1917
return (
2018
<div>
2119
<PythonCodeEditor
20+
code={pythonCode}
21+
onCodeChange={setPythonCode}
2222
onExecute={handleCodeExecution}
2323
height="500px"
2424
/>
@@ -29,7 +29,9 @@ export const IdeWithAutocomplete = () => {
2929
const GlobalVariablesTab = ({
3030
globalVariables,
3131
setGlobalVariables,
32-
setActiveTab
32+
setActiveTab,
33+
pythonCode,
34+
setPythonCode
3335
}) => {
3436

3537
const addGlobalVariable = () => {
@@ -288,7 +290,10 @@ const GlobalVariablesTab = ({
288290
<p style={{ color: '#bbbbbb', marginBottom: '20px', textAlign: 'center' }}>
289291
Define Python variables and functions here. They will be available in your event functions and throughout the simulation.
290292
</p>
291-
<IdeWithAutocomplete />
293+
<IdeWithAutocomplete
294+
pythonCode={pythonCode}
295+
setPythonCode={setPythonCode}
296+
/>
292297
</div>
293298
</div>
294299
</div>

src/PythonCodeEditor.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import { CodeiumEditor } from "@codeium/react-code-editor";
33
import { getApiEndpoint } from './config.js';
44

55
const PythonCodeEditor = ({
6-
initialCode = "# Define your Python variables and functions here\n# Example:\n# my_variable = 42\n# def my_function(x):\n# return x * 2\n",
6+
code = "# Define your Python variables and functions here\n# Example:\n# my_variable = 42\n# def my_function(x):\n# return x * 2\n",
77
onCodeChange,
88
onExecute,
99
height = "400px"
1010
}) => {
11-
const [code, setCode] = useState(initialCode);
1211
const [isExecuting, setIsExecuting] = useState(false);
1312
const [executionResult, setExecutionResult] = useState(null);
1413

1514
const handleCodeChange = useCallback((newCode) => {
16-
setCode(newCode);
1715
if (onCodeChange) {
1816
onCodeChange(newCode);
1917
}

0 commit comments

Comments
 (0)