Sijax stands for ‘Simple Ajax’, a Python/jQuery library designed to help you easily introduce Ajax to your application.It uses jQuery.ajax to issue AJAX requests.

In short: Sijax is a Python/jQuery library that makes AJAX easy to use in web applications.

Related course: Python Flask: Create Web Apps with Flask

Flask-Sijax

Installation

The installation of the Flask-Sijax is simple.

1
pip install flask-sijax

Configure SIJAX_STATIC_PATH: Static path to the Sijax javascript file to be mirrored.The default location is static/js/simax.

1
2
path = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app.config['SIJAX_STATIC_PATH'] = path

In this folder, keep the sijax.js and json2.js files.

SIJAX_JSON_URI-URI from which to load json2.js static files

1
app.config['SIJAX_JSON_URI'] = '/static/js/sijax/json2.js'

Sijax uses JSON to pass data between the browser and the server.This means that the browser requires native support JSON or JSON support is supported from the json2.js file.

Functions that are registered in this way cannot provide Sijax functionality because they cannot be accessed using the POST method by default (and Sijax uses POST requests).

View

To enable the View function to process Sijax requests, use

1
@app.route('/url', methods = ['GET', 'POST'])

or use the @flash_sijax.route auxiliary decorator such as:

1
@flask_sijax.route(app, '/hello')

Each Sijax processing function (like this) automatically receives at least one parameter, just as Python passes’self’ to the object method.The ‘obj_response’ parameter is the way the function replies to the browser.

1
2
def say_hi(obj_response):
obj_response.alert('Hi there!')

When an Ajax request is detected, Sijax handles it like this:

1
2
g.sijax.register_callback('say_hi', say_hi)
return g.sijax.process_request()

Example

The Sijax application code for the Sijax application is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
from flask import Flask, g
from flask_sijax import sijax

path = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax/')
app = Flask(__name__)

app.config['SIJAX_STATIC_PATH'] = path
app.config['SIJAX_JSON_URI'] = '/static/js/sijax/json2.js'
flask_sijax.Sijax(app)

@app.route('/')
def index():
return 'Index'

@flask_sijax.route(app, '/hello')
def hello():
def say_hi(obj_response):
obj_response.alert('Hi there!')
if g.sijax.is_sijax_request:
# Sijax request detected - let Sijax handle it
g.sijax.register_callback('say_hi', say_hi)
return g.sijax.process_request()
return _render_template('sijaxexample.html')

if __name__ == '__main__':
app.run(debug = True)

Related course: Python Flask: Create Web Apps with Flask