Skip to main content

Library to bind Django models with NiceGUI elements using DRF and SSE.

Project description

django-nice

django-nice is a Python library designed to seamlessly integrate Django models with NiceGUI elements using Django REST Framework (DRF) and Server-Sent Events (SSE) for real-time synchronization. This library allows you to bind NiceGUI frontend components (such as text areas, input fields, etc.) to Django model fields, ensuring that changes to either the backend or the frontend are synchronized in real-time.

Why Use django-nice?

When working with Django and NiceGUI, binding frontend elements directly to Django models in a dynamic, real-time manner can be challenging. Out-of-the-box integrations often rely on manual updates, polling, or heavy reliance on traditional forms, which can be slow or cumbersome for modern web applications that require seamless real-time interactions.

django-nice solves these challenges by:

  1. Real-Time Sync with SSE: The library leverages Server-Sent Events (SSE) to keep the frontend NiceGUI elements in sync with the backend Django models in real-time. Whenever the backend data changes, the frontend is updated immediately without needing to refresh or manually poll.

  2. Bidirectional Data Binding: The library allows changes in the frontend to automatically update the corresponding Django model, and vice versa. This ensures consistency between the client and the server.

  3. REST API-Based Updates: It uses Django REST Framework (DRF) to expose model fields as API endpoints. This makes the process of updating the Django backend from the frontend smooth, without needing to implement complex form handling.

Advantages Over Regular Django-NiceGUI Integration:

  • Real-Time Updates: Standard Django-NiceGUI integration doesn’t automatically sync data between the frontend and backend in real-time. django-nice provides automatic updates through SSE, allowing the frontend to reflect changes as soon as they happen in the backend.
  • Effortless Binding: Instead of manually writing JavaScript, forms, or custom API calls to keep frontend elements in sync with Django models, django-nice handles this for you with minimal configuration.
  • Improved User Experience: By offering real-time data updates, the library enhances the responsiveness of your NiceGUI app, creating a smoother and more interactive user experience.

Usage:

To get started, follow these steps:

1. Configure the Base URL in settings.py:

Add the following to your settings.py file to configure the API endpoint:

from django_nice.config import Config

# Configure the base URL (host) for the API
Config.configure(host='http://your-production-server.com', api_endpoint='/api')

2. Register API and SSE Endpoints in urls.py:

In your project's urls.py file, add the necessary API and SSE endpoints:

from django_nice.config import Config

# Register API and SSE endpoints for a model (e.g., Data model in app 'myapp')
Config.add_urls_to_project(urlpatterns, app_label='myapp', model_name='Data')

3. Bind Frontend Elements to Django Models:

Inside your NiceGUI components, bind frontend elements (like text areas) to Django model fields for real-time updates:

from nicegui import ui
from django_nice.frontend import bind_element_to_model

@ui.page('/')
def index():
    textarea = ui.textarea('This is data that is bound to the django model').classes('w-full')
    bind_element_to_model(textarea, app_label='myapp', model_name='Data', pk=1, field_name='data_to_display')

ui.run()

This example shows how to bind a NiceGUI textarea element to a Django model field, ensuring real-time synchronization between the frontend and backend. When the Django model changes, the textarea is updated automatically, and any changes made in the textarea are sent to the backend immediately.

Installation:

To install the library, simply run:

pip install django-nice

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

django_nice-0.3.4.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

django_nice-0.3.4-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file django_nice-0.3.4.tar.gz.

File metadata

  • Download URL: django_nice-0.3.4.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for django_nice-0.3.4.tar.gz
Algorithm Hash digest
SHA256 3cbe7ae9070f2ea9873fb22ba9a95f13341716c9b8418120a0fadbb44a4d1b41
MD5 650d2f7ff9987e032a967b9d1a1d5915
BLAKE2b-256 b69f2cc5cb6364cb5ac77d4f8874318473e4c66e771cea73d214c6d89c919c1d

See more details on using hashes here.

File details

Details for the file django_nice-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: django_nice-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for django_nice-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2649c250d410ec460d5218c3a63eb53decf2913b0c4e8efad14fd58e593d3800
MD5 9bb41ad57e4c5c9c17662b181edb98f8
BLAKE2b-256 448efee56616cdd11aa0b92008eccf7c112b131b1bb3b53c1ad9ed8953654f33

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