Configuration
Let's move on to the configuration for Flask-Sijax.
-
SIJAX_STATIC_PATH: The static directory where the Sijax javascript files should be mirrored. Static/js/sijax is the default location. The files sijax.js and json2.js are placed in this folder.
-
SIJAX_JSON_URI: The URI from which the json2.js static file should be loaded
The data is passed between the browser and the server via JSON in Sijax. This means that browsers must either support JSON natively or rely on the json2.js file for JSON support.
Because they can't be accessed using a POST method by default, functions registered can't provide Sijax functionality (and Sijax uses POST requests).
Make a View function accessible via POST using @app.route('/url', methods = ['GET', 'POST']) or use the @flask sijax.route helper decorator like this to make it capable of receiving Sijax requests:
@flask_sijax.route(app, '/hello')
Every Sijax handler function (including this one) is automatically passed at least one parameter, similar to how Python passes 'self' to object methods. The 'obj response' parameter is how the function communicates with the browser.
def say_hi(obj_response):
obj_response.alert('Hi there!')
When a Sijax request is discovered, Sijax responds as follows:
g.sijax.register_callback('say_hi', say_hi)
return g.sijax.process_request()
Sijax Application
To better understand Flask-Sijax, we will build a mini Sijax application, the code for which looks as follows:
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, '/firstApp)
def hello():
def say_hi(obj_response):
obj_response.alert('Hello! This is my first app')
if g.sijax.is_sijax_request:
# if a Sijax request us 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)
When a Sijax request (a specific jQuery.ajax() request) is sent to the server, g.sijax.is sijax request() detects the request and tells you to let Sijax handle it.
All functions registered with g.sijax.register callback() are accessible from the browser and can be called.
Sijax will execute the relevant (already registered) function and return the result to the browser when using g.sijax.process request().
FAQs
-
What is flask Sijax?
Flask-Sijax helps you add Sijax support to your Flask applications. Sijax is a Python/jQuery library that enables AJAX in web applications simple to use.
-
What kinds of applications can Flask be used for?
We can make practically any web application with Flask. Single-page applications, RESTful API-based applications, SAS applications, small to medium-sized websites, static websites, Microservices, and serverless apps are all possible.
-
Is Flask a backend?
There is just one "front end" language in web development: JavaScript (and things compiled to JavaScript, like TypeScript). Everything else is "back end" since it runs on a server rather than in the browser. Flask is a backend framework, which is built-in Python.
Key Takeaways
In this blog, we learnt about Flask Sijax, its installation, configuration, and how to build an application using Sijax.
Follow Coding Ninjas Blogs for more exciting and interesting technical articles.
Happy learning!