mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Allow custom units to be registered with Pint
This commit is contained in:
parent
ab85605c61
commit
3c985ec271
2 changed files with 15 additions and 1 deletions
|
|
@ -1186,6 +1186,11 @@ class Datasette:
|
||||||
}
|
}
|
||||||
return self._inspect
|
return self._inspect
|
||||||
|
|
||||||
|
def register_custom_units(self):
|
||||||
|
"Register any custom units defined in the metadata.json with Pint"
|
||||||
|
for unit in self.metadata.get('custom_units', []):
|
||||||
|
ureg.define(unit)
|
||||||
|
|
||||||
def app(self):
|
def app(self):
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
default_templates = str(app_root / 'datasette' / 'templates')
|
default_templates = str(app_root / 'datasette' / 'templates')
|
||||||
|
|
@ -1230,6 +1235,8 @@ class Datasette:
|
||||||
'/<db_name:[^/]+>/<table:[^/]+?>/<pk_path:[^/]+?><as_json:(\.jsono?)?$>'
|
'/<db_name:[^/]+>/<table:[^/]+?>/<pk_path:[^/]+?><as_json:(\.jsono?)?$>'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.register_custom_units()
|
||||||
|
|
||||||
@app.exception(Exception)
|
@app.exception(Exception)
|
||||||
def on_exception(request, exception):
|
def on_exception(request, exception):
|
||||||
title = None
|
title = None
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,14 @@ Column units are configured in the metadata like so::
|
||||||
}
|
}
|
||||||
|
|
||||||
Units are interpreted using Pint_, and you can see the full list of available units in
|
Units are interpreted using Pint_, and you can see the full list of available units in
|
||||||
Pint's `unit registry`_.
|
Pint's `unit registry`_. You can also add custom units to the metadata, which will be
|
||||||
|
registered with Pint::
|
||||||
|
|
||||||
|
{
|
||||||
|
"custom_units": [
|
||||||
|
"decibel = [] = dB"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
.. _Pint: https://pint.readthedocs.io/
|
.. _Pint: https://pint.readthedocs.io/
|
||||||
.. _unit registry: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
|
.. _unit registry: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue