2 Curious python implementations: Substitute Javascript for Python on your website

Today we bring two curious implementations of python. So far we had seen python on the server side with tornado, flask or django but we had never seen an implementation to replace javascript.

Although at that level Javascript is much more advanced as it has many frameworks and libraries like Angular or JQuery, these implementations are functional. They even support http Ajax calls.

The first implementation is brython

This implementation can be downloaded from https://github.com/brython-dev/brython/releases/tag/3.7.0rc1. And you can consult the installation procedure at http://www.brython.info/static_doc/en/install.html in this link you will see how to perform the installation step.

Some curious code examples are:
http://www.brython.info/static_doc/en/ajax.html:

from browser import document, ajax

def on_complete(req):
   if req.status==200 or req.status==0:
       document["result"].html = req.text
   else:
       document["result"].html = "error "+req.text

req = ajax.ajax()
req.bind('complete',on_complete)
# send a POST request to the url
req.open('POST',url,True)
req.set_header('content-type','application/x-www-form-urlencoded')
# send data as a dictionary
req.send({'x':0, 'y':1})

-The second example is with the asyncio library, which is used to make asynchronous calls.

import asyncio

@asyncio.coroutine
def test_wget(urls):
    results = []
    for u in urls:
        req = yield asyncio.HTTPRequest(u)
        results.append(req.response)
    return results

t = asyncio.ensure_future(test_wget(['http://google.com','http://wikipedia.org']))

The required information can be obtained from http://www.brython.info/static_doc/en/intro.html

The second implementation we bring is PyJS

This application has the documentation generated with javadoc. All the API information can be found at http://pyjs.org/api/ so inside you can find all the solutions to the needs you may have when using this python interpreter.

This library can be downloaded from http://pyjs.org/Download.html and can be used as it is shown in its code samples at http://pyjs.org/examples/. As it is said in https://github.com/pyjs/pyjs/wiki/GettingStarted (where it is also indicated how to start) PyJS is a python to javascript “compiler”. It converts the generated code into a functional Javascript application, just like typescript does. It is quite useful if you want to avoid programming in Javascript.

These 2 curious python implementations are only shown as a curiosity, we do not recommend to use any of the 2 tookit in production. If we were to recommend one of the two, we would prefer PyJS because by converting its code to JS you avoid the translation layer every time you run the web.

 

Leave a Reply