Skip to content

m-butterfield/django_webassembly

Repository files navigation

Django Webassembly

Running Django in the browser.

Live example hosted directly from this repo on Github Pages. - Give it some time to load. There is around 15MB to download and lots of code to run.

What is happening?

With webassembly, it is possible to run Python code in the browser using Pyodide. With service workers, we are able to intercept all http requests made by the browser, modify them, pass them through without doing anything, or create and return a new response of our own without ever actually calling out to the internet. Putting these concepts together, and heavily inspired by a similar approach with wordpress recently, I was able to get a very basic Django application and the Django admin running entirely in the browser.

What does this mean?

It means you can host a Django application using only static files, like the example linked above using Github Pages. Of course, there's no shared database, just a local SQLite db that lives in your browser. Perhaps that could also be staticly hosted and shared somewhere... A more practical use case could be to make a Django app work offline, sending updates back to the server when the connection is restored.

How does this work?

This repo contains the django_webassembly app. This can can be run like any other django app (python manage.py runserver), or you can simply host the static files. The easiest way to do this is to run python -m http.server and open http://localhost:8000. When running this way, the app is loaded and installed as a wheel, so you have to rebuild the wheel if you want to see your changes. You can do that by running make wheel. The service worker also needs to be reloaded.

The details of how this works can be seen in app.js, which loads the service worker, and worker.js which contains the service worker code. The service worker loads Pyodide, installs Python dependencies and the django_webassembly app, then runs some setup code in init.py. When this is done, the page reloads and all further requests are intercepted by the service worker. Each request is turned into Python code that calls an app client for django_webassembly, and the return value is converted back into a Javascript response that is returned to the browser.

What happens now?

I've always liked the idea of running Python in the browser, and I really just did this as a proof of concept out of curiousity. It's kind of finicky to use and develop so far, and I haven't done much testing or tried to run anything complicated. That being said, I think there are some interesting experiments that can come out of this. At the very least, it can be a nice way to showcase a Django app you've written without worrying about deploying it to a running server. I'm also interested in trying to run other languages in the browser. What would it take to run a Go webserver in the browser?

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published