Skip to main content

Convenient, but not fast

Project description

starlette-dataframe-response

Convenient, but not fast.

Resources:

Installation

$ pip install starlette-dataframe-response

Requirements

Python 3.7+

Dependencies

  • starlette
  • pandas
  • (vega_datasets)
  • (magicalimport)

Example

import pandas as pd
from starlette.requests import Request
from starlette_dataframe_response import DataFrameResponse, guess_media_type

async def get_dataset(request: Request):
    df: DataFrame = pd.read_csv("<some dataset>.csv")
    return DataFrameResponse(df, media_type=guess_media_type(request))

app = Starlette(
    debug=True,
    routes=[
        Route("/dataset/<some dataset>", get_dataset),
    ],
)

Then, supporting the request following.

# return dataset as json (orient=records)
GET /dataset/<some dataset>

# return dataset as csv
GET /dataset/<some dataset>?format=csv
# return dataset as markdown
GET /dataset/<some dataset>?format=markdown
# return dataset as html
GET /dataset/<some dataset>?format=html

If you want to customize the JSON response.

# use orient="columns"
DataFrameResponse(df, media_type=guess_media_type(request), to_json_orient="columns")

Or, if the response is created by DataFrameResponse.from_request(request, df), it is also OK.

$ use orient="columns"
GET /dataset/<some dataset>?to_json_orient=columns

python -m starlette_dataframe_response.distribute

And It also includes examples using vega-datasets.

$ python -m starlette_dataframe_response.distribute --port 8888

(with httpie)

$ http :8888/
$ http :8888/iris
$ http :8888/cars
$ http :8888/iris  format==csv
$ http :8888/iris/columns
$ http :8888/iris/describe
$ http :8888/iris/groupby/species/aggs/sepalWidth
$ http :8888/iris/groupby/species/aggs/sepalWidth fn==min fn==max fn==count fn==mean fn==std
$ http :8888/iris/groupby/species/aggs/sepalWidth,sepalLength

Or if you want to see an example using a custom data, in above included app examples/01data-provider/

Contribution

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette-dataframe-response-0.0.0.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page