How to run python code in your browser

codemaker2015

Vishnu Sivan

Posted on May 14, 2022

How to run python code in your browser

Web App development is a domain where newer tools and technologies emerge rapidly. Nowadays, companies are interested in creating web applications in order to attract customers through interactive web applications rather than shows some HTML pages over the internet to stand out.

Javascript is the queen of web programming languages which comprises almost the entire web development tools and frameworks. We can develop a web application in just a fraction of seconds using Angular, React or the native javascript.

Python is so popular with its library support that most companies prefer it as their best choice for AI and data science projects.

As the demand increases, new changes are aligned to develop the application in a rapid manner where the amalgamation of different technologies plays a vital role. By combining python and HTML, we can render the python codes on HTML pages.

Brython is an implementation of Python 3 which can be used to run python codes in your browser. Last week, Anaconda’s CEO Peter Wang announced a revolutionary technology called PyScript which allows users to run python code in their browser.

In this article, we will try to cover the implementations of python in HTML pages.

The big moves in web development — Brython and PyScript.

Getting Started

Let’s start with Brython.

1. Brython

Python frameworks such as Flask and Django is used to render on the server side whereas Brython is serve python code on the client-side.

Brython is a framework which can be used to inject python code over HTML pages. Its primary objective is to replace Javascript as the scripting language for the web. Brython is capable of interacting with DOM thus making it a promising framework on the scripting side. This ability of Brython can be used to create something new in the browser.

Installation

Brython provides CDN links which can be used to integrate with the web application.

  • Add the following code in the HTML file to add the Brython dependency.
  <script src="https://cdn.jsdelivr.net/npm/brython@3.10.5/brython.min.js"></script> 
  <script src="https://cdn.jsdelivr.net/npm/brython@3.10.5/brython_stdlib.js"></script>
Enter fullscreen mode Exit fullscreen mode
  • Bind the function brython() on HTML page load.
  <body onload=brython()>
Enter fullscreen mode Exit fullscreen mode
  • Write Python code inside the tag. </li> </ul> <div class="highlight"><pre class="highlight plaintext"><code> &lt;body onload="brython()"&gt; &lt;script type="text/python"&gt; # your python code &lt;/script&gt; &lt;/body&gt; </code></pre></div> <p></p> <h3> <a name="first-program" href="#first-program" class="anchor"> </a> First program </h3> <p>Let’s try our first Brython framework program which runs on the client-side.<br> </p> <div class="highlight"><pre class="highlight plaintext"><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="https://cdn.jsdelivr.net/npm/brython@3.10.5/brython.min.js"&gt; &lt;/script&gt; &lt;script src="https://cdn.jsdelivr.net/npm/brython@3.10.5/brython_stdlib.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body onload="brython()"&gt; &lt;h2 id="msg"&gt;&lt;/h2&gt; &lt;script type="text/python"&gt; from browser import document document &lt;= "Hello World" document['msg'].text = "Hello World" print("Hello World") &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre></div> <p></p> <p>Brython provides an online console to write and execute the python codes.</p> <p>You can include the python console on your website using,<br> </p> <div class="highlight"><pre class="highlight plaintext"><code> &lt;iframe src="http://brython.info/console.html" width="800" height="400"&gt;&lt;/iframe&gt; </code></pre></div> <p></p> <p>Brython can interact with DOM elements and Javascript libraries such as Highcharts, jQuery, Socket.io, Three.js, etc. It is easy to fetch data from the server using an ajax call from the brython library.</p> <p>If you are interested in trying out brython, checkout the following repositories.</p> <ul> <li><a href="https://github.com/brython-dev/brython/tree/master/www">brython-dev/brython</a></li> <li><a href="https://github.com/codemaker2015/brython-demo">codemaker2015/brython-demo</a></li> </ul> <h2> <a name="2-pyscript" href="#2-pyscript" class="anchor"> </a> 2. <a href="https://github.com/pyscript/pyscript">PyScript</a> </h2> <p>During PyCon US 2022, Anaconda’s CEO Peter Wang unveiled a revolutionary framework — PyScript. Which enables users to write python codes in the browser.</p> <p>PyScript is a JavaScript framework which allows users to embed python codes inside the HTML page like PHP. It enables the users to use the power of python libraries such as numpy, pandas and scikit-learn on web apps. It also enables users to carry out statistical and AI computations inside the web app rather than processing the data on a server and sending the response back to the client.</p> <p>PyScript is built on Pyodide. CPython is a port of Pyodide which is based on WebAssembly. WebAssembly converts human-readable .wat text format language to a binary .wasm format that browsers can run on.</p> <p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0608c30gemgv14b0mk3u.png" alt="pyscript"><br> Image credits: anaconda.cloud/pyscript</p> <h3> <a name="installation" href="#installation" class="anchor"> </a> Installation </h3> <p>PyScript provides CDN links which can be used to integrate with the web application.</p> <ul> <li>Add the following code in the HTML file to add the PyScript dependency. </li> </ul> <div class="highlight"><pre class="highlight plaintext"><code>&lt;link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /&gt; &lt;script defer src="https://pyscript.net/alpha/pyscript.js"&gt;&lt;/script&gt; </code></pre></div> <p></p> <ul> <li>Add PyScript components such as <py-script> and <py-repl> to the html page to write python code snippets inside the browser.</li> <li><code>&lt;py-script&gt;</code>: wraps python code that is executable within the web page.</li> <li><code>&lt;py-repl&gt;</code>: renders the code editor and allows the user to write code in it. </li> </ul> <div class="highlight"><pre class="highlight plaintext"><code>&lt;py-script&gt; # your python code &lt;/py-script&gt; </code></pre></div> <p></p> <h3> <a name="first-program" href="#first-program" class="anchor"> </a> First program </h3> <p>Let’s try our first PyScript framework program which runs on the client-side.<br> </p> <div class="highlight"><pre class="highlight plaintext"><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;title&gt;Hello World&lt;/title&gt; &lt;link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /&gt; &lt;script defer src="https://pyscript.net/alpha/pyscript.js"&gt;&lt;/script&gt; &lt;style&gt; #msg { font-size: x-large; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h1 id="msg"&gt; &lt;py-script&gt; print('Hello World') &lt;/py-script&gt; &lt;/h1&gt; &lt;/body&gt; &lt;/html&gt; </code></pre></div> <p></p> <p>You can include the python console on your website using,<br> </p> <div class="highlight"><pre class="highlight plaintext"><code>&lt;py-repl&gt; print('Hello World') &lt;/py-repl&gt; </code></pre></div> <p></p> <p>For example:<br> </p> <div class="highlight"><pre class="highlight plaintext"><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;title&gt;Hello World&lt;/title&gt; &lt;link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /&gt; &lt;script defer src="https://pyscript.net/alpha/pyscript.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;py-repl&gt; print('Hello World') &lt;/py-repl&gt; &lt;/body&gt; &lt;/html&gt; </code></pre></div> <p></p> <p>If you are interested in trying out pyscript, checkout the following repositories.</p> <ul> <li><a href="https://github.com/pyscript/pyscript/tree/main/pyscriptjs/examples">pyscript/pyscriptjs/examples</a></li> </ul> <p>Thanks for reading this article.</p> <p>If you enjoyed this article, please click on the heart button ♥ and share to help others find it!</p> <p>The full source is available on <a href="https://github.com/codemaker2015/python-on-browser-examples">https://github.com/codemaker2015/python-on-browser-examples</a></p> <p>Here are some useful links,</p> <ul> <li><a href="https://github.com/brython-dev/brython">brython-dev/brython</a></li> <li><a href="https://anaconda.cloud/pyscript-python-in-the-browser">Anaconda Nucleus</a></li> <li><a href="https://pyodide.org/en/stable/">Pyodide</a></li> <li><a href="https://github.com/pyodide/pyodide/tree/main/packages">pyodide/packages</a> </li> </ul> <p>Originally posted on Medium -<br> <a href="https://medium.com/@codemaker2016/how-to-run-python-code-in-your-browser-69b4044b803a">How to run python code in your browser</a></p>
💖 💪 🙅 🚩
codemaker2015
Vishnu Sivan

Posted on May 14, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related