0Day Forums
Run Python in a web page - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: Python (https://0day.red/Forum-Python)
+--- Thread: Run Python in a web page (/Thread-Run-Python-in-a-web-page)



Run Python in a web page - oxyblepsia444097 - 09-03-2022

Well, JavaScript you know what it is. It's an interpreted language that allows us to run code directly through the web pages we visit.

Now, what if I told you that we could do exactly the same thing, but in Python?

You tell me: WITCHCRAFT!!!

And you are right. But it's totally feasible thanks to Brython. Brython is an implementation of Python 3 running in the browser, with an interface for DOM elements and events.

Here is a code snippet of what it looks like:



<html>

<head>
<script type="text/javascript" src="/path/to/brython.js"></script>
</head>

<body onload="brython()">

<script type="text/python">
from browser import document, alert

def echo(event):
alert(document["zone"].value)

document["mybutton"].bind("click", echo)
</script>

<input id="zone"><button id="mybutton">click !</button>

</body>

</html>



So you just have to load the brython.js file in the page and then call the brython() function when the page is loaded to be able to put some Python code in a script tag like you would do in JavaScript. A tutorial is available here :

[To see links please register here]

?

Magic, isn't it ?

Brython supports Python 3 syntax, including list comprehensions, generators, metaclasses, imports, etc. and many modules from the CPython distribution.

Brython has libraries for interacting with DOM elements and events, as well as existing JavaScript libraries such as jQuery, D3, Highcharts, Raphael, etc. It supports the latest HTML5/CSS3 specifications, and can use CSS frameworks like Bootstrap3, LESS, SASS, etc.

In short, it's good stuff.

By the way, if you want to see what it looks like, there's a nice page of examples with source code here:

[To see links please register here]

?

It can allow you to use existing code in web pages without having to rewrite everything in JS. And that's nice.

If you want more info about Brython, this is the place to go:

[To see links please register here]




RE: Run Python in a web page - brahmani148 - 12-10-2022

you can also use selenium to automate webhandling with python