Removing <body onload="brython()"> #2229
Replies: 6 comments 5 replies
-
Hi @PierreQuentel For my brython use-case, i'm wondering if the current behaviour still works: I use brython for an educational page to embed editable and executable python blocks (and most importantly: in comparison to wasm alternatives, the turtle module can be used out of the box in brython...): https://ofi.gbsl.website (github.com). I currently use the following mechanism to run python scripts only on demand:
I guess the second step will become obsolete through the Thanks for diving in to my usecase :) |
Beta Was this translation helpful? Give feedback.
-
The new behavior is great. Nice job Pierre! In some circumstances, you may want to manually invoke brython () after doing other setup. @lebalz gave one example. In my case, using brython in browser extensions, I use js to start brython when certain conditions are met to avoid the memory hit on every page (there are many many open pages). Perhaps a parameter in the brython script tag could turn off auto-load? Something like : |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback ! In version 3.12.0 I have added and documented a couple of methods of the For your use cases, you can use
<button id="run_py_disabled">run text/py-disabled</button>
<script type="text/py-disabled" id="pyscript" debug=2>
print('run text/py-disabled', __name__)
</script>
<script>
document.getElementById('run_py_disabled').addEventListener('click',
function(ev){
for(var script of document.querySelectorAll('script[type="text/py-disabled"]')){
__BRYTHON__.runPythonSource(script.textContent, script.id)
}
}
)
<brython-options ids=""></brython-options>
for(var script of document.querySelectorAll('script[type="text/python"]')){
__BRYTHON__.runPythonSource(script.textContent, script.id)
} I think this feature is more flexible than observing attribute changes (from "text/py-disabled" to "text/python") or adding an option "autoload=false". |
Beta Was this translation helpful? Give feedback.
-
I'm not sure to understand what you mean by "starting the Brython interpreter": does it mean loading If it is the second option, in the example I gave, there was no call to <!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="/src/brython.js"></script>
</head>
<body>
<brython-options ids=""></brython-options>
<button id="run_python">run text/python with querySelector</button>
<script type="text/python" debug=1>
print('run Python script')
</script>
<script>
document.getElementById('run_python').addEventListener('click',
function(ev){
for(var script of document.querySelectorAll('script[type="text/python"]')){
__BRYTHON__.runPythonSource(script.textContent, script.id)
}
}
)
</script>
</body>
</html> In the first case, here is another minimal example <!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button id="run_python">run text/python with querySelector</button>
<script type="text/python" debug=1>
print('run Python script')
</script>
<script>
document.getElementById('run_python').addEventListener('click',
function(ev){
// load brython.js in the page
var script = document.createElement('SCRIPT')
script.setAttribute('src', "/src/brython.js")
script.addEventListener('load', function(ev){
// when loaded, run the Python scripts
for(var script of document.querySelectorAll('script[type="text/python"]')){
__BRYTHON__.runPythonSource(script.textContent, script.id)
}
})
document.body.appendChild(script)
}
)
</script>
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
Very good point ! Because of this radical change of removing "onload=brython()", in release 3.12 I have started to introduce this feature on brython.info:
This is still very rough, I will improve the interface in the next releases, something like on the python.org documentation pages |
Beta Was this translation helpful? Give feedback.
-
I did an experiment to verify my findings. Made a minimal html page on localhost. 3 versions of the page:
Here is the memory usage of each page according to Chrome (Total JS Heap Size in dev tools Memory tab):
Tested with both 3.8.9 and 3.11.0. Numbers differ slightly but pattern is the same. ResultsSo the biggest memory hit actually isn't from invoking My mistake was thinking that delaying ConclusionThe changes proposed above are fine, shouldn't impact my use case. I could save more memory by not inserting |
Beta Was this translation helpful? Give feedback.
-
Hello,
Since the beginning of Brython, there have been 3 requirements to load Python scripts in the browser:
brython.js
in the page<script type="text/python">
<body onload="brython()">
Commit 3793337 removes the 3rd requirement : you don't have to explicitely call brython() any more.
A typical Brython page would look like
To achieve this, the Brython engine now includes a mechanism that works this way:
brython.js
is loaded in the page, a list of all the scripts withtype="text/python"
already loaded is initialized as the arraypython_scripts
MutationObserver
then tracks the other Python scripts loaded in the page afterbrython.js
and adds them topython_scripts
brython.js
adds an event listener on DOMContentLoaded: when the HTML page has finished loading, the program checks if the<body>
element has a property 'onload'.onload
of<body>
is set to functionbrython
onload
callback is executed. If this callback called thebrython
function, stop here, otherwise callbrython()
The change is backwards-compatible : if the page has
<body onload="brython({debug=2, ...})>
, the programs runs the same as before (except that the list of Python scripts has already been initialized, but this is transparent for the user).If
brython()
is not called, the options such as debug, pythonpath etc. that were passed as arguments tobrython()
are now specified as attributes of the<script>
tag. For instance, the debug level is specified asAt the moment this per-script options setting is only partially included (there is some work ahead to replace the current mechanism).
Feedback is as always much appreciated !
Pierre
Beta Was this translation helpful? Give feedback.
All reactions