@@ -219,36 +219,51 @@ async function runCodeInline() {
219219 }
220220
221221 // Capture stdout with proper handling for carriage returns (for tqdm)
222- let outputText = '' ;
222+ let outputLines = [ ] ;
223223 let currentLine = '' ;
224224
225225 pyodide . setStdout ( {
226226 batched : ( text ) => {
227227 // Process character by character to handle \r correctly
228228 for ( let char of text ) {
229229 if ( char === '\r' ) {
230- // Carriage return - reset current line (for tqdm progress bars)
231- currentLine = '' ;
230+ // Carriage return - stay on same line, will overwrite
231+ // Don't do anything, just let currentLine get overwritten
232232 } else if ( char === '\n' ) {
233- // Newline - commit current line to output
234- outputText += currentLine + '\n' ;
233+ // Newline - commit current line to output array
234+ outputLines . push ( currentLine ) ;
235235 currentLine = '' ;
236236 } else {
237237 // Regular character - add to current line
238238 currentLine += char ;
239239 }
240240 }
241241
242- // Display accumulated output plus current line
243- const displayText = outputText + currentLine ;
244- output . innerHTML = `<pre style="margin: 0; color: #d4d4d4; white-space: pre-wrap;">${ escapeHtml ( displayText ) } </pre>` ;
242+ // Display all committed lines plus current line being built
243+ const allLines = [ ...outputLines ] ;
244+ if ( currentLine ) {
245+ allLines . push ( currentLine ) ;
246+ }
247+ output . innerHTML = `<pre style="margin: 0; color: #d4d4d4; white-space: pre-wrap;">${ escapeHtml ( allLines . join ( '\n' ) ) } </pre>` ;
245248 }
246249 } ) ;
247250
248251 pyodide . setStderr ( {
249252 batched : ( text ) => {
250- outputText += text ;
251- output . innerHTML = `<pre style="margin: 0; color: #ff6b6b; white-space: pre-wrap;">${ escapeHtml ( outputText ) } </pre>` ;
253+ // For stderr, just append everything
254+ for ( let char of text ) {
255+ if ( char === '\n' ) {
256+ outputLines . push ( currentLine ) ;
257+ currentLine = '' ;
258+ } else {
259+ currentLine += char ;
260+ }
261+ }
262+ const allLines = [ ...outputLines ] ;
263+ if ( currentLine ) {
264+ allLines . push ( currentLine ) ;
265+ }
266+ output . innerHTML = `<pre style="margin: 0; color: #ff6b6b; white-space: pre-wrap;">${ escapeHtml ( allLines . join ( '\n' ) ) } </pre>` ;
252267 }
253268 } ) ;
254269
@@ -284,11 +299,12 @@ __builtins__.input = mock_input
284299
285300 // Commit any remaining content in currentLine
286301 if ( currentLine ) {
287- outputText += currentLine + '\n' ;
288- output . innerHTML = `<pre style="margin: 0; color: #d4d4d4; white-space: pre-wrap;">${ escapeHtml ( outputText ) } </pre>` ;
302+ outputLines . push ( currentLine ) ;
303+ const allLines = outputLines ;
304+ output . innerHTML = `<pre style="margin: 0; color: #d4d4d4; white-space: pre-wrap;">${ escapeHtml ( allLines . join ( '\n' ) ) } </pre>` ;
289305 }
290306
291- if ( ! outputText && ! currentLine ) {
307+ if ( outputLines . length === 0 && ! currentLine ) {
292308 output . innerHTML = '<div style="color: #4CAF50;">✅ Code executed successfully (no output)</div>' ;
293309 }
294310 } catch ( err ) {
0 commit comments