Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nicolaiarocci/flask-mimerender
Python module for RESTful resource representation using MIME Media-Types and the Flask Microframework
https://github.com/nicolaiarocci/flask-mimerender
Last synced: 16 days ago
JSON representation
Python module for RESTful resource representation using MIME Media-Types and the Flask Microframework
- Host: GitHub
- URL: https://github.com/nicolaiarocci/flask-mimerender
- Owner: nicolaiarocci
- Created: 2012-01-24T11:02:50.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2013-10-18T12:41:44.000Z (about 11 years ago)
- Last Synced: 2024-10-12T04:07:58.738Z (about 1 month ago)
- Language: Python
- Homepage:
- Size: 111 KB
- Stars: 70
- Watchers: 10
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## About ##
This module allows, with the use of python decorators, to transparently select a render function for an HTTP request handler's result. It uses mimeparse to parse the HTTP Accept header and select the best available representation.## Author ##
[Nicola Iarocci](mailto:[email protected])### Contributors ###
[Bruno Ripa](http://twitter.com/brunoripa)## License ##
[MIT License](http://www.opensource.org/licenses/mit-license.php)## Attribution ##
This is a Flask port from the excellent [mimerender](http://code.google.com/p/mimerender/) v0.2.3 by Martin Blech. *Update* Martin has moved to GitHub and added Flask support to the original mimerender project. He has plans for further developments, so you might want to fork/clone [his project](https://github.com/martinblech/mimerender) instead.## Usage ##
The decorated function must return a dict with the objects necessary to
render the final result to the user. The selected renderer will be called
with the map contents as keyword arguments.
If override_arg_idx isn't None, the wrapped function's positional argument
at that index will be removed and used instead of the Accept header.
override_input_key works the same way, but with the specified query string
parameter.from flask import Flask, request, jsonify
from flaskmimerender import mimerenderrender_xml = lambda message: '%s' % message
render_json = jsonify
render_html = lambda message: '%s' % message
render_txt = lambda message: messageapp = Flask(__name__)
@app.route('/')
@mimerender(
default = 'html',
html = render_html,
xml = render_xml,
json = render_json,
txt = render_txt
)
def index():
if request.method == 'GET':
return {'message': 'Hello, World!'}if __name__ == "__main__":
app.run(debug=True)Then you can do:
$ curl -H "Accept: application/html" localhost:5000/
Hello, World!$ curl -H "Accept: application/xml" localhost:5000/
Hello, World!$ curl -H "Accept: application/json" localhost:5000/
{'message':'Hello, World!'}$ curl -H "Accept: text/plain" localhost:5000/
Hello, World!## Installation ##
Flask-MimeRender is on the official Python Package Index (PyPI). All you have to do ispip install flask-mimerender
and you're good to go.