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
In the css and js folders you can find files, that you need to add to your HTML file.
css/BeeflowAjax.css
- declaration for classes for IDE helperjs/js-url-2.3.0.min.js
- library used byjs/BeeflowAjax.js
- beeflow ajax library including websocketjs/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()
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
beeflow-ajax-0.0.2.tar.gz
(8.2 kB
view hashes)
Built Distribution
Close
Hashes for beeflow_ajax-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 096f1ccdd5c6c968b859dea41053a178e43322871dac29a692fdbbdeeb95257a |
|
MD5 | d5e0829b4f1e6fb97f3d30c84e6e0751 |
|
BLAKE2b-256 | 321f95afb093e62f5b8a5a60d9ad6a7e82ee3aa8ad8692226837e8103dbec448 |