home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 381315675

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions issue performed_via_github_app
https://github.com/simonw/datasette/issues/203#issuecomment-381315675 https://api.github.com/repos/simonw/datasette/issues/203 381315675 MDEyOklzc3VlQ29tbWVudDM4MTMxNTY3NQ== 45057 2018-04-14T09:14:45Z 2018-04-14T09:27:30Z CONTRIBUTOR

I'd like to figure out a sensible opt-in way to expose this in the JSON output as well. Maybe with a &_units=true parameter?

<s>From a machine-readable perspective I'm not sure why it would be useful to decorate the values with units</s>. Edit: Should have had some coffee first. It's clearly useful for stuff like map rendering!

I agree that the unit metadata should definitely be exposed in the JSON.

In #204 you said "I'd like to add support for using units when querying but this is PR is pretty usable as-is." - I'm fascinated to hear more about how this could work.

I'm thinking about a couple of approaches here. I think the simplest one is: if the column has a unit attached, optionally accept units in query fields:

```python column_units = ureg("Hz") # Create a unit object for the column's unit query_variable = ureg("4 GHz") # Supplied query variable

Now we can convert the query units into column units before querying

supplied_value.to(column_units).magnitude

4000000000.0

If the user doesn't supply units, pint just returns the plain

number and we can query as usual assuming it's the base unit

query_variable = ureg("50") query_variable

50

isinstance(query_variable, numbers.Number)

True ```

This also lets us do some nice unit conversion on querying:

```python column_units = ureg("m") query_variable = ureg("50 ft")

supplied_value.to(column_units)

<Quantity(15.239999999999998, 'meter')> ```

The alternative would be to provide a dropdown of units next to the query field (so a "Hz" field would give you "kHz", "MHz", "GHz"). Although this would be clearer to the user, it isn't so easy - we'd need to know more about the context of the field to give you sensible SI prefixes (I'm not so interested in nanoHertz, for example).

You also lose the bonus of being able to convert - although pint will happily show you all the compatible units, it again suffers from a lack of context:

```python ureg("m").compatible_units()

frozenset({<Unit('angstrom')>, <Unit('thou')>, <Unit('inch')>, <Unit('link')>, <Unit('foot')>, <Unit('survey_foot')>, <Unit('yard')>, <Unit('rod')>, <Unit('mile')>, <Unit('survey_mile')>, <Unit('league')>, <Unit('light_year')>}) ```

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
313837303  
Powered by Datasette · Queries took 2.745ms · About: github-to-sqlite