django-puepy


Namedjango-puepy JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummaryPuePy integration for Django
upload_time2024-08-03 18:31:24
maintainerNone
docs_urlNone
authorKen Kinder
requires_python<4.0,>=3.11
licenseApache
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Django PuePy Integration

Provides [PuePy](https://puepy.dev) integration for Django.

## Installation

1. Install the package using pip:

```
pip install django_puepy
```

2. Add `django_puepy` to your `INSTALLED_APPS`:

```python
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.

```python
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:

```python
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.




            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-puepy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ken Kinder",
    "author_email": "ken+github@kkinder.com",
    "download_url": "https://files.pythonhosted.org/packages/f5/a4/2df27d9166f55f1334e1feb27dab765855103ee8290f8f3cbf11249af4ce/django_puepy-0.1.2.tar.gz",
    "platform": null,
    "description": "# Django PuePy Integration\n\nProvides [PuePy](https://puepy.dev) integration for Django.\n\n## Installation\n\n1. Install the package using pip:\n\n```\npip install django_puepy\n```\n\n2. Add `django_puepy` to your `INSTALLED_APPS`:\n\n```python\nINSTALLED_APPS = [\n    ...\n    \"django_puepy\",\n]\n```\n\n## Usage\n\nTake a look at the `example_app` directory for a very simple Django app that makes use of django_puepy. There are just two files:\n\n- `example_app/backend_server.py`: Simple one-file Django app that shows a server-side Django controller\n- `example_app/static/todo.py`: The frontend PuePy code that interacts with the Django controller\n\n### Backend Usage\n\nOur example backend app, `example_app/backend_server.py`, specifies a frontend file, a runtime, and three ajax methods that are called by the frontend.\n\n```python\nclass TodoView(PuePyView):\n    frontend_url = \"/static/todo.py\"\n    runtime = \"mpy\"\n\n    @PuePyView.ajax\n    def add_todo(self, item):\n        todo_list.append(item)\n        return todo_list\n\n    @PuePyView.ajax\n    def remove_todo(self, index):\n        try:\n            del todo_list[index]\n        except IndexError:\n            pass\n        return todo_list\n\n    @PuePyView.ajax\n    def get_todos(self):\n        return todo_list\n```\n\nThe 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:\n\n```python\nfrom puepy import Application, Page, t\n\napp = Application()\nfrom django_backend import backend\n\n\n@app.page()\nclass TodoPage(Page):\n    def initial(self):\n        return {\"todos\": [], \"todo_add\": \"\", \"loading\": True}\n\n    def populate(self):\n        t.h1(\"Todo List\")\n        if self.state[\"loading\"]:\n            t.p(\"Loading...\")\n        with t.ul(ref=\"todos\"):\n            for i, todo_item in enumerate(self.state[\"todos\"]):\n                t.li(\n                    todo_item,\n                    t.button(\n                        \"X\", on_click=self.on_remove_todo_click, data_index=str(i)\n                    ),\n                )\n        t.hr()\n        with t.form(on_submit=self.on_add_form_submit, ref=\"form\"):\n            t.input(type=\"text\", bind=\"todo_add\")\n            t.button(\"Add Todo\")\n\n    def on_ready(self):\n        self.add_event_listener(\"load-data\", self.on_load_data)\n        self.trigger_event(\"load-data\")\n\n    async def on_load_data(self, event):\n        with self.state.mutate():\n            self.state[\"loading\"] = True\n            try:\n                self.state[\"todos\"] = await backend.get_todos()\n            finally:\n                self.state[\"loading\"] = False\n\n    async def on_add_form_submit(self, event):\n        event.preventDefault()\n        with self.state.mutate():\n            self.state[\"loading\"] = True\n            try:\n                self.state[\"todos\"] = await backend.add_todo(self.state[\"todo_add\"])\n            finally:\n                self.state[\"loading\"] = False\n\n    async def on_remove_todo_click(self, event):\n        index = int(event.target.getAttribute(\"data-index\"))\n        with self.state.mutate():\n            self.state[\"loading\"] = True\n            try:\n                self.state[\"todos\"] = await backend.remove_todo(index)\n            finally:\n                self.state[\"loading\"] = False\n\n\napp.mount(\"#app\")\n```\n\nThe result? A demo todo app.\n\n## Project status\n\nWhile 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.\n\n\n\n",
    "bugtrack_url": null,
    "license": "Apache",
    "summary": "PuePy integration for Django",
    "version": "0.1.2",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9a8253d4f9d816e8e8d90d76d4bd0639afa25d38f57753c8358d52b51abc8a41",
                "md5": "7e0fab106ba0ebb81b53bacd153a4fb8",
                "sha256": "9e8dd3125b6bdca572a0cbcdf5c5737f68c095986d3fa9209308746bce6de633"
            },
            "downloads": -1,
            "filename": "django_puepy-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7e0fab106ba0ebb81b53bacd153a4fb8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 40085,
            "upload_time": "2024-08-03T18:31:23",
            "upload_time_iso_8601": "2024-08-03T18:31:23.169600Z",
            "url": "https://files.pythonhosted.org/packages/9a/82/53d4f9d816e8e8d90d76d4bd0639afa25d38f57753c8358d52b51abc8a41/django_puepy-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f5a42df27d9166f55f1334e1feb27dab765855103ee8290f8f3cbf11249af4ce",
                "md5": "75efb1439087591ba5a28244484c0c1a",
                "sha256": "42bfe9fbccf007224b275a9fcd379661b7990ebc6ffa7a4cc1a8e4cbae515531"
            },
            "downloads": -1,
            "filename": "django_puepy-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "75efb1439087591ba5a28244484c0c1a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 39461,
            "upload_time": "2024-08-03T18:31:24",
            "upload_time_iso_8601": "2024-08-03T18:31:24.543148Z",
            "url": "https://files.pythonhosted.org/packages/f5/a4/2df27d9166f55f1334e1feb27dab765855103ee8290f8f3cbf11249af4ce/django_puepy-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-03 18:31:24",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-puepy"
}
        
Elapsed time: 0.96081s