refac: code interpreter
This commit is contained in:
@@ -77,6 +77,35 @@ self.onmessage = async (event) => {
|
||||
await loadPyodideAndPackages(self.packages);
|
||||
|
||||
try {
|
||||
// check if matplotlib is imported in the code
|
||||
if (code.includes('matplotlib')) {
|
||||
// Override plt.show() to return base64 image
|
||||
await self.pyodide.runPythonAsync(`import base64
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
# before importing matplotlib
|
||||
# to avoid the wasm backend (which needs js.document', not available in worker)
|
||||
os.environ["MPLBACKEND"] = "AGG"
|
||||
|
||||
import matplotlib.pyplot
|
||||
|
||||
_old_show = matplotlib.pyplot.show
|
||||
assert _old_show, "matplotlib.pyplot.show"
|
||||
|
||||
def show(*, block=None):
|
||||
buf = BytesIO()
|
||||
matplotlib.pyplot.savefig(buf, format="png")
|
||||
buf.seek(0)
|
||||
# encode to a base64 str
|
||||
img_str = base64.b64encode(buf.read()).decode('utf-8')
|
||||
matplotlib.pyplot.clf()
|
||||
buf.close()
|
||||
print(f"data:image/png;base64,{img_str}")
|
||||
|
||||
matplotlib.pyplot.show = show`);
|
||||
}
|
||||
|
||||
self.result = await self.pyodide.runPythonAsync(code);
|
||||
|
||||
// Safely process and recursively serialize the result
|
||||
|
||||
Reference in New Issue
Block a user