Skip to content

Console redirecting sys.stderr and sys.stdout#13

Merged
AngelaRemolina merged 3 commits into
masterfrom
console-stdout
Jul 3, 2021
Merged

Console redirecting sys.stderr and sys.stdout#13
AngelaRemolina merged 3 commits into
masterfrom
console-stdout

Conversation

@AngelaRemolina
Copy link
Copy Markdown
Owner

@AngelaRemolina AngelaRemolina commented Jun 28, 2021

This PR resolves #12 and partially closes AngelaRemolina/PyZombis#11
This is a different approach to this PR #11 where console.log method is overriden.

by default, print() will output to the web browser console and so are the error messages. sys.stderr and sys.stdout can be assigned to an object with a write() method, and this allows for the redirection of output to go to a window or text area, for example.

This can be found in the Brython documentation page
And that is exactly what I did:

  1. First I redirect the sys.stderr and sys.stdout to a <code> tag I have on the html.
  2. Then inside a try except I run the code received in the textarea with exec() if there is no problem the result is shown on screen. And if there is an Exception I format the traceback received by sys.exc_info() and print in on screen.
  3. The <code> tag is highlighted with highlight.js when there is an error.

Here is a gif of how it's looking:

console-highlight-exec

@AngelaRemolina
Copy link
Copy Markdown
Owner Author

AngelaRemolina commented Jun 28, 2021

Note: if this gets merged, the dist folder must be updated as well. (see branch dist-PR-13)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show console without overriding the console.log or console.error methods Add sugestions from mentors for the brython module.

1 participant