packages = ['numpy'] [[fetch]] from = './scripts/sandbox/' files = ['smc_1d.py', 'smc_1d_ui.py', 'smc_1d_example.py', 'smc_2d.py', 'smc_2d_example.py', 'hedac.py', 'hedac_example.py', 'common.py', 'ui.py']

(click on the green button to run the code)












from js import document import asyncio from pyodide.ffi import create_proxy # The configuration of each tab config = { 'SMC1D-tab': { 'scripts': { 'main': 'smc_1d.py', 'user': 'smc_1d_example.py', 'ui': 'smc_1d_ui.py', }, 'texts': { 'help': 'If you need context about how this code is used, see this script', 'legend': '
Top: the gaussians defined in initialState()
Middle: the trajectory, both in 1D (in black) and over time (in gray)
Bottom: w_hat (colored) and w (grayscale)', }, }, 'SMC2D-tab': { 'scripts': { 'main': 'smc_2d.py', 'user': 'smc_2d_example.py', 'ui': 'ui.py', 'common': 'common.py', }, 'texts': { 'help': 'If you need context about how this code is used, see this script and this one', 'legend': 'w_hatw', }, 'has_histogram': True, }, 'HEDAC-tab': { 'scripts': { 'main': 'hedac.py', 'user': 'hedac_example.py', 'ui': 'ui.py', 'common': 'common.py', }, 'texts': { 'help': 'If you need context about how this code is used, see this script and this one', 'legend': '', }, } } # UI-related variables canvas = document.getElementById('canvas') canvas_histogram = document.getElementById('canvas_histogram') ctx = canvas.getContext('2d') ctx_histogram = canvas_histogram.getContext('2d') output = document.getElementById('output') help = document.getElementById('help') legend = document.getElementById('legend') def onTabClicked(event): unregister_listeners() switch_to_tab(event.target.id) register_listeners() # Initialise all the tabs for tab, entry in config.items(): scripts = entry['scripts'] for id, filename in scripts.items(): with open(filename, 'r') as f: code = f.read() scripts[id] = { 'filename': filename, 'code': code, } editor = document.getElementById(tab + '-repl') editor.textContent = scripts['user']['code'] button = document.getElementById(tab) button.onclick = onTabClicked def switch_to_tab(id): output.innerText = '' entry = config[id] if ('has_histogram' in entry) and entry['has_histogram']: canvas_histogram.style.display = 'inline'; else: canvas_histogram.style.display = 'none'; texts = entry['texts'] help.innerHTML = texts['help'] legend.innerHTML = texts['legend'] scripts = entry['scripts'] exec(scripts['main']['code'], globals()) exec(scripts['ui']['code'], globals()) if 'common' in scripts: exec(scripts['common']['code'], globals()) editor = document.getElementById(id + '-repl') try: user_code = editor.getPySrc() except AttributeError: user_code = editor.textContent exec(user_code, globals()) reset() terminal = document.getElementsByClassName('py-terminal-docked') if terminal.length == 1: terminal[0].remove() switch_to_tab('SMC1D-tab') async def main(): run_buttons = document.getElementsByClassName("py-repl-run-button") for button in run_buttons: button.addEventListener('click', create_proxy(lambda evt: reset())) while True: # Call the appropriate update function update() # Update the rendering draw_scene(param) await asyncio.sleep(1E-6) pyscript.run_until_complete(main())