Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,24 +41,16 @@ def create_pyodide_interface():
|
|
| 41 |
try {
|
| 42 |
updateStatus('π Loading Pyodide core...', 'blue');
|
| 43 |
|
| 44 |
-
// Load Pyodide with error handling
|
| 45 |
pyodide = await loadPyodide({
|
| 46 |
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/"
|
| 47 |
});
|
| 48 |
|
| 49 |
updateStatus('π¦ Loading Python packages (numpy, matplotlib, pandas)...', 'blue');
|
| 50 |
|
| 51 |
-
|
| 52 |
-
await pyodide.loadPackage("numpy");
|
| 53 |
-
updateStatus('π¦ numpy loaded, loading matplotlib...', 'blue');
|
| 54 |
-
|
| 55 |
-
await pyodide.loadPackage("matplotlib");
|
| 56 |
-
updateStatus('π¦ matplotlib loaded, loading pandas...', 'blue');
|
| 57 |
-
|
| 58 |
-
await pyodide.loadPackage("pandas");
|
| 59 |
updateStatus('βοΈ Setting up matplotlib backend...', 'blue');
|
| 60 |
|
| 61 |
-
// Set up matplotlib backend for web
|
| 62 |
pyodide.runPython(`
|
| 63 |
import matplotlib
|
| 64 |
matplotlib.use('AGG')
|
|
@@ -68,22 +60,40 @@ def create_pyodide_interface():
|
|
| 68 |
import io
|
| 69 |
import base64
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
|
|
|
|
|
|
| 81 |
return None
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
original_show = plt.show
|
| 85 |
def custom_show(*args, **kwargs):
|
| 86 |
-
return
|
| 87 |
plt.show = custom_show
|
| 88 |
|
| 89 |
print("Pyodide Python environment ready!")
|
|
@@ -92,7 +102,6 @@ def create_pyodide_interface():
|
|
| 92 |
pyodideReady = true;
|
| 93 |
updateStatus('β
Pyodide ready! You can now execute Python code.', 'green');
|
| 94 |
|
| 95 |
-
// Show a test message
|
| 96 |
document.getElementById('pyodide-output').style.display = 'block';
|
| 97 |
document.getElementById('output-text').textContent = 'Pyodide initialization complete! Ready to execute Python code.';
|
| 98 |
|
|
@@ -118,6 +127,9 @@ def create_pyodide_interface():
|
|
| 118 |
try {
|
| 119 |
updateStatus('βΆοΈ Executing Python code...', 'blue');
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
// Capture stdout
|
| 122 |
pyodide.runPython(`
|
| 123 |
import sys
|
|
@@ -129,24 +141,18 @@ def create_pyodide_interface():
|
|
| 129 |
// Execute user code
|
| 130 |
let result = pyodide.runPython(code);
|
| 131 |
|
| 132 |
-
// Get captured output
|
| 133 |
let stdout = pyodide.runPython(`
|
| 134 |
sys.stdout = old_stdout
|
| 135 |
captured_output.getvalue()
|
| 136 |
`);
|
| 137 |
|
| 138 |
-
//
|
| 139 |
-
let plotData =
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
else:
|
| 145 |
-
None
|
| 146 |
-
`);
|
| 147 |
-
} catch (e) {
|
| 148 |
-
console.log('No plot generated or plot error:', e);
|
| 149 |
-
}
|
| 150 |
|
| 151 |
// Display results
|
| 152 |
let outputDiv = document.getElementById('pyodide-output');
|
|
@@ -155,29 +161,34 @@ def create_pyodide_interface():
|
|
| 155 |
|
| 156 |
outputDiv.style.display = 'block';
|
| 157 |
|
| 158 |
-
//
|
| 159 |
-
let
|
| 160 |
-
|
| 161 |
-
cleanOutput = cleanOutput.replace(/iVBORw0KGgoAAAANSUhEUgAA[A-Za-z0-9+/=]+/g, '[Image data filtered]');
|
| 162 |
-
|
| 163 |
-
outputText.textContent = cleanOutput || 'Code executed successfully (no output)';
|
| 164 |
|
| 165 |
// Display plot if generated
|
| 166 |
-
if (plotData && plotData.length > 100) {
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
if (cleanOutput.trim()) {
|
| 171 |
outputText.textContent += '\\n\\nπ Plot generated and displayed below!';
|
| 172 |
} else {
|
| 173 |
outputText.textContent = 'π Plot generated and displayed below!';
|
| 174 |
}
|
| 175 |
updateStatus('β
Code executed with plot generated!', 'green');
|
| 176 |
-
return 'Code executed successfully with plot
|
| 177 |
} else {
|
|
|
|
| 178 |
plotContainer.innerHTML = '';
|
| 179 |
updateStatus('β
Code executed successfully!', 'green');
|
| 180 |
-
return
|
| 181 |
}
|
| 182 |
|
| 183 |
} catch (error) {
|
|
@@ -206,7 +217,7 @@ def create_pyodide_interface():
|
|
| 206 |
safeInitPyodide();
|
| 207 |
}
|
| 208 |
|
| 209 |
-
// Make
|
| 210 |
window.executePyodideCode = executePyodideCode;
|
| 211 |
window.checkPyodideStatus = () => pyodideReady;
|
| 212 |
</script>
|
|
|
|
| 41 |
try {
|
| 42 |
updateStatus('π Loading Pyodide core...', 'blue');
|
| 43 |
|
|
|
|
| 44 |
pyodide = await loadPyodide({
|
| 45 |
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/"
|
| 46 |
});
|
| 47 |
|
| 48 |
updateStatus('π¦ Loading Python packages (numpy, matplotlib, pandas)...', 'blue');
|
| 49 |
|
| 50 |
+
await pyodide.loadPackage(["numpy", "matplotlib", "pandas"]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
updateStatus('βοΈ Setting up matplotlib backend...', 'blue');
|
| 52 |
|
| 53 |
+
// Set up matplotlib backend for web - IMPROVED VERSION
|
| 54 |
pyodide.runPython(`
|
| 55 |
import matplotlib
|
| 56 |
matplotlib.use('AGG')
|
|
|
|
| 60 |
import io
|
| 61 |
import base64
|
| 62 |
|
| 63 |
+
# Global variable to store plot data
|
| 64 |
+
_current_plot_data = None
|
| 65 |
+
|
| 66 |
+
def capture_plot():
|
| 67 |
+
global _current_plot_data
|
| 68 |
try:
|
| 69 |
+
if len(plt.get_fignums()) > 0:
|
| 70 |
+
buffer = io.BytesIO()
|
| 71 |
+
plt.savefig(buffer, format='png', bbox_inches='tight', dpi=100)
|
| 72 |
+
buffer.seek(0)
|
| 73 |
+
plot_data = buffer.getvalue()
|
| 74 |
+
buffer.close()
|
| 75 |
+
_current_plot_data = base64.b64encode(plot_data).decode()
|
| 76 |
+
plt.close('all') # Close all figures
|
| 77 |
+
return _current_plot_data
|
| 78 |
+
else:
|
| 79 |
+
_current_plot_data = None
|
| 80 |
+
return None
|
| 81 |
except Exception as e:
|
| 82 |
+
print(f"Plot capture error: {e}")
|
| 83 |
+
_current_plot_data = None
|
| 84 |
return None
|
| 85 |
|
| 86 |
+
def get_plot_data():
|
| 87 |
+
return _current_plot_data
|
| 88 |
+
|
| 89 |
+
def clear_plot_data():
|
| 90 |
+
global _current_plot_data
|
| 91 |
+
_current_plot_data = None
|
| 92 |
+
|
| 93 |
+
# Override plt.show() to capture plots automatically
|
| 94 |
original_show = plt.show
|
| 95 |
def custom_show(*args, **kwargs):
|
| 96 |
+
return capture_plot()
|
| 97 |
plt.show = custom_show
|
| 98 |
|
| 99 |
print("Pyodide Python environment ready!")
|
|
|
|
| 102 |
pyodideReady = true;
|
| 103 |
updateStatus('β
Pyodide ready! You can now execute Python code.', 'green');
|
| 104 |
|
|
|
|
| 105 |
document.getElementById('pyodide-output').style.display = 'block';
|
| 106 |
document.getElementById('output-text').textContent = 'Pyodide initialization complete! Ready to execute Python code.';
|
| 107 |
|
|
|
|
| 127 |
try {
|
| 128 |
updateStatus('βΆοΈ Executing Python code...', 'blue');
|
| 129 |
|
| 130 |
+
// Clear any previous plot data
|
| 131 |
+
pyodide.runPython('clear_plot_data()');
|
| 132 |
+
|
| 133 |
// Capture stdout
|
| 134 |
pyodide.runPython(`
|
| 135 |
import sys
|
|
|
|
| 141 |
// Execute user code
|
| 142 |
let result = pyodide.runPython(code);
|
| 143 |
|
| 144 |
+
// Get captured output (this should NOT contain base64 data)
|
| 145 |
let stdout = pyodide.runPython(`
|
| 146 |
sys.stdout = old_stdout
|
| 147 |
captured_output.getvalue()
|
| 148 |
`);
|
| 149 |
|
| 150 |
+
// Get plot data separately
|
| 151 |
+
let plotData = pyodide.runPython('get_plot_data()');
|
| 152 |
+
|
| 153 |
+
console.log('Stdout length:', stdout ? stdout.length : 0);
|
| 154 |
+
console.log('Plot data length:', plotData ? plotData.length : 0);
|
| 155 |
+
console.log('Plot data preview:', plotData ? plotData.substring(0, 50) + '...' : 'None');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
// Display results
|
| 158 |
let outputDiv = document.getElementById('pyodide-output');
|
|
|
|
| 161 |
|
| 162 |
outputDiv.style.display = 'block';
|
| 163 |
|
| 164 |
+
// Display clean text output
|
| 165 |
+
let textOutput = stdout || (result !== undefined ? String(result) : '');
|
| 166 |
+
outputText.textContent = textOutput || 'Code executed successfully (no output)';
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
// Display plot if generated
|
| 169 |
+
if (plotData && plotData.length > 100) {
|
| 170 |
+
console.log('Displaying plot...');
|
| 171 |
+
plotContainer.innerHTML = `
|
| 172 |
+
<div style="margin: 10px 0;">
|
| 173 |
+
<h5>π Generated Plot:</h5>
|
| 174 |
+
<img src="data:image/png;base64,${plotData}"
|
| 175 |
+
style="max-width: 100%; height: auto; border: 1px solid #ddd; border-radius: 3px;"
|
| 176 |
+
alt="Generated Plot">
|
| 177 |
+
</div>
|
| 178 |
+
`;
|
| 179 |
|
| 180 |
+
if (textOutput.trim()) {
|
|
|
|
| 181 |
outputText.textContent += '\\n\\nπ Plot generated and displayed below!';
|
| 182 |
} else {
|
| 183 |
outputText.textContent = 'π Plot generated and displayed below!';
|
| 184 |
}
|
| 185 |
updateStatus('β
Code executed with plot generated!', 'green');
|
| 186 |
+
return 'Code executed successfully with plot';
|
| 187 |
} else {
|
| 188 |
+
console.log('No plot data detected');
|
| 189 |
plotContainer.innerHTML = '';
|
| 190 |
updateStatus('β
Code executed successfully!', 'green');
|
| 191 |
+
return textOutput || 'Code executed successfully';
|
| 192 |
}
|
| 193 |
|
| 194 |
} catch (error) {
|
|
|
|
| 217 |
safeInitPyodide();
|
| 218 |
}
|
| 219 |
|
| 220 |
+
// Make functions globally available
|
| 221 |
window.executePyodideCode = executePyodideCode;
|
| 222 |
window.checkPyodideStatus = () => pyodideReady;
|
| 223 |
</script>
|