GithubHelp home page GithubHelp logo

nicolaiarocci / flask-mimerender Goto Github PK

View Code? Open in Web Editor NEW
70.0 10.0 5.0 114 KB

Python module for RESTful resource representation using MIME Media-Types and the Flask Microframework

Python 100.00%

flask-mimerender's Introduction

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

Contributors

Bruno Ripa

License

MIT License

Attribution

This is a Flask port from the excellent 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 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 mimerender

render_xml = lambda message: '<message>%s</message>' % message
render_json = jsonify
render_html = lambda message: '<html><body>%s</body></html>' % message
render_txt = lambda message: message

app = 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/
<html><body>Hello, World!</body></html>

$ curl -H "Accept: application/xml" localhost:5000/
<message>Hello, World!</message>

$ 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 is

pip install flask-mimerender

and you're good to go.

flask-mimerender's People

Contributors

jfreax avatar nicolaiarocci avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-mimerender's Issues

ValueError: need more than 1 value to unpack

Hello! When I use the example.py, it fails with the following err info. My python version is 2.7.2. Any ideas? Thanks a lot!

ValueError: need more than 1 value to unpack

Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in call
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in dispatch_request
return self.view_functionsrule.endpoint
File "C:\Users\xuli\Downloads\flask-mimerender-master\src\flaskmimerender.py", line 178, in wrapper
result, status = target(_args, *_kwargs)
ValueError: need more than 1 value to unpack

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.