How to run python code in your browser
Tags: python, pyscript, brython
Published: 2022-05-14 18:47:16

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](https://brython.info/)
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>
```
- Bind the function brython() on HTML page load.

```
  <body onload=brython()>
```
- Write Python code inside the <script type=”text/python”> tag.

```
  <body onload="brython()">
  <script type="text/python"> 
    # your python code 
  </script>
  </body>
```
### First program
Let’s try our first Brython framework program which runs on the client-side.

```
  <!DOCTYPE html>
  <html>
  <head>
      <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>
  </head>
  <body onload="brython()">
      <h2 id="msg"></h2>
      <script type="text/python">
          from browser import document
          document <= "Hello World"
          document['msg'].text = "Hello World"
          print("Hello World")
      </script>    
  </body>
  </html>
```
Brython provides an online console to write and execute the python codes.

You can include the python console on your website using,

```
  <iframe src="http://brython.info/console.html" width="800" height="400"></iframe>
```
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.

If you are interested in trying out brython, checkout the following repositories.

- [brython-dev/brython](https://github.com/brython-dev/brython/tree/master/www)
- [codemaker2015/brython-demo](https://github.com/codemaker2015/brython-demo)


## 2. [PyScript](https://github.com/pyscript/pyscript)
During PyCon US 2022, Anaconda’s CEO Peter Wang unveiled a revolutionary framework — PyScript. Which enables users to write python codes in the browser.

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.

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.

![pyscript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0608c30gemgv14b0mk3u.png)
Image credits: anaconda.cloud/pyscript

### Installation
PyScript provides CDN links which can be used to integrate with the web application.

- Add the following code in the HTML file to add the PyScript dependency.
```
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
```
- Add PyScript components such as <py-script> and <py-repl> to the html page to write python code snippets inside the browser.
- `<py-script>`: wraps python code that is executable within the web page.
- `<py-repl>`: renders the code editor and allows the user to write code in it.
```
<py-script> 
    # your python code 
</py-script>
```

### First program
Let’s try our first PyScript framework program which runs on the client-side.

```
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <style>
        #msg {
            font-size: x-large;
        }    
    </style>
</head>
<body>
    <h1 id="msg">
        <py-script> 
            print('Hello World') 
        </py-script>
    </h1>
</body>
</html>
```

You can include the python console on your website using,

```
<py-repl>
    print('Hello World')
</py-repl>
```

For example:

```
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
    <py-repl> 
        print('Hello World') 
    </py-repl>
</body>
</html>
```

If you are interested in trying out pyscript, checkout the following repositories.

- [pyscript/pyscriptjs/examples](https://github.com/pyscript/pyscript/tree/main/pyscriptjs/examples)

Thanks for reading this article.

If you enjoyed this article, please click on the heart button ♥ and share to help others find it!

The full source is available on https://github.com/codemaker2015/python-on-browser-examples

Here are some useful links,

- [brython-dev/brython](https://github.com/brython-dev/brython)
- [Anaconda Nucleus](https://anaconda.cloud/pyscript-python-in-the-browser)
- [Pyodide](https://pyodide.org/en/stable/)
- [pyodide/packages](https://github.com/pyodide/pyodide/tree/main/packages) 

Originally posted on Medium -
[How to run python code in your browser](https://medium.com/@codemaker2016/how-to-run-python-code-in-your-browser-69b4044b803a)