Learn Python with Talk Python's 270 hours of courses

PuePy: Reactive frontend framework in Python

Episode #469, published Mon, Jul 8, 2024, recorded Thu, Jun 20, 2024

Python is one of the most popular languages of the current era. It dominates data science, it an incredible choice for web development, and its many people's first language. But it's not super great on front-end programing, is it? Frameworks like React, Vue and other JavaScript frameworks rule the browser and few other languages even get a chance to play there. But with pyscript, which I've covered several times on this show, we have the possibility of Python on the front end. Yet it's not really a front end framework, just a runtime in the browser. That's why I'm excited to have Ken Kinder on the podcast to talk about his project PuePy, a reactive frontend framework in Python.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

Episode Deep Dive

Guest Background

Ken Kinder is a long-time Python developer with over two decades of experience. He’s worked extensively on line-of-business SaaS applications and has deep roots in Python-based back-end development. Seeking a way to use Python on the client side, Ken created PuePy, a reactive front-end framework for Python that builds upon the work of the PyScript team.

1. Motivation for a Python Front-End Framework

  • State of front-end development: JavaScript frameworks like React and Vue dominate the browser, requiring developers to learn separate tech stacks for client-side applications.
  • PyScript: An initiative that allows Python to run in the browser (via WebAssembly) using either Pyodide (full CPython) or MicroPython (minimal Python).
  • Ken’s goal: Combine Python’s simplicity and PyScript’s browser runtime with a reactive model akin to Vue.js to write interactive user interfaces in Python without JavaScript.

2. What is PuePy?

  • Overview: PuePy aims to replicate the reactive, component-based style of Vue.js—only entirely in Python.

  • Approach to UI:

    • Uses Python context managers to nest DOM elements (no templating system by default).
    • Follows a reactive pattern where changes to app “state” automatically propagate to the displayed elements.
  • Documentation and Tutorials: Main docs at puepy.dev and docs.puepy.dev. There are interactive tutorials and examples (such as counters, input forms, and multi-page demos).

3. Underpinnings: PyScript and WebAssembly

  • PyScript:

    • Supports Pyodide (for near-complete CPython in the browser) and MicroPython (for smaller downloads).
    • Allows installing Python wheels client-side (e.g., PuePy’s .whl file).
  • Performance considerations:

    • Using MicroPython results in smaller bundles and faster load times but excludes some Python standard library features.
  • Pyodide offers broader library support (e.g., data classes, more comprehensive re support) at the expense of larger download sizes.

4. Reactivity and the “Populate” Pattern

  • DOM creation:

    • PuePy uses Python context managers (with div(...) as d:) to define nested HTML elements.
    • This approach lets developers use regular Python flow control (loops, conditionals) when constructing the UI.
  • Reactivity:

    • State changes automatically trigger re-draw logic for the relevant DOM elements (similar to Vue).
  • “Refs” can be applied to elements to prevent them from losing focus or being re-created entirely on state changes.

5. Routing and Single-Page Apps

  • SPA behavior:

    • PuePy includes a basic router to map URLs (including parameters) to page components.
    • Developers can opt for “hash routing” (no server changes needed) or “HTML5 history” mode (server must serve the same SPA page for all routes).
  • Comparison with Django/Flask:

    • Ken suggests hooking PuePy to back-end Python frameworks by exposing APIs, though out-of-the-box you can run PuePy purely as static files if you do not need server-rendered content.

6. Use Cases and Current Limitations

  • Ideal for:

    • Internal tools, dynamic forms, quick prototypes, or simpler front-end applications where Python-based interactivity is desired.
    • Data visualization or content that benefits from real Python libraries in the browser (especially with Pyodide).
  • Potential challenges:

    • Browser-based debugging for Python is more limited than with JavaScript (mostly tracebacks and print).
    • Smaller community around front-end Python, so enterprise adoption might need additional caution or forking.
    • PuePy is a newer open-source project with one main contributor, so features and stability are evolving.

7. Progressive Web Apps (PWAs) and Other Possibilities

  • PWAs:

    • Discussed how PyScript-based apps can still be installed as Progressive Web Apps (especially with browser “add shortcut” / “add to home screen”).
    • Future plans: Ken mentioned improving direct PWA support and providing docs for integrating with frameworks like Django.
  • Electron:

    • Technically possible to ship PuePy inside Electron (since it’s just HTML and JavaScript APIs under the hood), but that would require some bridging between JavaScript-specific Electron features and Python code in PyScript.

8. Other Interesting Mentions

  • PGQueuer:
    • A simpler alternative to Celery-like task queues if you already use PostgreSQL.
    • Can handle basic job queuing needs by leveraging the database without spinning up a separate broker system.

Relevant Links and Resources Mentioned


Overall Takeaway

PuePy demonstrates that running Python in the browser with a reactive, Vue-like paradigm is both possible and surprisingly efficient. While still early-stage, it shows real promise for Python developers who want to stay in a Pythonic ecosystem when building front-end applications. Whether for quick internal tools, smaller interactive apps, or educational explorations, PuePy and PyScript offer a fresh alternative to the typical JavaScript-dominated front end.

Links from the show

Michael's Code in a Castle Course: talkpython.fm/castle

Ken Kinder: @bouncing@twit.social
PuePy: puepy.dev
PuePy Docs: docs.puepy.dev
PuePy on Github: github.com
pyscript: pyscript.net
VueJS: vuejs.org
Hello World example: docs.puepy.dev
Tutorial: docs.puepy.dev
Tutorial running at pyscript.com: pyscript.com
Micropython: micropython.org
Pyodide: pyodide.org
PgQueuer: github.com
Writerside: jetbrains.com

Michael's PWA pyscript app: github.com
Michael's demo of a PWA pyscript app: youtube.com
Python iOS Web App with pyscript and offline PWAs video: youtube.com
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm

--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy

Talk Python's Mastodon Michael Kennedy's Mastodon