Skip to content

promplate/pyth-on-line

Repository files navigation

Introducing Pythonline

Pythonline is a Python interpreter that runs in your browser. It is based on the Pyodide project, which is a WASM build of CPython.

With Pythonline, you can easily share your Python snippets with others, without the need to install Python on their local machine. You can also use it to run Python code on your phone or tablet, without the need to install any apps. Let's take the math module as an example:

>>> import math
>>> math.pi

How about math.e? try hovering this! 👉 math.pi / math.e

Main Features

You can use top-level await:

from asyncio import sleep

for i in range(10):
    print(i, end=" ")
    await sleep(0.1)

Native and informative traceback:

def reciprocal(x: int):
    return 1 / x

Try this:

1 + reciprocal(0)

Basic Usage

Pyodide supports a large subset of the Python standard library. You can use all of them here. It also supports all pure-python libs or adapted hybrid libs such as famous scientific libraries like NumPy, Pandas, SciPy, SciKit-Learn, etc.

Furthermore, you can use global variables like navigator from the window scope by:

from js import navigator

print(navigator.languages)
await navigator.clipboard.readText()

Let's try invoking web requests:

from asyncio import gather
from pyodide.http import pyfetch  # which is just a wrapper on the fetch in js

async def f(url):
    res = await pyfetch(url, method="HEAD", cache="no-store")
    print(res.status, res.status_text, res.headers.get("content-type"))
    return res.ok

await gather(*(f(".") for _ in range(10)))

This project is still work in progress for now, so feel free to get in touch if you have any feedback or suggestions!

Acknowledgements