Skip to content

v0.9

Latest

Choose a tag to compare

@ogrodnek ogrodnek released this 03 Jul 23:10
· 173 commits to main since this release
75441d4

PyView 0.9

The main things in this release are: dependency injection, flash messages, and a complete JS command API.

Dependency Injection with Depends()

Inspired by FastAPI, you can now inject services, auth, etc. into your LiveView methods

from pyview import LiveView, Depends, Session

async def current_user(session: Session):
    return await User.get(session["user_id"])

class Dashboard(LiveView):
    async def mount(self, socket, user=Depends(current_user)):
        socket.context = {"user": user}

Works in __init__, mount, handle_params, handle_event, and @event handlers. Sync or async, with per-request caching so a dependency resolves once and is reused everywhere.

Note, it's not yet implemented for LiveComponents

Flash Messages

put_flash and clear_flash on the socket primitives for toasts, save confirmations, and validation errors.

Complete JS Command API

The js API now covers the full Phoenix.js command set: attribute manipulation (set_attribute, remove_attribute, toggle_attribute), toggle_class, client-side navigation (navigate, patch), focus-stack management (push_focus, pop_focus), and exec.

Breaking Change: route registration takes classes

Routes now require a LiveView class as part of the work on adding Depends support. Previously while it was normal to use a class, you could pass a factory:

routes.add("/items", lambda: ItemsView())  # 0.8.x
routes.add("/items", ItemsView)                   # 0.9.0

Fixes

  • push_event calls during handle_info are now delivered to the browser (#141, thanks @avilior!)
  • Fixed a redirect edge case during socket teardown (#159)
  • create-view now targets the right directory for uv and src-layout projects (#137)
  • None renders as an empty string in t-string templates, not the literal "None"
  • Socket teardown cleanup for more reliable disconnects (#147)