Error messages - online Python course

108 views
Skip to first unread message

dgront .

unread,
Mar 24, 2020, 9:47:49 AM3/24/20
to bry...@googlegroups.com
Hi All,

coronavirus outbreak  forced me to make an online version of my Python course. I followed the idea of brisa.io, just made it on Read The Docs website:
(sorry, it's in Polish)

A you can see, each script has its own editor and console. What need most, is to capture error messages from Brython and display them in the console. How can I check if a Brython program runs correctly? If it doesn't, how can I get it's Python-like error message?

The scripting panel is just a simple Python class:

I use
python_runner(editor.getValue())

to run a script. I use https://ace.c9.io/ editor

Best,
Dominik

tecnobillo

unread,
Mar 24, 2020, 10:02:21 AM3/24/20
to brython
You can redirect the standard error output. For example:

```
import sys
from browser import document, alert
from browser.html import *

class Err:
    def write(self, err):
        document <= PRE(err)
        alert(err)

sys.stderr = Err()
```

dgront .

unread,
Mar 24, 2020, 10:54:24 AM3/24/20
to bry...@googlegroups.com
Thanks! Indeed it works.

Actually I've been already redirecting sys.stdout to a PRE element; I didn't realized I can also redirect stderr.

Best,
Dominik

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/83659c28-7917-4ad2-87c2-67962cd2ec8f%40googlegroups.com.

tecnobillo

unread,
Mar 24, 2020, 11:22:42 AM3/24/20
to brython
I'm glad it works, I usually do this to develop applications in Apache Cordova, to see errors.

Good luck during the pandemic. Greetings! :D
To unsubscribe from this group and stop receiving emails from it, send an email to bry...@googlegroups.com.

Angela Sofia Remolina Gutierrez

unread,
Jun 26, 2021, 4:42:14 PM6/26/21
to brython
Hello everyone!

I have a question about the redirect of sys.stderr. I do the class just as you said but when I make a syntax error on purpose, for example print("hello" without closing the parenthesis, the error gets logged on the browser dev tools console and nothing is showed on the html page. I think this does not work to redirect SyntaxError.

Do you have any idea on how to show the traceback for when it is a syntax error on screen?

Here is what I do and what I get on screen:
Captura de pantalla 2021-06-26 153924.png
Captura de pantalla 2021-06-26 153512.png

Edward Elliott

unread,
Jun 26, 2021, 5:42:23 PM6/26/21
to bry...@googlegroups.com
Syntax errors occur before your python code executes.  The script is still being parsed, so any error handling in your code won't take effect yet.

What I do is put an element with a default value on the page, like <p id="status">none</p>.  Then first thing in my script, I do 
doc ['status'] = 'init'
When script finishes setup, I change it to 
doc ['status'] = 'ready'
Error handling in the script prints error messages to doc ['status'].

If there's an error before the script starts, then the status will say "none" and I know to check the console to see what happened.  That may be the best you can do.

Not sure if there's a way to redirect syntax errors to the page.  Nothing in the docs to indicate so.
See here for arguments to brython () and debugging info.


To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/cb8f835e-95b3-4740-8f58-80d42c9e3fc5n%40googlegroups.com.

Angela Sofia Remolina Gutierrez

unread,
Jun 26, 2021, 6:38:18 PM6/26/21
to brython
Thank you for your response! :)

I understand what you do with that element, is like a flag that tell you when something happened, to then check the console that is a great idea!
The thing is I need to show also show the error on screen because we are building like an editor for an online Python course, and we want the student to see everything on the screen and save the step of opening the browser console.

I also tried with the Interpreter module because it shows all mistakes and outputs right away by doing: 

<body onload=brython()> 
<textarea id="code">print("hello")</textarea> 
<script type="text/python3">
 from interpreter import Interpreter Interpreter("code")
</script>
 </body>

but the problem with this one is that the print("hello") gets overwritten by the interpeter so the code never display on screen.

If you know any way of actually attaching like default code to run inside the interpreter that would be very helpful!

Thanks for the help! :D

Edward Elliott

unread,
Jun 27, 2021, 6:10:09 AM6/27/21
to bry...@googlegroups.com
I forgot the .innerHTML on my previous examples.  Should be 
doc ['status'].innerHTML = 'ready'

There should be a way to do what you want, but perhaps not with the interpreter module. Interpreter only recognizes code entered in a text area.  There's no method to programmatically pass code to interpreter.  That leaves (at least) two choices:
  1. Dynamically insert your "real" script text into the text area after the interpreter loads.  This way seems difficult, as you will have to mimic javascript keypress events to trigger the interpreter to execute your code.  More trouble than it's worth.
  2. Build your own loader script from scratch without using interpreter.
#2 isn't as hard as it sounds.  You can use the brython interpreter module code for reference.  Most of the code manages reading and writing to an interactive text area, which you may not need if the students enter complete scripts.  The execution part is quite small, see lines 222 - 262.  All the execution work is done by a single eval () call on line 223 - the rest is error handling.

I would suggest this approach: 
  • make an element on your html page for output messages.  whether it's a textarea, a div, whatever.
  • let the user input their script code somehow.  typing in a text area, upload a file, give a url to the script, copy/paste somewhere, whatever you want.
  • read the script contents into a text string
  • pass the string to eval ()
  • catch any errors and output messages, display them on your page
That should give you full control over catching and displaying any and all errors - including if the user script is not found.  You would also need a way to handle interactive input, if student scripts use it.

Maybe someone else has used brython eval () in this way?  I haven't, can't provide examples.

HTH,
Edward

dgront

unread,
Jun 27, 2021, 7:30:23 AM6/27/21
to bry...@googlegroups.com
So let me show my approach to that. An online console, where students can edit scripts, looks like here:

(click "Uruchom" to run the script)

Here is the code of the widget:
I use ace editor (written in JS) to display script content, but a textarea will work just fine.

Rather than eval() I use browser.run_script() function documented here:
https://brython.info/static_doc/en/browser.html

The other steps are as on Edward's list. Let me know if this solution works for you.

Best,
Dominik





Angela Sofia Remolina Gutierrez

unread,
Jun 27, 2021, 7:50:35 PM6/27/21
to brython
Thank you Dominik and Edward your comments have been incredibly helpful!

I used exec() rather than eval(), because of the way I was reciving the code.

So first I redirect the sys.stderr and sys.stdout to a <code> tag I have on my html


import sys
from browser import document, html
from browser.html import *
logger = document['consoleCode']
preElem = document['consolePre']
class NewOut:
    def write(self, data):
        logger.innerHTML += str(data)
sys.stderr = sys.stdout = NewOut()
          

and then I just used a try except like you recomended where the result or exception gets printed.

import sys
try:
    exec("""${prog}
""")
except Exception as msg:
    trace = str(sys.exc_info())
    print(trace)

And now this is working :D

Thank you for all your help!

- Angela.

Edward Elliott

unread,
Jun 28, 2021, 10:06:29 AM6/28/21
to bry...@googlegroups.com
Great, glad it helped.  One thing - your catch block will catch anything derived from class Exception, but not from class BaseException.  Some runtime errors derive from BaseException instead of Exception.

If you want to catch absolutely everything, maybe catch BaseException, or a generic catch block without a type.

Glad you got it working. 🙂


Angela Sofia Remolina Gutierrez

unread,
Jun 28, 2021, 5:46:38 PM6/28/21
to brython
I did not know that :o 
I will definitely check that out.
Thank you again!

Reply all
Reply to author
Forward
0 new messages