#2685·brython

javascript.import_modules() crashes in webworker but works fine not in webworker

Author: cjlee112Created May 14, 2026Updated May 14, 2026

Here is a modtest.py that loads a module just fine:

import javascript

def try_mm(mm):
    print(mm.to_dict()['default']['mermaidAPI'])

javascript.import_modules(['https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs'], try_mm)
print('modtest loaded')

If we include this in HTML head as a webworker, then it crashes with javascript error message "ReferenceError: window is not defined":

<script type="text/python" class="webworker" id="mermaid-ww" src="modtest.py"></script>
<script type="text/python">
from browser import worker

def onready(myworker):
    print('worker ready')
def receive(msg):
    print(f'msg: {msg}')

worker.create_worker('mermaid-ww', onready, receive)
</script>

But if we include this at the same position in HTML head NOT as a webworker, then it works fine (prints a big dictionary of the mermaid API functions):

<script type="text/python" src="modtest.py"></script>

Here is the full console output from the crash:

modtest loaded
worker ready
ReferenceError: window is not defined