Write Chrome Extensions in Python
In Python?!! That’s right, and it’s pretty painless with PyScript…
There are plenty of articles about the emergence of PyScript for embedding Python code directly into HTML, but until now the creation of browser extensions in Python has been something of a closed door.
I was lucky enough to enlist the help of PyScript’s Engineering Manager and finally worked out how to run a Python script as a Chrome Extension, displaying output in a popup box (see the screenshot above). This throws the doors wide open to all sorts of other Python/PyScript possibilities, building on the template which I’m about to share…
Create Your Extension Files
Create a new folder for your Extension and download or clone the following repository into it:
GitHub - PFython/pyscript-local-runtime
This repository provides a template for running PyScript and all its runtime dependencies locally. You'd typically want…
runtime/setup.sh (Linux/Posix) or
runtime/setup.py (Windows) to download the latest PyScript runtime files and dependencies locally.
Add custom icons to
/images and update the default icon name in
/manifest.json if required.
Add your own Python code in between the
<py-script> tags in
Change or delete
/<script src="finished.js"></script> and
/index.html as required.
Load Your Extension into Chrome
chrome://extensions in the Chrome address bar.
Toggle the “Developer Mode” switch ON (top right of the page).
Click “Load unpacked” and select your extension folder.
You should see your Extension listed something like this:
Click on the Extensions (“jigsaw”) icon in the top right of the browser and pin your Extension to the Extensions Launcher:
Click on your snazzy new Extension icon to run Python in the browser!
At the time of writing there are virtually NO helpful search results for this topic online, so I hope this article and the pyscript-local-runtime repository spare you the hours of trial-and-error I went through just to get these basic building blocks in place. You can just fast-forward to the fun part — tinkering with PyScript — and exploring the “Art of the Possible” within the constraints of browser extensions generally.
I can’t wait to see what you create… Feel free to post any examples you’d like to showcase in the comments below and don’t forget to “Star” the repository while you’re there please — it’s a great morale boost.