Skip to main content

PuePy integration for Django

Project description

Django PuePy Integration

Provides PuePy integration for Django.

Installation

  1. Install the package using pip:
pip install django_puepy
  1. Add django_puepy to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    "django_puepy",
]

Usage

Take a look at the example_app directory for a very simple Django app that makes use of django_puepy. There are just two files:

  • example_app/backend_server.py: Simple one-file Django app that shows a server-side Django controller
  • example_app/static/todo.py: The frontend PuePy code that interacts with the Django controller

Backend Usage

Our example backend app, example_app/backend_server.py, specifies a frontend file, a runtime, and three ajax methods that are called by the frontend.

class TodoView(PuePyView):
    frontend_url = "/static/todo.py"
    runtime = "mpy"

    @PuePyView.ajax
    def add_todo(self, item):
        todo_list.append(item)
        return todo_list

    @PuePyView.ajax
    def remove_todo(self, index):
        try:
            del todo_list[index]
        except IndexError:
            pass
        return todo_list

    @PuePyView.ajax
    def get_todos(self):
        return todo_list

The frontend file, static/todo.py, is a regular PuePy app+page, but makes calls to the django_backend to interact with the ajax methods specified above:

from puepy import Application, Page, t

app = Application()
from django_backend import backend


@app.page()
class TodoPage(Page):
    def initial(self):
        return {"todos": [], "todo_add": "", "loading": True}

    def populate(self):
        t.h1("Todo List")
        if self.state["loading"]:
            t.p("Loading...")
        with t.ul(ref="todos"):
            for i, todo_item in enumerate(self.state["todos"]):
                t.li(
                    todo_item,
                    t.button(
                        "X", on_click=self.on_remove_todo_click, data_index=str(i)
                    ),
                )
        t.hr()
        with t.form(on_submit=self.on_add_form_submit, ref="form"):
            t.input(type="text", bind="todo_add")
            t.button("Add Todo")

    def on_ready(self):
        self.add_event_listener("load-data", self.on_load_data)
        self.trigger_event("load-data")

    async def on_load_data(self, event):
        with self.state.mutate():
            self.state["loading"] = True
            try:
                self.state["todos"] = await backend.get_todos()
            finally:
                self.state["loading"] = False

    async def on_add_form_submit(self, event):
        event.preventDefault()
        with self.state.mutate():
            self.state["loading"] = True
            try:
                self.state["todos"] = await backend.add_todo(self.state["todo_add"])
            finally:
                self.state["loading"] = False

    async def on_remove_todo_click(self, event):
        index = int(event.target.getAttribute("data-index"))
        with self.state.mutate():
            self.state["loading"] = True
            try:
                self.state["todos"] = await backend.remove_todo(index)
            finally:
                self.state["loading"] = False


app.mount("#app")

The result? A demo todo app.

Project status

While PuePy is approach relative stability, this Django integration is currently only an experiment. It is not recommended for production use at all and will hopefully evolve over time.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_puepy-0.1.2.tar.gz (39.5 kB view details)

Uploaded Source

Built Distribution

django_puepy-0.1.2-py3-none-any.whl (40.1 kB view details)

Uploaded Python 3

File details

Details for the file django_puepy-0.1.2.tar.gz.

File metadata

  • Download URL: django_puepy-0.1.2.tar.gz
  • Upload date:
  • Size: 39.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for django_puepy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 42bfe9fbccf007224b275a9fcd379661b7990ebc6ffa7a4cc1a8e4cbae515531
MD5 75efb1439087591ba5a28244484c0c1a
BLAKE2b-256 f5a42df27d9166f55f1334e1feb27dab765855103ee8290f8f3cbf11249af4ce

See more details on using hashes here.

File details

Details for the file django_puepy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: django_puepy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 40.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for django_puepy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9e8dd3125b6bdca572a0cbcdf5c5737f68c095986d3fa9209308746bce6de633
MD5 7e0fab106ba0ebb81b53bacd153a4fb8
BLAKE2b-256 9a8253d4f9d816e8e8d90d76d4bd0639afa25d38f57753c8358d52b51abc8a41

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page