gwchq-textjam 0.1.67 → 0.1.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,13 +71,10 @@ const PyodideWorker = () => {
71
71
  }
72
72
  break;
73
73
  case "writeFile":
74
- pyodide.FS.writeFile(
75
- `${WORKING_DIR}/${data.filename}`,
76
- encoder.encode(data.content),
77
- );
74
+ pyodide.FS.writeFile(`${WORKING_DIR}/${data.filename}`, encoder.encode(data.content));
78
75
  break;
79
76
  case "runPython":
80
- runPython(data.python);
77
+ runPython(data.python, data.userModuleNames);
81
78
  break;
82
79
  case "stopPython":
83
80
  stopped = true;
@@ -92,7 +89,7 @@ const PyodideWorker = () => {
92
89
  onmessage(event);
93
90
  });
94
91
 
95
- const runPython = async (python) => {
92
+ const runPython = async (python, userModuleNames) => {
96
93
  stopped = false;
97
94
 
98
95
  try {
@@ -106,7 +103,7 @@ const PyodideWorker = () => {
106
103
  postMessage({ method: "handleError", ...parsePythonError(error) });
107
104
  }
108
105
 
109
- await clearPyodideData();
106
+ await clearPyodideData(userModuleNames);
110
107
  };
111
108
 
112
109
  const checkIfStopped = () => {
@@ -413,15 +410,25 @@ const PyodideWorker = () => {
413
410
  },
414
411
  };
415
412
 
416
- const clearPyodideData = async () => {
413
+ const clearPyodideData = async (userModuleNames) => {
417
414
  postMessage({ method: "handleLoading" });
418
- console.log("clearPyodideData");
419
- await pyodide.runPythonAsync(`
415
+ try {
416
+ await pyodide.runPythonAsync(`
420
417
  # Clear all user-defined variables and modules
421
- for name in dir():
422
- if not name.startswith('_') and not name=='basthon':
423
- del globals()[name]
424
- `);
418
+ for name in list(globals()):
419
+ if not name.startswith('_') and not name=='basthon':
420
+ del globals()[name]
421
+
422
+ import sys
423
+ # Remove user modules from sys.modules
424
+ user_modules = ${JSON.stringify(userModuleNames)}
425
+ for name in user_modules:
426
+ if name in sys.modules:
427
+ del sys.modules[name]
428
+ `);
429
+ } catch (error) {
430
+ console.error("Error while clearing Pyodide data:", error);
431
+ }
425
432
  console.log("clearPyodideData done");
426
433
  postMessage({ method: "handleLoaded", stdinBuffer, interruptBuffer });
427
434
  };
@@ -472,13 +479,6 @@ const PyodideWorker = () => {
472
479
  pyodide.setInterruptBuffer(interruptBuffer);
473
480
  }
474
481
 
475
- // add root directory to sys.path for allowing imports of user files
476
- await pyodide.runPythonAsync(`
477
- import sys
478
- if '/' not in sys.path:
479
- sys.path.insert(0, '/')
480
- `);
481
-
482
482
  postMessage({ method: "handleLoaded", stdinBuffer, interruptBuffer });
483
483
  };
484
484
 
package/dist/index.js CHANGED
@@ -369691,6 +369691,7 @@ const preview_svg_1 = __importDefault(__webpack_require__(80417));
369691
369691
  const ProjectTypes_1 = __webpack_require__(27130);
369692
369692
  const projectHelpers_1 = __webpack_require__(2610);
369693
369693
  const Errors_1 = __webpack_require__(20339);
369694
+ const helpers_1 = __webpack_require__(55717);
369694
369695
  const PYTHON_ENTRY_FILE_PATH = "/main.py";
369695
369696
  const url = new URL(PyodideWorker_url_1.default, window.origin).href;
369696
369697
  const PyodideRunner = ({ active, packageApiUrl, }) => {
@@ -369722,6 +369723,7 @@ const PyodideRunner = ({ active, packageApiUrl, }) => {
369722
369723
  const project = (0, stores_1.useAppSelector)((s) => s.editor.project);
369723
369724
  const projectComponents = project.components || [];
369724
369725
  const projectCode = (0, react_1.useMemo)(() => projectComponents.filter((component) => component.type === ProjectTypes_1.ProjectComponentType.FILE), [projectComponents]);
369726
+ const userModuleNames = (0, react_1.useMemo)(() => (0, helpers_1.getPythonUserModuleNames)(projectCode), [projectCode]);
369725
369727
  const focussedFileIndex = (0, stores_1.useAppSelector)((state) => state.editor.focussedFileIndices)[0];
369726
369728
  const openedFiles = (0, stores_1.useAppSelector)((state) => state.editor.openedFiles)[0];
369727
369729
  const codeRunTriggered = (0, stores_1.useAppSelector)((s) => s.editor.codeRunTriggered);
@@ -369924,6 +369926,7 @@ const PyodideRunner = ({ active, packageApiUrl, }) => {
369924
369926
  pyodideWorker?.postMessage({
369925
369927
  method: "runPython",
369926
369928
  python: entryFile.content,
369929
+ userModuleNames,
369927
369930
  });
369928
369931
  };
369929
369932
  const handleStop = () => {
@@ -370127,6 +370130,28 @@ const PythonRunner = ({ packageApiUrl }) => {
370127
370130
  exports["default"] = PythonRunner;
370128
370131
 
370129
370132
 
370133
+ /***/ }),
370134
+
370135
+ /***/ 55717:
370136
+ /***/ ((__unused_webpack_module, exports) => {
370137
+
370138
+
370139
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
370140
+ exports.getPythonUserModuleNames = void 0;
370141
+ const getPythonUserModuleNames = (projectFiles) => {
370142
+ return projectFiles.reduce((moduleNames, component) => {
370143
+ if (component.path) {
370144
+ moduleNames.push(component.path
370145
+ .replace(/^\//, "") // remove leading slash
370146
+ .replace(/\.py$/, "") // remove .py extension
370147
+ .replace(/\//g, "."));
370148
+ }
370149
+ return moduleNames;
370150
+ }, []);
370151
+ };
370152
+ exports.getPythonUserModuleNames = getPythonUserModuleNames;
370153
+
370154
+
370130
370155
  /***/ }),
370131
370156
 
370132
370157
  /***/ 78329:
@@ -381158,7 +381183,7 @@ module.exports = webpackAsyncContext;
381158
381183
  /***/ 24427:
381159
381184
  /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
381160
381185
 
381161
- module.exports = __webpack_require__.p + "assets/PyodideWorker442c933f736a86f038a9.js";
381186
+ module.exports = __webpack_require__.p + "assets/PyodideWorkeraf47e97605454ed8ba8f.js";
381162
381187
 
381163
381188
  /***/ }),
381164
381189
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gwchq-textjam",
3
3
  "description": "Embeddable React editor used in Raspberry Pi text-based projects.",
4
- "version": "0.1.67",
4
+ "version": "0.1.69",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/GirlsFirst/gwchq-textjam",
7
7
  "author": "Girls Who Code HQ",