Skip to main content

A simple wrapper around Django EventStream's `send_event` to make it simpler to use with HTMX and django-template-partials.

Project description

Django SSE4HTMX

A simple wrapper around Django EventStream's send_event to make it simpler to send a properly formatted HTML fragment rendered from a template or a partial (if you have django-template-partials installed). It gives you a similar feel to Django's class-based views (CBVs).

Installation

Install the package using uv:

uv add django-sse4htmx

Install the package using pip:

pip install django-sse4htmx

Install the package using poetry:

poetry add django-sse4htmx

Usage

from django_sse4htmx import SSEFragmentSender


class MyEventSender(SSEFragmentSender):
    channel_name = 'my_channel'
    template_name = 'my_template.html#my_partial'

    def get_context_data(self):
        # here you can add custom context data to be passed to the template or partial
        return {
            'my_context': 'my_context_value',
            'request': self.kwargs.get('request'),
        }


# in your code anywhere such as a view, a signal responder,
# a queued task in a task queue, etc.:

MyEventSender()(value="some value")  # value is passed to the template or partial as a context variable

# if that syntax bothers you, can also use the `send` method:

MyEventSender().send(value="some value")

# Alternatively if you will use the same sender more than once:

my_sender = MyEventSender()
my_sender(value="some value")
my_sender(value="another value")
my_sender.send(value="yet another value")

Any keyword arguments passed to the __call__ or send method will be passed to the template or partial as context variables.

Gotchas

  • The current request and other values which are normally in the template context will not be there unless you pass them explicitly as keyword arguments to the __call__ or send method (as shown in the example above). The current request is only available from a view or a middleware, so you may need to adjust your templates (or partials) accordingly if your existing template code assumes their presence.

Code of Conduct

Roadmap

  • Initial release, works in quick manual testing with the sample project.
  • Add Unit Tests
  • tox
  • improve documentation
  • Support for i18n/l10n given a language code?
  • Consider adding a way to send multiple events at once (if users even want it, which could happen since HTMX supports it)
  • Clean up the demo page layout a bit. It's admittedly rushed, just to get something working quickly.

Running the Example Project

  • Install uv if you haven't already.
  • Clone the repository
  • cd into the cloned repository, then into example
  • uv sync --all-groups to install the dependencies
  • source .venv/bin/activate
  • python manage.py runserver
  • Visit http://127.0.0.1:8000/ in your browser

You can watch the JavaScript console to see debugging logs as SSE messages are received.

Contributing

  • The project is using pre-commit hooks to set code style and standards.
  • Please fork the repository, install pre-commit uv tool install pre-commit, make your changes on a new branch, then submit a Pull Request against main.

Changes

  • 0.1.1: added repo urls to project config

  • 0.1.0: Initial release 2025-01-13

License

MIT License

Copyright (c) 2024 Duna Mae Cat

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


There is still 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_sse4htmx-0.1.1.tar.gz (44.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_sse4htmx-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django_sse4htmx-0.1.1.tar.gz.

File metadata

  • Download URL: django_sse4htmx-0.1.1.tar.gz
  • Upload date:
  • Size: 44.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.13

File hashes

Hashes for django_sse4htmx-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2862f13247a5877a36518d9bbe0c07393904c43279a24c524f2826bf99b33dc7
MD5 860ab28ed50845d7f5976f2461942b2d
BLAKE2b-256 88130f65efc96af6d2bfe7b7358247ec5a91dfb078b5a3b7b65d7275ea838618

See more details on using hashes here.

File details

Details for the file django_sse4htmx-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sse4htmx-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b43f2836870da581c0c126a93123542ac6f25dd7bc12084aa19cacb009592078
MD5 ea08d6f8e1bc70484633fac60ab6f4f6
BLAKE2b-256 1de21eb4c213059d954f5f5fdb17e2c1f3697ac2c265c9798b5ddbed549ac4de

See more details on using hashes here.

Supported by

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