{"id":5220,"date":"2019-05-04T08:47:25","date_gmt":"2019-05-04T08:47:25","guid":{"rendered":"https:\/\/gtechbooster.com\/?p=5220"},"modified":"2023-06-21T11:42:59","modified_gmt":"2023-06-21T11:42:59","slug":"science-plus-python-webassembly-equals-pyodide","status":"publish","type":"post","link":"https:\/\/gtechbooster.com\/science-plus-python-webassembly-equals-pyodide\/","title":{"rendered":"Science plus Python &amp; WebAssembly Equals Pyodide"},"content":{"rendered":"\n<p>Based on Iodide, we now have Pyodide which allows a Python\ninterpreter to run inside the browser and create living documents there,\n thus bringing data science to the browser.<\/p>\n\n\n<div class=\"gtech-migrated-from-ad-inserter-placement-2\" style=\"text-align: center;\" id=\"gtech-2791691725\"><div style=\"margin-left: auto;margin-right: auto;text-align: center;\" id=\"gtech-2709533024\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/78735\" rel=\"noopener\" class=\"notrack\" aria-label=\"005\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2026\/03\/005.webp\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2026\/03\/005.webp 1000w, https:\/\/gtechbooster.com\/media\/2026\/03\/005-768x768.webp 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" width=\"500\" height=\"500\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div><p>As we&#8217;ve seen in &#8220;<a href=\"https:\/\/gtechbooster.com\/vscode-can-now-run-in-the-browser\">Run VSCode in the browser<\/a>&#8220;,  portability is the new trend sought after. And with that we mean the  ease of running everything inside the ubiquitous browser, be it an IDE, a  game or an interpreter.\u00a0<\/p>\n\n\n<p>Pyodide is the Python offspring of Iodide, the attempt to bring\nJavascript and science to the browser. Iodide was already a success, but\n since Javascript had no well-defined scientific stack whereas Python\ndoes, the idea of replacing JavaScript with Python was suggested. And\nthus, Pyodide.<\/p>\n\n\n<p>The solution was to make a Python interpreter run inside the\nbrowser&#8217;s Javascript virtual machine and, unlike VSCode&#8217;s code-server\nwhich has a server part that the client\/browser communicates with, the\nPython interpreter is loaded exclusively into the browser client-side\nwithout any remote components.<\/p>\n\n\n<p>That happens because of the magic of Webassembly, which allows  compiling languages other than Javascript for use inside the browser.  For example Perl with <a href=\"https:\/\/webperl.zero-g.net\/\">WebPerl<\/a>.  The notion here is that your Perl, or Python or whatever, code is not  translated to JavaScript, but instead their respective  interpreters\/binaries are ported to WebAssembly which subsequently  targets the Javascript VM living inside the browser.<\/p>\n\n\n\n<p>In the case of Pyodide it was a Python interpreter which was followed\n quickly with support for Numpy, Pandas and Matplotlib, by far the most\nused modules in the Python science ecosystem, and later on with support\nfor Scipy and scikit-learn.So not just Python but Python+Scientific\nstack.<\/p>\n\n\n<p>It is worth noting that Pyodide has full access to the browser\u2019s Web\nAPIs.This means that while the data loading and processing is performed\nin Python, the results are handed over to Javascript and WebGL for doing\n the plotting using for instance JS libraries like d3.<\/p>\n\n\n<p>It also makes possible accessing the DOM through Python without  touching Javascript, but as WebAssembly cannot currently directly access  the DOM this happens through a proxy.To access any Web API, WebAssembly  needs to call out to JavaScript, which then makes the Web API call.  Emscripten therefore creates the HTML and JavaScript glue code needed to  achieve this.<\/p>\n\n\n\n<p>In Pyodide you do that by importing the document object from JavaScript over to the Python side.<\/p>\n\n\n<p><code>from js import document<\/code><\/p><div class=\"gtech-mid-cont\" style=\"text-align: center;\" id=\"gtech-2974460199\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-3204399269\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/17207\" rel=\"noopener\" class=\"notrack\" aria-label=\"26001\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2023\/01\/26001.jpeg 1024w, https:\/\/gtechbooster.com\/media\/2023\/01\/26001-768x960.jpeg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" width=\"500\" height=\"625\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>\n\n\n<p>and then start calling methods on it from Python:<\/p>\n\n\n<p><code>document.getElementById(\"myElement\")<\/code><\/p>\n\n\n<p>In this example we&#8217;re making UI widgets:<\/p>\n\n\n<p><code>%% py<\/code><br \/><code>from js import iodide<\/code><br \/><code>button = iodide.output.element('button')<\/code><br \/><code>button.textContent = \"Click me!\"<\/code><br \/><code>count = 0<\/code><br \/><code>def onclick(evt):<\/code><br \/><code>&nbsp; global count<\/code><br \/><code>&nbsp; if elt.style.backgroundColor == 'rgb(255, 255, 255)':<\/code><br \/><code>&nbsp; &nbsp; &nbsp;elt.style.backgroundColor = '#ffcccc'<\/code><br \/><code>&nbsp; &nbsp; &nbsp;elt.innerText = \"I've been changed \" + str(count) +&nbsp;<\/code><code>\" times!\"<\/code><\/p>\n\n\n<p><code>&nbsp; else:<\/code><br \/><code>&nbsp; &nbsp; &nbsp;elt.style.backgroundColor = '#ffffff'<\/code><br \/><code>&nbsp; &nbsp; &nbsp;elt.innerText = \"I've been changed \" + str(count) + \" times!\"<\/code><br \/><code> count += 1<\/code><br \/><code>button.addEventListener('click', onclick)<\/code><\/p>\n\n\n<p>There&#8217;s an ongoing debate based on the current Webassembly&#8217;s\ninability to call the WebAPI&#8217;s directly without the intervention of\nJavascript. The debate is whether WebAssembly is going to render\nJavascript obsolete since you can program the browser through any\nWebassembly supported language.If direct access to the DOM is possible\nthen you might have a case.<\/p>\n\n\n<p>While Pyodide is based on Iodide, it may be used standalone in any\ncontext where you want to run Python inside a web browser. Iodide,\nhowever, forms the basis of a tool with which you can create living\ndocuments in the browser comprising of your code, plots, data tables,\neven interactive visualizations into one document, running your code and\n witness its results exclusively inside the browser.Furthermore you can\nexport and share your notebook so that others can easily access the\nsource and replicate and extend your presentation.<\/p>\n\n\n<p>Their repos on Github with their installation instructions can be found at&nbsp;project&nbsp;<a href=\"https:\/\/github.com\/iodide-project\/iodide\">Iodide<\/a> and&nbsp;project&nbsp;<a href=\"https:\/\/github.com\/iodide-project\/pyodide\">Pyiodide<\/a>&nbsp;while there&#8217;s also an interactive playground at&nbsp;<a href=\"https:\/\/alpha.iodide.io\/notebooks\/300\/\">A Brief Tour of Pyiodide<\/a>&nbsp;where you can do your experiments and experience how it works without having to install anything.<\/p>\n\n\n<p>There you have it;Python after Data Science conquers the Browser too.\u00a0<\/p>\n\n\n<h4 class=\"wp-block-heading\">More Information<\/h4>\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/iodide-project\/iodide\">Iodide<\/a><\/li><li><a href=\"https:\/\/github.com\/iodide-project\/pyodide\">Pyiodide<\/a><\/li><li><a href=\"https:\/\/alpha.iodide.io\/notebooks\/300\/\">A Brief Tour of Pyiodide<\/a>\u00a0<\/li><li><a href=\"https:\/\/hacks.mozilla.org\/2019\/04\/pyodide-bringing-the-scientific-python-stack-to-the-browser\/\">Mozilla Hacks: Pyodide- Bringing the scientific Python stack to the browser<\/a><\/li><\/ul>\n\n\n<pre class=\"wp-block-preformatted\">Credit: Nikos Vaggalis (i-programmer.info)<\/pre>\n<div class=\"gtech-end-cont\" id=\"gtech-2942593170\"><div style=\"margin-right: auto;margin-left: auto;text-align: center;\" id=\"gtech-2108508580\"><a data-bid=\"1\" data-no-instant=\"1\" href=\"https:\/\/gtechbooster.com\/linkout\/76065\" rel=\"noopener\" class=\"notrack\" aria-label=\"26002\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/gtechbooster.com\/media\/2025\/10\/26002.jpg\" alt=\"\"  srcset=\"https:\/\/gtechbooster.com\/media\/2025\/10\/26002.jpg 1200w, https:\/\/gtechbooster.com\/media\/2025\/10\/26002-768x768.jpg 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" width=\"500\" height=\"500\"  style=\"display: inline-block;\" \/><\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Based on Iodide, we now have Pyodide which allows a Python interpreter to run inside the browser and create living documents there, thus bringing data science to the browser. As we&#8217;ve seen in &#8220;Run VSCode in the browser&#8220;, portability is the new trend sought after. And with that we mean the ease of running everything [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":5230,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1915],"tags":[466,6,670,671,146],"class_list":["post-5220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ndocs","tag-iodide","tag-programming","tag-pyodide","tag-python","tag-web-browser"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/5220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/comments?post=5220"}],"version-history":[{"count":0,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/posts\/5220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media\/5230"}],"wp:attachment":[{"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/media?parent=5220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/categories?post=5220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gtechbooster.com\/api-json\/wp\/v2\/tags?post=5220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}