Hi Andy, that was helpful to track the order of things. For example the output of this surprised me:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body onload="brython()">
<script type="text/javascript">
console.log("1 - Javascript : Before Loading Brython")
</script>
<script src="
https://raw.githack.com/brython-dev/brython/master/www/src/brython.js"></script>
<script type="text/javascript">
console.log("2 - Javascript : After Loading Brython")
</script>
<script type="text/python">
print("3 - Python : After Loading Brython")
</script>
<script type="text/javascript">
console.log("4 - Javascript : Before loading brython stdlib")
</script>
<script src="
https://raw.githack.com/brython-dev/brython/master/www/src/brython_stdlib.js"></script>
<script type="text/python">
print("5 - Python : After Loading std lib")
</script>
<script type="text/javascript">
console.log("6 - Javascript : After loading brython stdlib")
</script>
</body>
</html>
I'd forgotten that what the brython() on load task does is go through and process all the text/python scripts so the output might surprise people who expect each script in turn to process.
The output is:
1 - Javascript : Before Loading Brython
2 - Javascript : After Loading Brython
4 - Javascript : Before loading brython stdlib
6 - Javascript : After loading brython stdlib
3 - Python : After Loading Brython
5 - Python : After Loading std lib
All javascript runs in order, followed by all python scripts in order. This explains some things about Rays original code. He has this:
"Loading Brython" will only be on the screen for as long as it takes to load the brython.min.js but, and this is critical, NO BUILDING OF INDEXDB etc happens at this point so it will be a flash and not a lot more than that. Same for the next step of "Loading Standard Libraries".
In Rays program we then get to:
<script type="text/python">
print("Loading main python program...")
from browser import document
document["psychology"].text = "Loading Main Program..."
</script>
<!-- Your main logic should go into here -->
<script type="text/python" src="main.py"></script>
These are python scripts so I expect they get read but nothing happens immediately because the body brython() has yet to run to initialize them. Then brython() runs and it puts "Loading Main Program" onto the screen. Then it runs the code of main.py and its at that point that the importing and transpilation starts to happen This is why "Loading Main Program" stays on the screen so long - it's not just "loading" the main program it's doing a lot of other work. The reason it's doing a lot of work is that this program looks innocuous:
# This script is developed based on Brython https://brython.info
from browser import document, bind
from string import capwords as say
@bind("#say", "click")
def echo(event):
message = document["message"].value
document["output"].text = say(message)
--- but the string module is poison for performance. It's actually a very good example of what a largish Brython codebase might take to process. Look at what it ends up pulling in:
load string from precompiled
brython.js:9614 load re from precompiled
brython.js:9614 load enum from precompiled
brython.js:9614 load sys from precompiled
brython.js:9614 load types from precompiled
brython.js:9614 load sre_compile from precompiled
brython.js:9614 load _sre from precompiled
brython.js:9614 load operator from precompiled
brython.js:9614 load _operator from precompiled
brython.js:9614 load sre_constants from precompiled
brython.js:9614 load sre_parse from precompiled
brython.js:9614 load functools from precompiled
brython.js:9614 load abc from precompiled
brython.js:9614 load _py_abc from precompiled
brython.js:9614 load _weakrefset from precompiled
brython.js:9614 load _weakref from precompiled
brython.js:9614 load collections from precompiled
brython.js:9614 load _collections_abc from precompiled
brython.js:9614 load heapq from precompiled
brython.js:9614 load itertools from precompiled
brython.js:9614 load keyword from precompiled
brython.js:9614 load reprlib from precompiled
brython.js:9614 load _thread from precompiled
brython.js:9614 load _collections from precompiled
brython.js:9614 load _functools from precompiled
Change the code to:
# This script is developed based on Brython https://brython.info
from browser import document, bind
@bind("#say", "click")
def echo(event):
message = document["message"].value
document["output"].text = message.title() # Does same as capwords
And it runs instantly and doesn't import all that other stuff from the standard library.
There's something else going on with the animation related to the storagedb saving happening on the main thread and blocking it so that the svg animation is blocked. We can get around that with css animations which are not so blocked but I'll ping Ray on his repo for that.