top of page
  • Writer's pictureNick Lansley

JavaScript? My web browser runs Python! And BASIC! and…

%CODEBRYTHONCLOCK%

If you’re reading this in a modern web browser window you should see a clock at the top of this post. Nice clock! Programmed in Python3 by a team called Brython (Browser-Python), it’s running in Python right now on your web browser. Have a look at the source view of this post if you don’t believe m… I’ll wait..

This is a great example of code translation. In reality, your web browser is running a JavaScript library that is interpreting the Python code. The way it works is like this. Once set running, the Brython Javascript interpreter:

  1. Looks for script tags of type “text/python”

  2. Reads the python program code inside the script tags

  3. Interprets the code into JavaScript

  4. Runs the JavaScript version

So, if you’ve grown up with, or just prefer, the friendly and disciplined style of coding offered by Python then there’s nothing to stop you using it on the web.

The choose-your-favourite-language-and-convert-it-to-javascript paradigm is becoming more popular, with several languages becoming available for use with a web browser. The pattern is mainly for ‘on the fly’ code translation with the original language embedded in the web page itself.

Why? Recently JavaScript has had a shot in the arm in terms of usage and capability thanks to two technologies:

  1. AJAX calls which allow a browser to update part of a web page from the server rather than refreshing the whole page. This makes web applications behave much more like fully installed apps on your computer. JavaScript running embedded in the web page makes AJAX happen.

  2. Node.JS – a technology that has brought JavaScript to the server side with some frankly outrageous improvements in web server performance in terms of the number of simultaneous connections that can be serviced by one machine. This is because JavaScript runs on a single CPU thread and is ‘non-blocking’; that is, it can continue doing something else while it waits for a callback that some other process has completed. Node.JS is the big growth area in server-side programming and these language conversions allow you to bring your non-JS skills to this table.

Another example is Google’s DART language, which was invented by Google developers to cut down on common developer errors when programming in JavaScript itself. DART is similar to JavaScript but has these advantages:

  1. It enforces a degree of discipline over the use of different variable types. For example, you can declare a variable as an integer, and an integer it will stay or you’ll get an error.

  2. It makes most of the more common JavaScript tasks easier to code with less chance of bugs.

  3. It simplifies some of the most complex tasks in JavaScript such as setting up and running callbacks and other asynchronous code where errors can abound. Given the whopping size and scale of Google’s web apps, they should know!

An alternative to ‘on the fly’ code translation is to convert the finished program to Javascript then embed that resulting JS code in the web page. An example is JSBasic which has an example page where you write in the beloved programming language of my Sinclair ZX Spectrum-filled youth:

10 print "Hello, world!"20 goto 10

..and click a button which converts BASIC to JavaScript:

function runProgram() 
{ run('console', 22, 40, line10);}/* Generated by the JSBasic compiler at 11:15:13 27/Sep/16 */function line10() 
{ console.println("Hello, world!" ); return line10;}

…which you can embed in a web page.

Quite a movement in re-focussing many classic programming languages for the web that compile or translate the source code of that language to JavaScript. Here are some links I’ve found from a quick search (some are works in progress):

  1. BASIC – JSBasic

  2. Ć (Portable C) – Cito

  3. PASCAL – P2JS

  4. PHP – php.js

A downside to all this? The challenge is that arguably the best code is one that is human-written especially for the task and ideally peer-reviewed. That means getting to know JavaScript itself – especially the new ECMAScript 6 which corrects a lot of the coding issues (and tightens up the discipline of coding, thus fixing common developer bugs) of ECMAScript 5.

These language converters will get you going, but perhaps teasing open a decent ECMAScript 6 book is the very best way of honing your skills for the present and future web.

Baz Luhrmann once said, ‘Wear Sunscreen’. I say, “Learn JavaScript“.

2 views0 comments

Recent Posts

See All

The Daily Peacehaven Timelapse Video

A couple of posts ago I wrote about the ‘WeatherPi’ Raspberry Pi that collects the latest weather readings every 10 minutes and make a decision whether or not to lower the storm shutters in front of o

Live weather data and images!

I wanted to take the peaceful few days between Christmas and New Year to really refresh the data collected from the local weather that helps our Raspberry Pi-powered Storm Shutters Controller (known a

Post: Blog2_Post
bottom of page