Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Can Python functions run in html as javascript does?

I have the following python code

def add(x, y)
      return x + y
add(6,9)

Can I run the python code in html as javascript does?


1 Answer
Rajendra Dharmkar

It is not possible to run Python in any modern browser because none contain a python interpreter. Javascript is the only language which runs in a browser without plugins like Flash or ActiveX.

One way to write Python code that will run in the browser is to use a "transpiler". This is a tool that will compile the python code into Javascript. So the browser is ultimately running the language it knows, but you're writing Python. There are already a number of languages like CoffeeScript, TypeScript and even React JSX templates that compile down to raw javascript.

An example of a Python to Javascript transpiler is Transcript. We must note that since it's not actually Python, there are no guarantees for performance or compatibility since that's largely up to how well the transpiler does it's conversion. We may start with a 3 line Python script that compiles into 30 odd lines of javascript in order to replicate what we are trying to do.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.