Skip to content

Commit ebaeafd

Browse files
authored
document PandasHTMLRenderer (see #2) [skip ci]
1 parent 1955c6d commit ebaeafd

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The HTTP header and format parameter are enabled by default on every pandas view
5353

5454
Format | Content Type | pandas DataFrame Function | Notes
5555
-------|--------------|---------------------------|--------------
56+
HTML | `text/html` | `to_html()` | See notes on [HTML output](#html-output)
5657
CSV | `text/csv` | `to_csv()` |  
5758
TXT | `text/plain` | `to_csv()` | Useful for testing, as most browsers will download a CSV file instead of displaying it
5859
JSON | `application/json` | `to_json()` | [`date_format` and `orient`][to_json] can be provided in URL (e.g. `/path.json?orient=columns`)
@@ -76,7 +77,7 @@ pip install rest-pandas
7677

7778
**NOTE:** Django REST Pandas relies on pandas, which itself relies on NumPy and other scientific Python libraries written in C. This is usually fine, since pip can use Python Wheels to install precompiled versions. If you are having trouble installing DRP due to dependency issues, you may need to pre-install pandas using apt or conda.
7879

79-
### Usage Example
80+
### Usage Examples
8081

8182
#### No Model
8283

@@ -257,6 +258,26 @@ Alternately, you can disable date serialization globally by setting `DATETIME_FO
257258
DATE_FORMAT = None
258259
```
259260

261+
#### HTML Output
262+
263+
The HTML renderer provides the ability to create an interactive view that shares the same URL as your data API. The dataframe is processed by `to_html()`, then passed to [TemplateHTMLRenderer] with the following context:
264+
265+
context variable | description
266+
-----------------|------------------
267+
`table` | Output `<table>` from `to_html()`
268+
`name` | View name
269+
`description` | View description
270+
`url` | Current URL Path (without parameters)
271+
`url_params` | URL parameters
272+
`available_formats` | Array of allowed extensions (e.g. `'csv'`, `'json'`, `'xlsx'`)
273+
`wq_chart_type` | Recommended chart type (for use with [wq/chartapp.js], see below)
274+
275+
As with `TemplateHTMLRenderer`, the template name is controlled by the view. If you are using DRP together with the [wq framework], you can leverage the default [mustache/rest_pandas.html] template, which is designed for use with the [wq/chartapp.js] plugin. Otherwise, you will probably want to provide a custom template and/or set `template_name` on the view.
276+
277+
If you need to do a lot of customization, and/or you don't really need the entire dataframe rendered in a `<table>`, you can always create another view for the interface and make the `PandasView` only handle the API.
278+
279+
> Note: For backwards compatibility, `PandasHTMLRenderer` is only included in the default `PANDAS_RENDERERS` if `rest_pandas` is listed in your installed apps.
280+
260281
## Building Interactive Charts
261282

262283
In addition to use as a data export tool, DRP is well-suited for creating data API backends for interactive charts. In particular, DRP can be used with [d3.js], [wq/pandas.js], and [wq/chart.js], to create interactive time series, scatter, and box plot charts - as well as any of the infinite other charting possibilities d3.js provides.
@@ -283,7 +304,7 @@ function render(error, data) {
283304
});
284305
```
285306

286-
DRP includes three custom serializers with `transform_dataframe()` functions that address common use cases. These serializer classes can be leveraged by assigning them to `pandas_serializer_class` on your view.
307+
DRP includes three custom serializers with `transform_dataframe()` functions that address common use cases. These serializer classes can be leveraged by assigning them to `pandas_serializer_class` on your view. If you are using the [wq framework], these serializers can automatically leverage DRP's default [HTML template](#html-output) together with [wq/chartapp.js] to provide interactive charts. If you are not using the full wq framework, you can still use [wq/pandas.js] and [wq/chart.js] directly with the CSV output of these serializers.
287308

288309
For documentation purposes, the examples below assume the following dataset:
289310

@@ -520,10 +541,10 @@ pandas.get('/data/boxplot.csv?group=year', function(data) {
520541
[Django REST Framework]: http://django-rest-framework.org
521542
[pandas]: http://pandas.pydata.org
522543
[d3.js]: http://d3js.org
523-
[wq.app]: http://wq.io/wq.app
524-
[wq/chart.js]: http://wq.io/docs/chart-js
525-
[wq.db]: http://wq.io/wq.db
526-
[chart]: http://wq.io/docs/chart
544+
[wq.app]: https://wq.io/wq.app
545+
[wq/chart.js]: https://wq.io/docs/chart-js
546+
[wq.db]: https://wq.io/wq.db
547+
[chart]: https://wq.io/docs/chart
527548
[climata-viewer]: http://climata.houstoneng.net
528549
[Django Pandas]: https://github.com/chrisdev/django-pandas/
529550
[bokeh]: http://bokeh.pydata.org/
@@ -538,7 +559,11 @@ pandas.get('/data/boxplot.csv?group=year', function(data) {
538559
[DateTimeField]: http://www.django-rest-framework.org/api-guide/fields/#datetimefield
539560
[serializers]: https://github.com/wq/django-rest-pandas/blob/master/rest_pandas/serializers.py
540561
[renderers]: https://github.com/wq/django-rest-pandas/blob/master/rest_pandas/renderers.py
541-
[wq/pandas.js]: http://wq.io/docs/pandas-js
562+
[TemplateHTMLRenderer]: http://www.django-rest-framework.org/api-guide/renderers/#templatehtmlrenderer
563+
[wq framework]: https://wq.io/
564+
[wq/chartapp.js]: https://wq.io/docs/chartapp-js
565+
[wq/pandas.js]: https://wq.io/docs/pandas-js
566+
[mustache/rest_pandas.html]: https://github.com/wq/django-rest-pandas/blob/master/rest_pandas/mustache/rest_pandas.html
542567
[to_json]: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_json.html
543568
[unstacks]: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.unstack.html
544569
[boxplot_stats]: http://matplotlib.org/api/cbook_api.html#matplotlib.cbook.boxplot_stats

0 commit comments

Comments
 (0)