Skip to main content

Library to maintain ajax and websockets communication without writing complicated code in JS

Project description

BeeflowAjaxPy

The library to maintain ajax and websockets communication without writing complicated code in JS

Installation

$ pip install beeflow-ajax
$ npm i @beeflow/beeflow_ajax_js

or use cdn

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@beeflow/beeflow_ajax_js@1.0.2/css/BeeflowAjax.css"/>
<script src="https://cdn.jsdelivr.net/npm/@beeflow/beeflow_ajax_js@1.0.2/js/js-url-2.3.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@beeflow/beeflow_ajax_js@1.0.2/js/BeeflowAjax.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@beeflow/beeflow_ajax_js@1.0.2/js/BeeflowAjaxSwalMessages.js"></script>

Where

  • css/BeeflowAjax.css - declaration for classes for IDE helper
  • js/js-url-2.3.0.min.js - library used by
  • js/BeeflowAjax.js - beeflow ajax library including websocket
  • js/BeeflowAjaxSwalMessages.js - definition of messages that use sweet alerts (you need to have sweet alerts library)

Usage examples - HTML / JavaScript

Ajax

Delete button with confirmation

<a href="https://some.web.example/api/v1/record/<int:record_id>" data-method="delete"
   data-confirm="Are you sure you want to delete this record?"
   class="ajax-link" title="Delete record"
   data-callback="() => { console.log('Hello world!') }">delete</a>

Form

<form action="https://some.web.example/api/v1/record/" method="post" class="ajax-form">
    <input type="text" name="some_data">
    <input type="submit" name="submit" value="Send">
</form>

Select

<select data-ajax-datasource="https://some.web.example/api/v1/record/all" data-defaul-value="10"
        data-url-value="language_code=pl"></select>

Sending data using own script

BeeflowAjax.send("https://some.web.example/api/v1/record/", {"some": "data"}, submitButton, callbackMethod, 'POST');

WebSocket

Initialisation

BeeflowAjax.websocket.init("ws://some.web.example/api/v1/ws/<any:connectionId>", {some: "data"}, () => {
    consolr.log('Callback function')
})

Form

<form action="https://some.web.example/api/v1/record/" method="post" class="websocket-form">
    <input type="text" name="some_data">
    <input type="submit" name="submit" value="Send">
</form>

Usage examples - Python

Ajax

Django
from django.http import HttpResponse  # or any response class
from rest_framework.views import APIView
from beeflow_ajax.lib import AjaxResponse

...


class SomeView(APIView):
    ajax = AjaxResponse(HttpResponse)

    def get(self, request, id: int, *args, **kwargs):
        return self.ajax.assign("#some-html-element-id", f"Received id: {id}").response(*args, **kwargs)
FastAPI
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from starlette import status
from beeflow_ajax.lib import AjaxResponse

router = APIRouter()


@router.get("/some-view-id", status_code=status.HTTP_200_OK)
async def new_game(db: Session = Depends(get_db)):
    ajax = AjaxResponse()

    # as we didn't pass any response object to the AjaxResponse initializer, response() method will return dictionary
    return ajax.assign("#some-html-element-id", f"Received id: {id}").response()

Custom commands

You can register custom commands

BeeflowAjax.addCommandHandler(
    'customCommand', (data) => { alert(data['message']) }
)
def handle_custom_data(data: Dict[str, Any]) -> 'AjaxResponse':
    ajax = AjaxResponse()
    ajax.add_command("customCommand", data)
    return ajax


ajax = AjaxResponse()
ajax.register_command_handler("customCommand", handle_custom_data)

Development

First install pre-commit

$ pip install pre-commit
$ pre-commit install

Publishing Python

$ python -m build
$ twine upload dist/*

Publishing JS

In the beeflow_ajax/beeflow_ajax_js directory, run commands

$ npm version patch
$ npm publish

Then use these urls in the browser:

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

beeflow_ajax-1.0.18.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

beeflow_ajax-1.0.18-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file beeflow_ajax-1.0.18.tar.gz.

File metadata

  • Download URL: beeflow_ajax-1.0.18.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for beeflow_ajax-1.0.18.tar.gz
Algorithm Hash digest
SHA256 79c68dcf3412119717195bf67c4f712a6ff9ea0d5e4e359748f54b72d1818ffb
MD5 69aa832e2aed58e0fa94d6ed092d8b81
BLAKE2b-256 e1a79cb0414ea1d25766bb5a549a78f81bad3298dfc74a9fa59725146d3cc9b4

See more details on using hashes here.

File details

Details for the file beeflow_ajax-1.0.18-py3-none-any.whl.

File metadata

File hashes

Hashes for beeflow_ajax-1.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 ec982800ac959097a3e8a567cbb3ea016e343d585e170a9e559ef3185f21e432
MD5 18abc5554f351bd473b0b634b6c881b2
BLAKE2b-256 51e9dbda977a7b47d29a78ea78aa6f009e31161683d0b249740f9192d075e0ad

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