home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 1627451646

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/1153#issuecomment-1627451646 https://api.github.com/repos/simonw/datasette/issues/1153 1627451646 IC_kwDOBm6k_c5hAPD- 9599 2023-07-08T18:21:24Z 2023-07-08T18:21:24Z OWNER

This one was tricky:

I wanted complete control over the YAML example here, so I could ensure it used multi-line strings correctly.

I ended up changing my cog helper function to this:

```python import json import textwrap from yaml import safe_dump from ruamel.yaml import round_trip_load

def metadata_example(cog, data=None, yaml=None): assert data or yaml, "Must provide data= or yaml=" assert not (data and yaml), "Cannot use data= and yaml=" output_yaml = None if yaml: # dedent it first yaml = textwrap.dedent(yaml).strip() # round_trip_load to preserve key order: data = round_trip_load(yaml) output_yaml = yaml else: output_yaml = safe_dump(data, sort_keys=False) cog.out("\n.. tab:: YAML\n\n") cog.out(" .. code-block:: yaml\n\n") cog.out(textwrap.indent(output_yaml, " ")) cog.out("\n\n.. tab:: JSON\n\n") cog.out(" .. code-block:: json\n\n") cog.out(textwrap.indent(json.dumps(data, indent=2), " ")) cog.out("\n") This allows me to call it ith YAML in some places: .. [[[cog metadata_example(cog, yaml=""" databases: fixtures: queries: neighborhood_search: fragment: fragment-goes-here hide_sql: true sql: |- select neighborhood, facet_cities.name, state from facetable join facet_cities on facetable.city_id = facet_cities.id where neighborhood like '%' || :text || '%' order by neighborhood; """) .. ]]] ``` I had to introduce https://pypi.org/project/ruamel.yaml/ as a dependency here in order to load YAML from disk while maintaining key order.

I'm still using safe_dump(data, sort_keys=False) from PyYAML as I couldn't get the result I wanted for outputting YAML from an input of JSON using PyYAML.

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