David Bolton
August 31, 2021
JavaScript is one of the world’s most popular programming languages. Not only does it usually place highly on lists of popular programming languages produced by RedMonk, Stack Overflow, TIOBE, and other organizations—it also has the world’s largest developer community, at least according to SlashData’s recent State of the Developer Nation.
For web developers, JavaScript is an essential language—perhaps the essential language, given its compatibility. Other rivals have long fallen by the wayside. For example, Delphi allowed you to create form-based applications that ran in the browser on Windows—but nobody used them. More recently, some developers have angled Dart as a possible JavaScript replacement—but it failed to gain traction from many browser creators.
If you’re sick of using JavaScript, though, you could always explore developing Python or C# applications that run in the browser. On a broader level, do these other, popular languages represent a threat to JavaScript’s “territory”? A handful of web development tools allow you to explore what other languages can deliver.
Brython’s goal is to replace JavaScript with Python as the scripting language for browsers. For example, below is the Python source code for “Hello World!” in a browser. Enter your name in the text box, click the button, and a pop-up window appears. Not bad for five lines of code.
from browser import document, alert
from browser.widgets.dialog import InfoDialog
def echo(ev):
InfoDialog("Hello", f"Hello {document['zone'].value} !")
document["test"].bind("click", echo)
All it takes to run this is a short HTML file, and your Python placed inside a script element. This is very elegant. It also runs on smartphones as well as in all modern browsers.
<body onload="brython()">
<script type="text/python">
Python code goes here.
</script>
<button ...>
</body>
A series of modules have been provided for web-related stuff not usually supported in Python—for instance, Ajax, markdown, local storage, timers, web sockets, workers and more. I tried it with Chrome, which seemed fine (but it’s claimed that performance is better with Firefo)x.
The Brython Wiki on GitHub lists a series of apps powered by Brython.
A programming alternative to Bryton is Pyodide, which compiles Python 3.9 with a scientific stack to WebAssembly (scientific stack being NumPy, Pandas, SciPy and over 75 packages). It also lets you install pure Python wheels from PyPi.
This is a fairly new project that only appeared in 2018, and is a spinoff of an unmaintained Iodide project. Like Brython, you run Python programs inside a script but here you have to pass the Python code into a function like this:
pyodide.runPython(`
import sys
sys.version
`);
Despite its newness, Pyodide is a very impressive programming tool but harder to get into than Brython. If you want WebAssembly, then go with Pyodide, as it’s built on it (WebAssembly can be used to improve the performance of Brython, but it’s more complicated).
Other alternatives to Brython and Pyodide include Skulpt and Transcrypt. I learned Python using CodeSkulptor based on Skulpt, on a remote course at Rice University. Skulpt is very good for learning Python as it just runs in the browser. You see an editor window on the left with a run button below and an output window on the right; but it’s more of a sandbox for running Python rather than a way of developing software.
Transcrypt uses a different approach and precompiles Python to JavaScript. This doesn’t use Python between script tags, as that would slow things up. Unlike Brython, it uses JavaScript libraries, not Python. This is an interesting approach, letting you write JavaScript in Python. If you are comfortable with JavaScript and Python, this may work for you.
Be sure to take a look at their gallery of applications.
If you prefer to program and run C# in the browser, Blazor is the way to go. Like Pyodide, it also appeared in 2018 and comes in two flavors: Server and WebAssembly. It’s also cross-platform, so you can develop Blazor apps on Linux and Mac as well as Windows.
Blazor builds on ASP.NET using Razor syntax which mixes HTML with code in an @code block. For instance, this is a webpage that increments a counter when you click the button:
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
In the server version, your C# code runs on the server and communicates with the JavaScript front end using the SignalR protocol. Only in the WebAssembly version is your C# (now compiled as WebAssembly) running in the browser. You can have it hosted in an Asp.Net server so that data is fetched from the server.
Each of these programming technologies, including the C# Blazor WebAssembly, uses JavaScript behind the scenes in one way or another, even if only to load a component. But you don’t need to know JavaScript to use any of them (slthough with Transcrypt it would probably help, as you can look at the JavaScript code that the Python code transpiles to).
With the exception of Blazor, I don’t think any of them are exactly mainstream—but Brython, Pyodide and Transcrypt have potential. As for WebAssembly, because it’s compiled code, it has become very popular for running malware, in particular hidden crypto mining on website visitors’ machines. That said, it’s still only a few years old, so possibly someone out there is going to implement an easy-to-develop language that outputs WebAssembly.
Are any of these a threat to JavaScript? Probably not, but if you’re looking at alternative means for web development, feel free to give these a try.