django-turbojs


Namedjango-turbojs JSON
Version 0.5.3 PyPI version JSON
download
home_pageNone
SummaryIntegrate Hotwire Turbo with Django allowing for a Python-driven dynamic web experience.
upload_time2024-04-22 17:39:44
maintainerNone
docs_urlNone
authorNikita Marchant
requires_python<4.0,>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fhotwire-django%2Fdjango-turbojs%2Fbadge%3Fref%3Dmain&style=flat)](https://actions-badge.atrox.dev/hotwire-django/django-turbojs/goto?ref=main)
[![Documentation Status](https://readthedocs.org/projects/django-turbojs/badge/?version=latest)](https://django-turbojs.readthedocs.io/en/latest/?badge=latest)
[![Issues](https://img.shields.io/github/issues/hotwire-django/django-turbojs)](https://img.shields.io/github/issues/hotwire-django/django-turbojs)
[![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Ftwitter.com%2FDjangoHotwire)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fhotwire-django%2Fdjango-turbojs)

# Hotwired Turbo for Django

Integrate [Hotwire Turbo](https://turbo.hotwired.dev/) with Django with ease. This project is fork of [turbo-django](https://github.com/hotwire-django/turbo-django)


## Requirements

- Python 3.8+
- Django 3.1+
- Channels 3.0+ _(Optional for Turbo Frames, but needed for Turbo Stream support)_

## Installation

Turbo Django is available on PyPI - to install it, just run:

    pip install django-turbojs

Add `turbo` and `channels` to `INSTALLED_APPS`, and copy the following `CHANNEL_LAYERS` setting:

```python
INSTALLED_APPS = [
    ...
    'turbo',
    'channels'
    ...
]

CHANNEL_LAYERS = {
    "default": {
        # You will need to `pip install channels_redis` and configure a redis instance.
        # Using InMemoryChannelLayer will not work as the memory is not shared between threads.
        # See https://channels.readthedocs.io/en/latest/topics/channel_layers.html
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    }
}

```

And collect static files if the development server is not hosting them:

```sh
./manage.py collectstatic
```

_Note: Both Hotwire and this library are still in beta development and may introduce breaking API changes between releases.  It is advised to pin the library to a specific version during install._

## Quickstart
Want to see Hotwire in action? Here's a simple broadcast that can be setup in less than a minute.

**The basics:**

* A Turbo Stream class is declared in python.

* A template subscribes to the Turbo Stream.

* HTML is be pushed to all subscribed pages which replaces the content of specified HTML p tag.


### Example

First, in a django app called `quickstart`, declare `BroadcastStream` in a file named `streams.py`.

```python
# streams.py

import turbo

class BroadcastStream(turbo.Stream):
    pass

```

Then, create a template that subscribes to the stream.

```python
from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('quickstart/', TemplateView.as_view(template_name='broadcast_example.html'))
]
```

```html
# broadcast_example.html

{% load turbo_streams %}
<!DOCTYPE html>
<html lang="en">
<head>
    {% include "turbo/head.html" %}
</head>
<body>
    {% turbo_subscribe 'quickstart:BroadcastStream' %}

    <p id="broadcast_box">Placeholder for broadcast</p>
</body>
</html>
```

Now run ``./manage.py shell``.  Import the Turbo Stream and tell the stream to take the current timestamp and ``update`` the element with id `broadcast_box` on all subscribed pages.

```python
from quickstart.streams import BroadcastStream
from datetime import datetime

BroadcastStream().update(text=f"The date and time is now: {datetime.now()}", id="broadcast_box")
```

With the `quickstart/` path open in a browser window, watch as the broadcast pushes messages to the page.

Now change `.update()` to `.append()` and resend the broadcast a few times.  Notice you do not have to reload the page to get this modified behavior.

Excited to learn more?  Be sure to walk through the [tutorial](https://django-turbojs.readthedocs.io/en/latest/index.html) and read more about what Turbo can do for you.

## Documentation
Read the [full documentation](https://django-turbojs.readthedocs.io/en/latest/index.html) at readthedocs.io.


## Contribute

Discussions about a Django/Hotwire integration are happening on the [Hotwire forum](https://discuss.hotwired.dev/t/django-backend-support-for-hotwire/1570). And on Slack, which you can join by [clicking here!](https://join.slack.com/t/pragmaticmindsgruppe/shared_invite/zt-kl0e0plt-uXGQ1PUt5yRohLNYcVvhhQ)

As this new magic is discovered, you can expect to see a few repositories with experiments and demos appear in [@hotwire-django](https://github.com/hotwire-django). If you too are experimenting, we encourage you to ask for write access to the GitHub organization and to publish your work in a @hotwire-django repository.


## License

django-turbojs is released under the [MIT License](https://opensource.org/licenses/MIT) to keep compatibility with the Hotwire project.

If you submit a pull request. Remember to add yourself to `CONTRIBUTORS.md`!

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-turbojs",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Nikita Marchant",
    "author_email": "C4ptainCrunch@github-username.x",
    "download_url": "https://files.pythonhosted.org/packages/69/71/d8fc58b7141ba770c85056be02e335b4f48cb91ed45b8d1a0dd1b486149f/django_turbojs-0.5.3.tar.gz",
    "platform": null,
    "description": "[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fhotwire-django%2Fdjango-turbojs%2Fbadge%3Fref%3Dmain&style=flat)](https://actions-badge.atrox.dev/hotwire-django/django-turbojs/goto?ref=main)\n[![Documentation Status](https://readthedocs.org/projects/django-turbojs/badge/?version=latest)](https://django-turbojs.readthedocs.io/en/latest/?badge=latest)\n[![Issues](https://img.shields.io/github/issues/hotwire-django/django-turbojs)](https://img.shields.io/github/issues/hotwire-django/django-turbojs)\n[![Twitter](https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Ftwitter.com%2FDjangoHotwire)](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fhotwire-django%2Fdjango-turbojs)\n\n# Hotwired Turbo for Django\n\nIntegrate [Hotwire Turbo](https://turbo.hotwired.dev/) with Django with ease. This project is fork of [turbo-django](https://github.com/hotwire-django/turbo-django)\n\n\n## Requirements\n\n- Python 3.8+\n- Django 3.1+\n- Channels 3.0+ _(Optional for Turbo Frames, but needed for Turbo Stream support)_\n\n## Installation\n\nTurbo Django is available on PyPI - to install it, just run:\n\n    pip install django-turbojs\n\nAdd `turbo` and `channels` to `INSTALLED_APPS`, and copy the following `CHANNEL_LAYERS` setting:\n\n```python\nINSTALLED_APPS = [\n    ...\n    'turbo',\n    'channels'\n    ...\n]\n\nCHANNEL_LAYERS = {\n    \"default\": {\n        # You will need to `pip install channels_redis` and configure a redis instance.\n        # Using InMemoryChannelLayer will not work as the memory is not shared between threads.\n        # See https://channels.readthedocs.io/en/latest/topics/channel_layers.html\n        \"BACKEND\": \"channels_redis.core.RedisChannelLayer\",\n        \"CONFIG\": {\n            \"hosts\": [(\"127.0.0.1\", 6379)],\n        },\n    }\n}\n\n```\n\nAnd collect static files if the development server is not hosting them:\n\n```sh\n./manage.py collectstatic\n```\n\n_Note: Both Hotwire and this library are still in beta development and may introduce breaking API changes between releases.  It is advised to pin the library to a specific version during install._\n\n## Quickstart\nWant to see Hotwire in action? Here's a simple broadcast that can be setup in less than a minute.\n\n**The basics:**\n\n* A Turbo Stream class is declared in python.\n\n* A template subscribes to the Turbo Stream.\n\n* HTML is be pushed to all subscribed pages which replaces the content of specified HTML p tag.\n\n\n### Example\n\nFirst, in a django app called `quickstart`, declare `BroadcastStream` in a file named `streams.py`.\n\n```python\n# streams.py\n\nimport turbo\n\nclass BroadcastStream(turbo.Stream):\n    pass\n\n```\n\nThen, create a template that subscribes to the stream.\n\n```python\nfrom django.urls import path\nfrom django.views.generic import TemplateView\n\nurlpatterns = [\n    path('quickstart/', TemplateView.as_view(template_name='broadcast_example.html'))\n]\n```\n\n```html\n# broadcast_example.html\n\n{% load turbo_streams %}\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    {% include \"turbo/head.html\" %}\n</head>\n<body>\n    {% turbo_subscribe 'quickstart:BroadcastStream' %}\n\n    <p id=\"broadcast_box\">Placeholder for broadcast</p>\n</body>\n</html>\n```\n\nNow run ``./manage.py shell``.  Import the Turbo Stream and tell the stream to take the current timestamp and ``update`` the element with id `broadcast_box` on all subscribed pages.\n\n```python\nfrom quickstart.streams import BroadcastStream\nfrom datetime import datetime\n\nBroadcastStream().update(text=f\"The date and time is now: {datetime.now()}\", id=\"broadcast_box\")\n```\n\nWith the `quickstart/` path open in a browser window, watch as the broadcast pushes messages to the page.\n\nNow change `.update()` to `.append()` and resend the broadcast a few times.  Notice you do not have to reload the page to get this modified behavior.\n\nExcited to learn more?  Be sure to walk through the [tutorial](https://django-turbojs.readthedocs.io/en/latest/index.html) and read more about what Turbo can do for you.\n\n## Documentation\nRead the [full documentation](https://django-turbojs.readthedocs.io/en/latest/index.html) at readthedocs.io.\n\n\n## Contribute\n\nDiscussions about a Django/Hotwire integration are happening on the [Hotwire forum](https://discuss.hotwired.dev/t/django-backend-support-for-hotwire/1570). And on Slack, which you can join by [clicking here!](https://join.slack.com/t/pragmaticmindsgruppe/shared_invite/zt-kl0e0plt-uXGQ1PUt5yRohLNYcVvhhQ)\n\nAs this new magic is discovered, you can expect to see a few repositories with experiments and demos appear in [@hotwire-django](https://github.com/hotwire-django). If you too are experimenting, we encourage you to ask for write access to the GitHub organization and to publish your work in a @hotwire-django repository.\n\n\n## License\n\ndjango-turbojs is released under the [MIT License](https://opensource.org/licenses/MIT) to keep compatibility with the Hotwire project.\n\nIf you submit a pull request. Remember to add yourself to `CONTRIBUTORS.md`!\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Integrate Hotwire Turbo with Django allowing for a Python-driven dynamic web experience.",
    "version": "0.5.3",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d297afe12abb3d5fb52a17ee7507613f6e010b823ea6efb6f8f679d0d790e9d0",
                "md5": "49ad0608deb08fe51c81394505dbe218",
                "sha256": "5cf6db9718a1dba39f1c37b5aac27d019075faf3e002708628a059f874aae317"
            },
            "downloads": -1,
            "filename": "django_turbojs-0.5.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "49ad0608deb08fe51c81394505dbe218",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 35351,
            "upload_time": "2024-04-22T17:39:42",
            "upload_time_iso_8601": "2024-04-22T17:39:42.764800Z",
            "url": "https://files.pythonhosted.org/packages/d2/97/afe12abb3d5fb52a17ee7507613f6e010b823ea6efb6f8f679d0d790e9d0/django_turbojs-0.5.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6971d8fc58b7141ba770c85056be02e335b4f48cb91ed45b8d1a0dd1b486149f",
                "md5": "6ce30864977fe37d228df0febea4134c",
                "sha256": "fce987428eb0b1418b25239a19d01595288e2794d89344f700e13edea19db0ab"
            },
            "downloads": -1,
            "filename": "django_turbojs-0.5.3.tar.gz",
            "has_sig": false,
            "md5_digest": "6ce30864977fe37d228df0febea4134c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 33321,
            "upload_time": "2024-04-22T17:39:44",
            "upload_time_iso_8601": "2024-04-22T17:39:44.037297Z",
            "url": "https://files.pythonhosted.org/packages/69/71/d8fc58b7141ba770c85056be02e335b4f48cb91ed45b8d1a0dd1b486149f/django_turbojs-0.5.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-22 17:39:44",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "django-turbojs"
}
        
Elapsed time: 0.22485s