Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -135,7 +135,7 @@ def create_pyodide_interface():
|
|
| 135 |
captured_output.getvalue()
|
| 136 |
`);
|
| 137 |
|
| 138 |
-
// Check for plots
|
| 139 |
let plotData = null;
|
| 140 |
try {
|
| 141 |
plotData = pyodide.runPython(`
|
|
@@ -155,21 +155,31 @@ def create_pyodide_interface():
|
|
| 155 |
|
| 156 |
outputDiv.style.display = 'block';
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
// Display plot if generated
|
| 162 |
-
if (plotData) {
|
| 163 |
plotContainer.innerHTML = `<img src="data:image/png;base64,${plotData}" style="max-width: 100%; height: auto; border: 1px solid #ddd;" alt="Generated Plot">`;
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
updateStatus('β
Code executed with plot generated!', 'green');
|
|
|
|
| 166 |
} else {
|
| 167 |
plotContainer.innerHTML = '';
|
| 168 |
updateStatus('β
Code executed successfully!', 'green');
|
|
|
|
| 169 |
}
|
| 170 |
|
| 171 |
-
return output + (plotData ? '\\nπ Plot displayed above' : '');
|
| 172 |
-
|
| 173 |
} catch (error) {
|
| 174 |
console.error('Execution error:', error);
|
| 175 |
document.getElementById('output-text').textContent = 'Error: ' + error.toString();
|
|
|
|
| 135 |
captured_output.getvalue()
|
| 136 |
`);
|
| 137 |
|
| 138 |
+
// Check for plots separately - don't mix with stdout
|
| 139 |
let plotData = null;
|
| 140 |
try {
|
| 141 |
plotData = pyodide.runPython(`
|
|
|
|
| 155 |
|
| 156 |
outputDiv.style.display = 'block';
|
| 157 |
|
| 158 |
+
// Clean text output - exclude any base64 data that might have leaked
|
| 159 |
+
let cleanOutput = stdout || (result !== undefined ? String(result) : '');
|
| 160 |
+
// Filter out any base64 image data that might have accidentally been printed
|
| 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) { // Valid base64 should be long
|
| 167 |
plotContainer.innerHTML = `<img src="data:image/png;base64,${plotData}" style="max-width: 100%; height: auto; border: 1px solid #ddd;" alt="Generated Plot">`;
|
| 168 |
+
|
| 169 |
+
// Add plot success message to text (but not the raw base64)
|
| 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 generated';
|
| 177 |
} else {
|
| 178 |
plotContainer.innerHTML = '';
|
| 179 |
updateStatus('β
Code executed successfully!', 'green');
|
| 180 |
+
return cleanOutput || 'Code executed successfully';
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
| 183 |
} catch (error) {
|
| 184 |
console.error('Execution error:', error);
|
| 185 |
document.getElementById('output-text').textContent = 'Error: ' + error.toString();
|