librelist archives

« back to archive

response based on accept headers

response based on accept headers

From:
Daniele Sluijters
Date:
2014-06-25 @ 12:20
Hi,

I’m working on an application that I would like to be able to respond in 
either JSON, CSV (because Excel still rules the world :/) or HTML 
depending on what was requested.

I ran into http://flask.pocoo.org/snippets/45/ and I was wondering if I 
should use that and extend the matching a bit or if there are better 
options to handle this, route decorators or something else?

Kind regards,

-- 
Daniele Sluijters

Re: [flask] response based on accept headers

From:
Moritz Beber
Date:
2014-06-25 @ 13:32
Hey,

I've done something like this with the value of a form select list:

VALID_FORMATS = {
    "csv": (pandas.DataFrame.to_csv, {"encoding": "utf-8", "header": True,
            "index": False, "sep": ";"}),
    "tsv": (pandas.DataFrame.to_csv, {"encoding": "utf-8", "header": True,
            "index": False, "sep": "\t"}),
    "html": (pandas.DataFrame.to_html, {"header": True,
            "index": False, "escape": False}),
    "hdf": (pandas.DataFrame.to_hdf, {"key": "Scores", "format": "fixed"}),
    "xls": (pandas.DataFrame.to_excel, {"header": True,
            "index": False, "engine": "xlwt", "sheet_name": "Scores"})
    }


you could do the same by splitting the header on "/" and use the
second part with a  dict for the appropriate return type.

HTH,
Moritz

On Wed, Jun 25, 2014 at 2:20 PM, Daniele Sluijters
<daniele.sluijters@nedap.com> wrote:
> Hi,
>
> I’m working on an application that I would like to be able to respond in
either JSON, CSV (because Excel still rules the world :/) or HTML 
depending on what was requested.
>
> I ran into http://flask.pocoo.org/snippets/45/ and I was wondering if I 
should use that and extend the matching a bit or if there are better 
options to handle this, route decorators or something else?
>
> Kind regards,
>
> --
> Daniele Sluijters
>