Skip to content

Instantly share code, notes, and snippets.

@uolter
Created November 26, 2012 09:16
Show Gist options
  • Save uolter/4147337 to your computer and use it in GitHub Desktop.
Save uolter/4147337 to your computer and use it in GitHub Desktop.
jsonp decorator for python
'''
Created on Nov 23, 2012
@author: uolter
'''
from functools import wraps
from flask import request, current_app
def jsonp(func):
"""Wraps JSONified output for JSONP requests."""
@wraps(func)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
response = func(*args, **kwargs)
data = str(response.data)
content = str(callback) + '(' + data + ')'
mimetype = 'application/javascript'
response = current_app.response_class(content, mimetype=mimetype, status=response.status_code)
return response
else:
return func(*args, **kwargs)
return decorated_function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment