I want run funcion to stemm each row of a table. I want do it after of HTML code is loaded, I try this
<body>
<script
type="text/javascript"
src="https://cdn.jsdelivr.net/pyodide/dev/full/pyodide.js"
></script>
<script type="text/javascript">
async function main(kw) {
let pyodide = await loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/dev/full/",
});
pyodide.globals.set("mikw", kw);
await pyodide.loadPackage("micropip");
await pyodide.runPythonAsync(`
import micropip
import js
await micropip.install('snowballstemmer')
import snowballstemmer
stemmer = snowballstemmer.stemmer('english')
div = js.document.createElement("div")
div.innerHTML = stemmer.stemWords('Hello this my default text '.split())
js.document.body.prepend(div)
varP = js.document.getElementById('origen').textContent
print(varP)
salida = stemmer.stemWords(varP.split())
print(salida)
divSalida = js.document.createElement("div")
div.innerHTML =salida
salida = stemmer.stemWords(mikw.split())
print(salida)
`);
}
main('This are the second text, I put this text on arguments function');
</script>
<div id="origen" >This text is inside of HTML element</div>
</body>
The output is
['This', 'text', 'is', 'insid', 'of', 'HTML', 'element']
['This', 'are', 'the', 'second', 'text,', 'I', 'put', 'this', 'text', 'on', 'argument', 'function']
But I can't (or I don't know) use function to stemm other kws after load html, for example after load DOM (to get values of my HTML table and run each)
....
<div id="origen" onclick="temp()">This text is inside of HTML element</div>
<script>
myNewkwStemm=main('other word');
myNewkwStemm2=main('word2');
</script>
</body>
You are loading Pyodide multiple times by including the loadPyodide
call in the main function, which gives an error for your subsequent main
calls. Move loadPyodide
outside main
should resolve the issue:
<script type="text/javascript">
let initPyodide = loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/dev/full/",
})
async function main(kw) {
let pyodide = await initPyodide
// ... other code
}
Also you probably do not want to rely on pyodide.globals
to pass the argument in, it leads to a race condition since you are running everything asynchronously.