Introduction - DOM API
Creating a document
Attributes and methods
Brython-specific built-in modules
Working with Brython
Brython for Firefox OS
Testing, debugging and profiling
Interactive testThe Brythons site, or its mirror available for download, include a console where you can test Python code Please note that the namespace is not refreshed when you click on "run", you must reload the page for that For debugging and testing Brython, a number of test scripts are grouped in the directory
Debugging scriptsWhatever the debugging level, syntax errors are reported in the browser console (or at the place defined by
generates the messagex = $a
By setting the debugging level to 1 in the call to functionSyntaxError: unknown token [$] module '__main__' line 1 x = $a ^
generates :x = [1,2] x
IndexError: list index out of range module '__main__' line 2 x
Debugging Python CodeA simple time-travel step back and forth debugger is implemented here As of this writing it is not full featured and supports only line step. You will find documentation on how each function in the debugger works (in case you want to build on it) Currently only python language specific programs are supported. The debugger does not fully support the input statements; only supporting input with a string literal for argument (more on this below).
rython_Debugger For DevelopersThe debugger provides 4 hooks (on_debugging_started, on_step_update, on_debugging_end, and on_debugging_error) which take a callback that you can decide to do whatever you want with. The way the debugger works in record mode (default) when you run
start the debugging session, takes code to debug as parameter as well as an optional boolean flag for whether to live debug or record. Currently live debug is not supported and debugging by default starts in record mode. TheBrython_Debugger.
function to call when you want to stop the debugging sessionBrython_Debugger.
This function when called steps forward one step in the recorded debugging sessionBrython_Debugger.
This function when called steps backward one step in the recorded debugging sessionBrython_Debugger.
check if you can step to the specified stepBrython_Debugger.
seek to a specific step in the recorded debugging session take a number from 0 to the last step as parameter. If a number larger than the last step is entered nothing will happenBrython_Debugger.
return whether a debugging session is activeBrython_Debugger.
returns whether this debugger is in recording modeBrython_Debugger.
returns whether the current step is the last stepBrython_Debugger.
returns whether the current step is the first stepBrython_Debugger.
return a number indicating the current stepBrython_Debugger.
returns the current frame/state (it should be state)Brython_Debugger.
returns all recorded statesBrython_Debugger.
The maximum number of steps executed before the debugger halts, default 10000Brython_Debugger.
object should contain the data you want paced later to the set_trace functiondo not use event names already used by the debugger add a trace call, (which will be called on step update) Brython_Debugger.
cb is called after debugging session has startedBrython_Debugger.
cb is called after debugging session has endedBrython_Debugger.
cb is called after either a syntax or runtime error occursBrython_Debugger.
cb is called whenever a state is changed using setState
Profiling scriptsTo enable profiling one has to pass the "profile" option to the brython function:
Which will print out something like:from profile import Profilep = Profile() p.enable() do_something() do_something_else() p.create_stats()
where each line corresponds to a function and the different columns correspond to1 run in 0.249 secondsOrdered by: standard name (averaged over 1 run)ncalls tottime percall cumtime var percall module.function:lineno 101/1 0.023 0.000 1.012 0.010 .fact:180
ncalls is the total number number of times the function was called (if the function was called non-recursively, the second number behind the backslash indicates how many calls were top-level calls in the recursion)
tottime is the total time (in seconds) spent in the function not including subcalls
percall is the average time spent in the function per call, not including subcalls
cumtime is the total time spent in function including subcalls
var percall is the average time spent in function per one non-recursive call
standard name is the name of the function in the form module.function_name:line_numberOptionally one can also use the following form, taking advantage of running the code several times and averaging it out:
which will print out something like:from profile import Profilep = Profile() p.call(function_to_profile,200,arg1,arg2,kwarg1=v1)
Collected profile data can be saved to local storage for later use:200 runs in 0.249 secondsOrdered by: standard name (averaged over 1 run)ncalls tottime percall cumtime var percall module.function:lineno 101/1 0.023 0.000 1.012 0.010 function_to_profile:16
Profile data can also be read back:p.dump_stats('run1')
And aggregated togetherdata = Stats('run1')