Skip to main content

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.

Metadata

Release files for django-sse4htmx 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-sse4htmx 0.1.1
File Size Uploaded Provenance Actions
django_sse4htmx-0.1.1.tar.gz 44.0 kB Without provenance Details for django_sse4htmx-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-sse4htmx 0.1.1
File Interpreter ABI Platform Actions
django_sse4htmx-0.1.1-py3-none-any.whl Python 3 none any Details for django_sse4htmx-0.1.1-py3-none-any.whl

Total release size: 48.7 kB

Release files / django_sse4htmx-0.1.1.tar.gz

Download URL django_sse4htmx-0.1.1.tar.gz
Size 44.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2862f13247a5877a36518d9bbe0c07393904c43279a24c524f2826bf99b33dc7
BLAKE2b-256 checksum
How to use checksums
88130f65efc96af6d2bfe7b7358247ec5a91dfb078b5a3b7b65d7275ea838618
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.13

Release files / django_sse4htmx-0.1.1-py3-none-any.whl

Download URL django_sse4htmx-0.1.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b43f2836870da581c0c126a93123542ac6f25dd7bc12084aa19cacb009592078
BLAKE2b-256 checksum
How to use checksums
1de21eb4c213059d954f5f5fdb17e2c1f3697ac2c265c9798b5ddbed549ac4de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.13

Security for django-sse4htmx 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page