datasette/datasette/plugin_demo.py
Simon Willison f2720b0c6b
First working prototype of plugins, refs #14
Uses pluggy: https://pluggy.readthedocs.io/

Two example plugins - an uppercase template filter and a convert_units() SQL function.
2018-04-15 16:17:36 -07:00

17 lines
424 B
Python

from datasette import hookimpl
import pint
ureg = pint.UnitRegistry()
@hookimpl
def prepare_jinja2_environment(env):
env.filters['uppercase'] = lambda u: u.upper()
@hookimpl
def prepare_connection(conn):
def convert_units(amount, from_, to_):
"select convert_units(100, 'm', 'ft');"
return (amount * ureg(from_)).to(to_).to_tuple()[0]
conn.create_function('convert_units', 3, convert_units)