-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtmp.py
More file actions
221 lines (215 loc) · 7.17 KB
/
tmp.py
File metadata and controls
221 lines (215 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
// pyodide_runner.js - Runs Python code in Pyodide within Deno
import { serve } from "https://deno.land/std/http/server.ts";
// TODO: AVM
import { loadPyodide } from "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.mjs";
//const pyodide = await loadPyodide({ indexURL: "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/" });
let pyodide;
try {
pyodide = await loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/",
});
// Load micropip for package management
await pyodide.loadPackage("micropip");
// Load additional packages if specified
//{self._get_package_loading_code()}
} catch (error) {
console.error("Failed to initialize Pyodide:", error);
Deno.exit(1);
}
// Load Pyodide
//const pyodidePromise = (async () => {
// const pyodide = await import("https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js");
// return await pyodide.loadPyodide();
//})();
"""
# JS_CODE_0 = """\
# // pyodide_runner.js - Runs Python code in Pyodide within Deno
# import { serve } from "https://deno.land/std/http/server.ts";
# import { loadPyodide } from "https://cdn.jsdelivr.net/pyodide/v0.27.5/full/pyodide.mjs";
#
#
# const indexURL = "https://cdn.jsdelivr.net/pyodide/v0.27.5/full/";
# const pyodidePromise = loadPyodide({
# indexURL,
# locateFile: path => indexURL + path
# });
#
#
# // Function to execute Python code and return the result
# async function executePythonCode(code, returnFinalAnswer = false) {
# //TODO:AVM:
# const pyodide = await pyodidePromise;
# //const pyodide = await pyodideReadyPromise;
#
# // Create a capture for stdout
# pyodide.runPython(`
# import sys
# import io
# sys.stdout = io.StringIO()
# `);
#
# // Execute the code and capture any errors
# let result = null;
# let error = null;
# let stdout = "";
#
# try {
# // Execute the code
# if (returnFinalAnswer) {
# // Extract the final_answer call if present
# const finalAnswerMatch = code.match(/final_answer\\s*\\((.*)\\)/);
# if (finalAnswerMatch) {
# // Execute the code up to the final_answer call
# const preCode = code.replace(/final_answer\\s*\\(.*\\)/, "");
# pyodide.runPython(preCode);
#
# // Execute the final_answer expression and get the result
# const finalAnswerExpr = finalAnswerMatch[1];
# result = pyodide.runPython(`${finalAnswerExpr}`);
#
# // Handle image results
# if (result && result.constructor.name === "Image") {
# // Convert PIL Image to base64
# const pngBytes = pyodide.runPython(`
# import io
# import base64
# buf = io.BytesIO()
# _result.save(buf, format='PNG')
# base64.b64encode(buf.getvalue()).decode('utf-8')
# `);
# result = { type: "image", data: pngBytes };
# }
# }
# } else {
# // Just run the code without expecting a final answer
# result = pyodide.runPython(code);
# }
#
# // Get captured stdout
# stdout = pyodide.runPython("sys.stdout.getvalue()");
# } catch (e) {
# error = {
# name: e.constructor.name,
# message: e.message,
# stack: e.stack
# };
# }
#
# return {
# result: result,
# stdout: stdout,
# error: error
# };
# }
#
# // Start a simple HTTP server to receive code execution requests
# const port = 8765;
# console.log(`Starting Pyodide server on port ${port}`);
#
# serve(async (req) => {
# if (req.method === "POST") {
# try {
# const body = await req.json();
# const { code, returnFinalAnswer = false, packages = [] } = body;
#
# // Load any requested packages
# if (packages && packages.length > 0) {
# const pyodide = await pyodidePromise;
# await pyodide.loadPackagesFromImports(code);
# for (const pkg of packages) {
# try {
# await pyodide.loadPackage(pkg);
# } catch (e) {
# console.error(`Failed to load package ${pkg}: ${e.message}`);
# }
# }
# }
#
# const result = await executePythonCode(code, returnFinalAnswer);
# return new Response(JSON.stringify(result), {
# headers: { "Content-Type": "application/json" }
# });
# } catch (e) {
# return new Response(JSON.stringify({ error: e.message }), {
# status: 500,
# headers: { "Content-Type": "application/json" }
# });
# }
# }
#
# return new Response("Pyodide-Deno Executor is running. Send POST requests with code to execute.", {
# headers: { "Content-Type": "text/plain" }
# });
# });
# """
# JS_CODE_2 = """\
# // pyodide_runner.js - Runs Python code in Pyodide within Deno
# import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
# import { loadPyodide } from "https://cdn.jsdelivr.net/pyodide/v0.23.4/full/pyodide.mjs";
#
# const handler = async (request) => {
# try {
# // Initialize Pyodide if not already initialized
# if (!globalThis.pyodide) {
# console.log("Initializing Pyodide...");
# globalThis.pyodide = await loadPyodide({
# indexURL: "https://cdn.jsdelivr.net/pyodide/v0.23.4/full/",
# stdout: (text) => console.log(text),
# stderr: (text) => console.error(text)
# });
# console.log("Pyodide initialized successfully");
# }
#
# if (request.method === "POST") {
# const body = await request.json();
# const code = body.code;
#
# let output = "";
# let error = null;
# let result = null;
#
# try {
# // Redirect stdout/stderr to capture output
# globalThis.pyodide.setStdout({ write: (text) => output += text });
# globalThis.pyodide.setStderr({ write: (text) => output += text });
#
# // Execute the Python code
# result = await globalThis.pyodide.runPythonAsync(code);
#
# return new Response(JSON.stringify({
# output,
# result: result?.toString() || "",
# error: null
# }), {
# headers: { "Content-Type": "application/json" }
# });
# } catch (e) {
# return new Response(JSON.stringify({
# output,
# result: null,
# error: e.toString()
# }), {
# headers: { "Content-Type": "application/json" }
# });
# }
# }
#
# return new Response("Method not allowed", { status: 405 });
# } catch (e) {
# return new Response(JSON.stringify({ error: e.toString() }), {
# status: 500,
# headers: { "Content-Type": "application/json" }
# });
# }
# };
#
# //console.log(`Starting server on port {self.port}...`);
# //await serve(handler, { port: {self.port} });
# //const port = 8765;
# //console.log(`Starting Pyodide server on port ${port}`);
# //await serve(handler, { port: 8765 });
# //const port = 8000;
# //console.log(`Starting Pyodide server on port ${port}`);
# //await serve(handler, { port: 8000 });
# """