pyodide.worker.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { loadPyodide, type PyodideInterface } from 'pyodide';
  2. declare global {
  3. interface Window {
  4. stdout: string | null;
  5. stderr: string | null;
  6. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  7. result: any;
  8. pyodide: PyodideInterface;
  9. packages: string[];
  10. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  11. [key: string]: any;
  12. }
  13. }
  14. async function loadPyodideAndPackages(packages: string[] = []) {
  15. self.stdout = null;
  16. self.stderr = null;
  17. self.result = null;
  18. self.pyodide = await loadPyodide({
  19. indexURL: '/pyodide/',
  20. stdout: (text) => {
  21. console.log('Python output:', text);
  22. if (self.stdout) {
  23. self.stdout += `${text}\n`;
  24. } else {
  25. self.stdout = `${text}\n`;
  26. }
  27. },
  28. stderr: (text) => {
  29. console.log('An error occurred:', text);
  30. if (self.stderr) {
  31. self.stderr += `${text}\n`;
  32. } else {
  33. self.stderr = `${text}\n`;
  34. }
  35. },
  36. packages: ['micropip']
  37. });
  38. const micropip = self.pyodide.pyimport('micropip');
  39. // await micropip.set_index_urls('https://pypi.org/pypi/{package_name}/json');
  40. await micropip.install(packages);
  41. }
  42. self.onmessage = async (event) => {
  43. const { id, code, ...context } = event.data;
  44. console.log(event.data);
  45. // The worker copies the context in its own "memory" (an object mapping name to values)
  46. for (const key of Object.keys(context)) {
  47. self[key] = context[key];
  48. }
  49. // make sure loading is done
  50. await loadPyodideAndPackages(self.packages);
  51. try {
  52. self.result = await self.pyodide.runPythonAsync(code);
  53. // Safely process and recursively serialize the result
  54. self.result = processResult(self.result);
  55. console.log('Python result:', self.result);
  56. } catch (error) {
  57. self.stderr = error.toString();
  58. }
  59. self.postMessage({ id, result: self.result, stdout: self.stdout, stderr: self.stderr });
  60. };
  61. function processResult(result: any): any {
  62. // Catch and always return JSON-safe string representations
  63. try {
  64. if (result == null) {
  65. // Handle null and undefined
  66. return null;
  67. }
  68. if (typeof result === 'string' || typeof result === 'number' || typeof result === 'boolean') {
  69. // Handle primitive types directly
  70. return result;
  71. }
  72. if (Array.isArray(result)) {
  73. // If it's an array, recursively process items
  74. return result.map((item) => processResult(item));
  75. }
  76. if (typeof result.toJs === 'function') {
  77. // If it's a Pyodide proxy object (e.g., Pandas DF, Numpy Array), convert to JS and process recursively
  78. return processResult(result.toJs());
  79. }
  80. if (typeof result === 'object') {
  81. // Convert JS objects to a recursively serialized representation
  82. const processedObject: { [key: string]: any } = {};
  83. for (const key in result) {
  84. if (Object.prototype.hasOwnProperty.call(result, key)) {
  85. processedObject[key] = processResult(result[key]);
  86. }
  87. }
  88. return processedObject;
  89. }
  90. // Stringify anything that's left (e.g., Proxy objects that cannot be directly processed)
  91. return JSON.stringify(result);
  92. } catch (err) {
  93. // In case something unexpected happens, we return a stringified fallback
  94. return `[processResult error]: ${err.toString()}`;
  95. }
  96. }
  97. export default {};