Hi,
Indeed, something doesn't seem right.
Ofc course some stuff aren't optimal (e.g. building the HTML from a string), but it shouldn't take so much time.
First, you can try the last version of Brython that came out today that'd reduce the loading time, but it likely won't solve this issue (you only win ~200ms for load time).
From what I see when profiling your website, there is a big cascade of imports, it might be that one of your dependencies has a big dependency tree ?
You import the module "re", without using it it seems (idem for pickle ?).
You use JQuery, that is a big dependency, maybe removing it could help (I assume it also generates lot of JS<=>Python conversions) ?
Try also to see if json/random is a big dependency or not.
Also don't use "open", but asynchronous AJAX queries to not block the page while waiting for the JSON files.
You can prefetch the images (in the header, link tags with preload ?).
Put your script in the header and add the attribute "defer" to start loading it sooner.
if ('forward' in view['neighbors'] and view['neighbors']['forward'] !=
view_number):
forward_arrow = 'arrow_up.gif'
forward_clickable = True
else:
forward_arrow = 'invalid_arrow_up.gif'
forward_clickable = False
This is not a performance issue, but a style issue :
forward_clickable = bool( your_condition )
forward_arrow = f"{"" if forward_clickable else "invalid_"}arrow_up.gif"
Also, use a dict + loop.
for dir in ["forward", "right", ...]:
_isClickable = bool( condition )
isClickable[dir] = _isClickable
arrows[dir] = f"{"" if _isClickable else "invalid_"}arrow_{dir}.gif"
Then your 28 lines that has lot of commons can be written as 5 lines (4 if you don't use the temporary variable).
if direction == 'E':
center = 'center_arrow_right.gif'
alt='Facing East'
elif direction == 'S':
center = 'center_arrow_down.gif'
alt='Facing South'
elif direction == 'W':
center = 'center_arrow_left.gif'
alt='Facing West'
elif direction == 'N':
center = 'center_arrow_up.gif'
alt='Facing North'
Use dicts to do the conversions, e.g. :
center = direction2img[direction]
For "myst_click" I think you might have better ways to handle it.