By default, if you try to use the
await in Script preparation block it will fail with error
await is only valid in async functions and at the top level of modules.
It is now possible to execute async code in the Script Preparation block. To do so you'll need to define
globalMeasureThatScriptPrepareFunction function and put the async code in there.
Here is a sample script preparation code:
/* you can put the code here. this code will execute first*/
console.log("In the script preparation block");
async function globalMeasureThatScriptPrepareFunction() {
/* this code will be executed third */
window.globalPyodide = await loadPyodide();
console.log(globalPyodide.runPython('import sys; sys.version'));
await globalPyodide.loadPackage('numpy');
await globalPyodide.runPython(`
import numpy as np
import random
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
def efficient_sort(arr):
return sorted(arr)
def generate_random_array(size, lower_bound, upper_bound):
return [random.randint(lower_bound, upper_bound) for _ in range(size)]
# Generate a random array
random_array = generate_random_array(250, 1, 500000)
print("Original array:", random_array)
`);
}
/*you can put the code here, exact order in which globalMeasureThatScriptPrepareFunction() is defined does not matter.*/
/* this code will be executed second */