Python dialog box bookmarklet for running code in current web browser tab
With this bookmarklet you can run Python code inside a dialog box on the web page you're currently viewing. Works on most pages, but not on pages with very strict content security policies.
javascript:(function(){ async function main() { let pyodide = await loadPyodide(); await pyodide.loadPackage("micropip"); var logBackup = console.log; var logMessages = []; console.log = function() { logMessages.push.apply(logMessages, arguments); logBackup.apply(console, arguments); }; a = await pyodide.runPythonAsync(_code); var a; wait(function() { return a }, function() { _input.value = logMessages.join("\n") }); setTimeout(function() { a = 1 }, 0); function wait(condition, callback) { if (typeof condition() !== "undefined") { callback(); } else { setTimeout(function () { wait(condition, callback); }, 0) } }; _cancel.innerText = "Close"; _return.style.display = "inline"; }; function callback() { _style = `<style>body:has(#mydialog[open]) { overflow: hidden }; #mydialog::backdrop { all: revert }</style>`; document.head.insertAdjacentHTML('beforeend', _style); _element = `<dialog id="mydialog" style="all: revert; width: clamp(300px, 75vw, 600px); height: 250px; padding-bottom: 0px"><textarea id="myinput" autofocus autocapitalize="none" autocomplete="off" spellcheck="false" style="all: revert; font-family: monospace; font-size: 16px; width: -webkit-fill-available; width: -moz-available; width: stretch; height: 208px; resize: none;"></textarea><button id="mycancelbtn" style="all: revert; font-size: 15px; margin-right: 16px">Cancel</button><button id="myokbtn" style="all: revert; font-size: 15px"><b>OK</b></button><button id="myreturnbtn" style="all: revert; font-size: 15px; display: none">Return</button></dialog>`; document.body.insertAdjacentHTML('afterbegin', _element); fetch(u + 'python_stdlib.zip'); _dialog = document.getElementById("mydialog"); _input = document.getElementById("myinput"); _return = document.getElementById("myreturnbtn"); _ok = document.getElementById("myokbtn"); _cancel = document.getElementById("mycancelbtn"); _return.onclick = function() { _input.removeAttribute('readonly'); _input.value = _code; _return.style.display = "none"; _ok.style.display = "inline"; _cancel.innerText = "Cancel" }; _cancel.onclick = function() { _dialog.remove(); }; _ok.onclick = function() { _ok.style.display = "none"; _input.setAttribute('readonly', ''); _code = _input.value.trimEnd(); if (! _code.includes('print(')) { _return.style.display = "inline"; _input.value = "Code error: no print()" } else { _input.value = "Please wait..."; main(); } }; _dialog.showModal(); _input.value = `import micropip\nawait micropip.install("numpy")\nimport numpy as np\n\nn = 8\n\n# Create a nxn matrix filled with 0\nmatrix = np.zeros((n, n), dtype=int)\n\n# fill 1 with alternate rows and column\nmatrix[::2, 1::2] = 1\nmatrix[1::2, ::2] = 1\n\n# Print the checkerboard pattern\nfor i in range(n):\n for j in range(n):\n print(matrix[i][j], end=" ")\n print()`; _input.scrollTop = _input.scrollHeight }; document.addEventListener('securitypolicyviolation', (e) => { alert("Could not run bookmarklet due to web page security settings"); _dialog.remove(); }, { once: true }); var s = document.createElement("script"); const u = 'https://cdn.jsdelivr.net/pyodide/v0.27.0/full/'; s.src = u + 'pyodide.js'; if (s.addEventListener) {s.addEventListener("load", callback, false, { once: true })} else if (s.readyState) { s.onreadystatechange = callback }; document.body.appendChild(s) })() /*www.no-op.co*/