Skip to content

Commit

Permalink
@wq/analyst
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Aug 17, 2022
1 parent 7e5c1f8 commit 59adc95
Show file tree
Hide file tree
Showing 33 changed files with 1,877 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jobs:
package:
- pandas
- chart
- analyst
steps:
- uses: actions/checkout@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ DRP is configured by defining one or more [API views][api] and mapping them to U
3. **Data Visualization**
* [@wq/pandas]
* [@wq/chart]
* [@wq/analyst]

[Django REST Framework]: http://django-rest-framework.org
[pandas]: http://pandas.pydata.org
Expand All @@ -55,3 +56,4 @@ DRP is configured by defining one or more [API views][api] and mapping them to U
[renderers]: https://django-rest-pandas.wq.io/renderers/
[@wq/pandas]: https://django-rest-pandas.wq.io/@wq/pandas
[@wq/chart]: https://django-rest-pandas.wq.io/@wq/chart
[@wq/analyst]: https://django-rest-pandas.wq.io/@wq/analyst
19 changes: 19 additions & 0 deletions docs/@wq/analyst.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: "@wq/analyst"
module: Django REST Pandas
---

![@wq/analyst](../images/@wq/analyst.svg)

**@wq/analyst** is a [@wq/app plugin] providing interactive React tables and charts. @wq/analyst is primarily intended as a client for [Django REST Pandas].

## Installation

FIXME

## API

FIXME

[@wq/app plugin]: https://wq.io/plugins/
[Django REST Pandas]: ../index.md
79 changes: 71 additions & 8 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,65 @@ wq_config:

### URL Configuration

To use [Django REST Pandas] in your project, you will generally need to define an [API view][api] and register it with urls.py as shown below:

```python
# urls.py
from django.conf.urls import patterns, include, url

from django.urls import path
from .views import TimeSeriesView
urlpatterns = patterns('',
url(r'^data', TimeSeriesView.as_view()),

urlpatterns = (
path("data", TimeSeriesView.as_view()),
)

# This is only required to support extension-style formats (e.g. /data.csv)
# The following is required to support extension-style formats (e.g. /data.csv)
from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = format_suffix_patterns(urlpatterns)
```

When [using DRP with the wq framework][wq-setup], you can instead register a [`PandasViewSet`][PandasViewSet] subclass with [wq.db.rest.router]. wq.db provides extension-style formats by default.

```
from wq.db import rest
from .views import TimeSeriesViewSet
rest.router.add_page(
"data",
{
"url": "data",
"template": "table",
"table": {"url": "/data.csv"},
},
TimeSeriesViewSet,
)
```

### Customizing Renderers

You can override the [default renderers][renderers] by setting `PANDAS_RENDERERS` in your `settings.py`, or by overriding `renderer_classes` in your individual view(s). `PANDAS_RENDERERS` is defined separately from Django REST Framework's own `DEFAULT_RENDERER_CLASSES` setting, in case you want to have DRP-enabled views intermingled with regular DRF views.
DRP provides a set of [default renderers][renderers] that you can oveeride by setting `REST_PANDAS["RENDERERS"]` in your `settings.py`, or by overriding `renderer_classes` in your individual view(s). `REST_PANDAS["RENDERERS"]` should be a list or tuple of string paths pointing to one or more Django REST Framework renderer classes.

The default `REST_PANDAS["RENDERERS"]` setting is as follows:

```python
REST_PANDAS = {
"RENDERERS": (
"rest_pandas.renderers.PandasHTMLRenderer",
"rest_pandas.renderers.PandasCSVRenderer",
"rest_pandas.renderers.PandasTextRenderer",
"rest_pandas.renderers.PandasJSONRenderer",
"rest_pandas.renderers.PandasExcelRenderer",
"rest_pandas.renderers.PandasOldExcelRenderer",
"rest_pandas.renderers.PandasPNGRenderer",
"rest_pandas.renderers.PandasSVGRenderer",
),
)
```

> When [using DRP with the wq framework][wq-setup], `"rest_pandas.renderers.PandasHTMLRenderer"` is automatically replaced with `"wq.db.rest.renderers.HTMLRenderer"` by default.

`REST_PANDAS["RENDERERS"]` is similar to Django REST Framework's own `DEFAULT_RENDERER_CLASSES` setting, but defined separately in case you plan to have DRP-enabled views intermingled with regular DRF views. That said, it is also possible to include DRP renderers in `DEFAULT_RENDERER_CLASSES`. To do so, extend `PandasMixin` in you view or set `Meta.list_serializer_class` explicitly on your serializer. Otherwise, you may get an error saying the serializer output is not a `DataFrame`.

You can also include DRP renderers in `DEFAULT_RENDERER_CLASSES`. In that case, extend `PandasMixin` or set `list_serializer_class` on your serializer. Otherwise, you may get an error saying the serializer output is not a `DataFrame`. In short, there are three paths to getting DRP renderers working with your views:
In short, there are three paths to getting DRP renderers working with your views:

1. Extend [PandasView], [PandasSimpleView], or [PandasViewSet], and use the `PANDAS_RENDERERS` setting (which defaults to the list above).
2. Extend [PandasMixin] and customize `REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES']` to add one or more `rest_pandas` renderers.
Expand All @@ -45,6 +85,20 @@ class TimeSeriesView(PandasMixin, ListAPIView):
...
```

### Field Labels

By default, Django REST Pandas will update the dataframe columns to use the `label` attribute of defined serializer fields, and/or the `verbose_name` attribute of any underlying model fields. To disable this functionality (and use the field names as column names), set `REST_PANDAS["APPLY_FIELD_LABELS"]` to false.

```python
REST_PANDAS = {
"APPLY_FIELD_LABELS": True, # Default in DRP 2.0
}

REST_PANDAS = {
"APPLY_FIELD_LABELS": False, # Compatible with DRP 1.x
}
```

### Date Formatting

By default, Django REST Framework will serialize dates as strings before they are processed by the renderer classes. In many cases, you may want to preserve the dates as `datetime` objects and let Pandas handle the rendering. To do this, define an explicit [DateTimeField] or [DateField] on your DRF serializer and set `format=None`:
Expand All @@ -65,9 +119,18 @@ Alternately, you can disable date serialization globally by setting `DATETIME_FO
DATE_FORMAT = None
```

[Django REST Pandas]: ./index.md
[renderers]: ./renderers/index.md
[wq-setup]: ./guides/integrate-with-wq-framework.md
[api]: ./api/index.md
[PandasViewSet]: ./api/PandasViewSet.md
[PandasView]: ./api/PandasView.md
[PandasSimpleView]: ./api/PandasSimpleView.md
[PandasMixin]: ./api/PandasMixin.md

[#32]: https://github.com/wq/django-rest-pandas/issues/32
[#36]: https://github.com/wq/django-rest-pandas/issues/36

[wq.db.rest.router]: https://wq.io/wq.db/router
[DateField]: http://www.django-rest-framework.org/api-guide/fields/#datefield
[DateTimeField]: http://www.django-rest-framework.org/api-guide/fields/#datetimefield

12 changes: 12 additions & 0 deletions docs/guides/integrate-with-wq-framework.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
icon: pin
---

# How To: Integrate with the wq Framework

Django REST Pandas has built-in support for integration with the [wq framework].

FIXME


[wq framework]: https://wq.io/
1 change: 1 addition & 0 deletions docs/images/@wq/analyst.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Django REST Pandas is distributed on the Python package index. (Also see the li

Module | Github | PyPI | npm | Description
-----------------------------------------|---------------------------------|-----------------------|-----------------------|-------------
[![Django REST Pandas][drp.svg]][wq/drp] | [wq/django-rest-pandas][gh/drp] | [rest-pandas][py/drp] | [@wq/pandas][npm/drp] | Django application
[![Django REST Pandas][drp.svg]][wq/drp] | [wq/django-rest-pandas][gh/drp] | [rest-pandas][py/drp] | [@wq/analyst][npm/drp] | Django application

[drp.svg]: ../images/icons/django-rest-pandas.svg

Expand All @@ -24,4 +24,4 @@ Django REST Pandas is distributed on the Python package index. (Also see the li

[py/drp]: https://pypi.org/project/rest-pandas

[npm/drp]: https://npmjs.com/package/@wq/pandas
[npm/drp]: https://npmjs.com/package/@wq/analyst
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
testMatch: ['**/__tests__/**/*.js?(x)'],
transformIgnorePatterns: [
'/node_modules/(?!@wq|d3|internmap|delaunator|robust-predicates)',
'/node_modules/(?!@wq|redux-orm|d3|internmap|delaunator|robust-predicates)',
],
};
16 changes: 16 additions & 0 deletions packages/analyst/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[![@wq/analyst][logo]][docs]

**@wq/analyst** is a @wq/app plugin providing interactive React tables and charts. @wq/analyst is primarily intended as a client for [Django REST Pandas].

### [Documentation][docs]

[**Installation**][installation]
•
[**API**][api]

[logo]: https://django-rest-pandas.wq.io/images/@wq/analyst.svg
[docs]: https://django-rest-pandas.wq.io/@wq/analyst
[installation]: https://django-rest-pandas.wq.io/@wq/analyst#installation
[api]: https://django-rest-pandas.wq.io/@wq/analyst#api

[Django REST Pandas]: https://django-rest-pandas.wq.io/
Loading

0 comments on commit 59adc95

Please sign in to comment.