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.
Practice now: Test your Python skills with interactive challenges
Flask-Sijax
Installation
The installation of the Flask-Sijax is simple.
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.
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
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
@app.route('/url', methods = ['GET', 'POST'])
or use the @flash_sijax.route auxiliary decorator such as:
@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.
def say_hi(obj_response):
obj_response.alert('Hi there!')
When an Ajax request is detected, Sijax handles it like this:
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:
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)
Practice now: Test your Python skills with interactive challenges