Gramex 1.84 adds an ML Predictor, FormHandler date comparisons, and deprecates old UI libraries.
MLPredictor lets you run ML models on dynamic datasets and render the output in a FormHandler-like interface.
You should use MLPredictor if
scikit-learn
compatible .predict()
function. You must save it as model.pkl
Let’s create a sample classifier model with this iris.csv
and save it in model.pkl
:
import joblib
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
data = pd.read_csv('iris.csv')
model = KNeighborsClassifier()
model.fit(data.drop(columns=['species']), data['species'])
joblib.dump(model, 'model.pkl')
This model.pkl
can be used to predict data from any file (including the original iris.csv
) with this gramex.yaml
config:
url:
mlpredictor-iris:
pattern: /iris
handler: MLPredictor
kwargs:
config_dir: $YAMLPATH
data:
url: iris.csv
columns: [sepal_length, sepal_width, petal_length, petal_width]
target_col: species
Now, visit /iris?petal_length>=6&_format=html
. This will run the predictions for all petal lengths > 6, and show:
sepal_length | sepal_width | petal_length | petal_width | species |
---|---|---|---|---|
7.6 | 3.0 | 6.6 | 2.1 | versicolor |
7.3 | 2.9 | 6.3 | 1.8 | virginica |
7.2 | 3.6 | 6.1 | 2.5 | virginica |
7.7 | 3.8 | 6.7 | 2.2 | versicolor |
7.7 | 2.6 | 6.9 | 2.3 | virginica |
7.4 | 2.8 | 6.1 | 1.9 | virginica |
7.9 | 3.8 | 6.4 | 2.0 | versicolor |
7.7 | 3.0 | 6.1 | 2.3 | versicolor |
The kwargs:
MLPredictor accepts are:
config_dir
: The directory containing a model.pkl
file. The file MUST be called model.pkl
. This is consistent with MLHandlerdata
: The location of the data to predict from. This can contain any dynamic database
dynamic file. E.g.:data: {url: /path/to/file.xlsx}
data: {url: 'postgresql://$USER:$PASS@server/db'; table: public.sales}
columns
: List of input columns from data
to pass to the model. If the model was created with MLHandler, this is optional.target_col
: Optional name of the predicted column. If the model was created with MLHandler, it picks it from the training dataset. Else defaults to prediction
.All FormHandler filters are applicable, making MLPredictor an good way to predict for different subsets. For example:
/mlpredictor?date=2022-22-02
– predict all results for a specific date/mlpredictor?sort=-date&_limit=30
– predict the 30 most recent results/mlpredictor?city=Rome
– predict only for the city of RomeFormHandler used to treat dates as strings for comparison. For example,
?date>=20201231
becomes SELECT * FROM table WHERE date > "20201231"
.
This works fine for specific date formats on specific databases, e.g. YYYYMMDD
. But it does not
work well for ?date=2020-12-31
on all databases. Nor does ?date=12/31/2020
or ?date=31-Dec-2020
.
Now FormHandler parses all date columns
sensibly (defaulting to MM/DD/YY – for example, 10/11/12
is Oct 11, 2012).
Gramex serves several UI libraries as part of its UI components. But different applications need different versions.
Rather than maintain all versions in Gramex, we’re deprecating these libraries. App developers should npm install
their own libraries.
The following libraries will be removed in v1.86.
"@fortawesome/fontawesome-free": "5",
"bootstrap-select": "1.13",
"comicgen": "^1.9.1",
"d3": "4",
"d3-scale-chromatic": "*",
"d3v5": "npm:d3@5",
"daterangepicker": "2",
"daterangepickerv3": "npm:daterangepicker@3",
"dayjs": "^1.11.1",
"dropzone": "5",
"file-saver": "2",
"font-awesome": "*",
"g1": "^0.18.0",
"html2canvas": "^1.4.1",
"jquery": "~3",
"leaflet": "1.6",
"lodash": "4",
"moment": "^2.29.3",
"morphdom": "2",
"noty": "3",
"numeral": "2",
"popper.js": "1",
"select2": "4",
"tether-shepherd": "1",
"topojson": "3",
"uifactory": "^1.24.0",
"url-search-params": "npm:@ungap/url-search-params@0.1",
"vega": "^5.22.1",
"vega-embed": "^6.20.8",
"vega-lite": "4",
Gramex 1.84 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: