Gramex 1.72 introduces OpenAPI support, a root cause algorithm, charts in UIFactory, and more.
OpenAPIHandler automatically generates documentation for your APIs. Here’s what the output looks like:
To expose your entire app as an OpenAPI spec, add this to gramex.yaml
:
url:
openapi:
pattern: /$YAMLURL/docs
handler: OpenAPIHandler
When creating FunctionHandlers, Python type annotations (see below) are automatically converted into the right types on the UI, making it easy to test the app.
from gramex.transforms import handler
from typing import List
from typing_extensions import Annotated
@handler
def compare(
x: Annotated[List[float], 'First list'] = [],
y: Annotated[List[float], 'Second list'] = [],
) -> bool:
'''
Return True if the first list (x) is larger than the second list (y)
'''
return True if sum(x) > sum(y) else False
TopCause is an algorithm that answers the question What’s the single biggest change I can make to improve my outcome?.
Say a Rugby team is recruiting for heavy people, and have weight data like this.
male | age | height | weight |
---|---|---|---|
1 | 90.0 | 151.7 | 47.8 |
0 | 90.0 | 139.7 | 36.4 |
0 | 90.0 | 136.5 | 31.8 |
1 | 20.0 | 156.8 | 53.0 |
0 | 10.0 | 145.4 | 41.2 |
If they want to know What’s the single biggest driver of weight?, TopCause can answer that.
import gramex.ml
import gramex.cache
from gramex.transforms import handler
@handler
def drivers():
data = gramex.cache.open('weight.csv')
model = gramex.ml.TopCause()
model.fit(data, data['weight'])
return model.result_
This returns:
value | gain | p | type | |
---|---|---|---|---|
weight | 55.0 | 16.9 | 1.8e-267 | num |
height | 164.5 | 12.7 | 8.4e-13 | num |
male | NaN | NaN | 0.057 | num |
age | NaN | NaN | 0.453 | num |
… and it indicates that:
UIFactory now supports a <vega-chart>
component that embeds Vega charts.
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/uifactory@0.0.16"
import="https://cdn.jsdelivr.net/npm/uifactory@0.0.16/src/vega-chart.html"
></script>
<vega-chart
spec="https://vega.github.io/editor/spec/vega-lite/bar_diverging_stack_transform.vl.json"
></vega-chart>
This embeds a chart in-place:
Any attributes are passed to the chart as Vega Signals, making it possible to create dynamic charts. The Gramex Guide will soon feature a tutorial on this.
This feature is in beta, and only the core capability is released. A collection of charts will be shared in the next release, and documented.
FormHandler introduced MongoDB read support in 1.71. Now it has
MongoDB write-back support. In other words, PUT
, POST
and DELETE
methods work as expected.
In addition, FormHandler also supports nested keys. For example:
# Search for records where `{"parent": {"child": "value"}}
curl 'localhost:9988/mongodb?parent.child=value'
# Search for records where `{"date": {"year": "2020", "month": "12"}}`
curl 'localhost:9988/mongodb?date.year=2020&date.month=12'
It also supports inserting or updating nested values using the .=
operator. For example:
# To insert `{"parent": {"child": "value"}}`
curl -X POST 'localhost:9988/mongodb?parent.={"child": "value"}'
# To insert `{"date": {"year": 2020, "month": 12}}`
curl -X POST 'localhost:9988/mongodb?date.={"year": 2020, "month": 12}'
ComicHandler supports templates that combine multiple characters.
This feature is in beta, and only the core capability is released. A collection of templates will be shared in the next release, and documented.
The Gramex release process is documented.
cache.memory.size
in gramex.yaml
. Earlier, it reported that it
couldn’t cache, not why.Gramex 1.72 is backward compatible with previous releases unless the release notes say otherwise. Automated builds test this.
Every Gramex release is tested for security vulnerabilities using the following tools.
The Gramex code base has:
See the Gramex installation and upgrade instructions.
Note: Gramex 1.72 does not work with Python 3.8 or 3.9. We recommend Python 3.7.