mic3333 commited on
Commit
e286f46
Β·
verified Β·
1 Parent(s): 8ec449d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
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
- let output = stdout || (result !== undefined ? String(result) : '');
159
- outputText.textContent = output || 'Code executed successfully (no output)';
 
 
 
 
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
- outputText.textContent += '\\n\\nπŸ“Š Plot generated successfully!';
 
 
 
 
 
 
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();