Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions documentation/OUTPUT_FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ hug provides a large catalog of built-in output formats, which can be used to bu
- `hug.output_format.html`: Outputs Hyper Text Markup Language (HTML).
- `hug.output_format.json_camelcase`: Outputs in the JSON format, but first converts all keys to camelCase to better conform to Javascript coding standards.
- `hug.output_format.pretty_json`: Outputs in the JSON format, with extra whitespace to improve human readability.
- `hug.output_format.json_streaming_lines`: Takes an interable or yield statements and streams it as newline-seperated json objects.
- `hug.output_format.image(format)`: Outputs an image (of the specified format).
- There are convenience calls in the form `hug.output_format.{FORMAT}_image for the following image types: 'png', 'jpg', 'bmp', 'eps', 'gif', 'im', 'jpeg', 'msp', 'pcx', 'ppm', 'spider', 'tiff', 'webp', 'xbm',
'cur', 'dcx', 'fli', 'flc', 'gbr', 'gd', 'ico', 'icns', 'imt', 'iptc', 'naa', 'mcidas', 'mpo', 'pcd',
Expand Down
10 changes: 10 additions & 0 deletions hug/output_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ def json(content, request=None, response=None, ensure_ascii=False, **kwargs):
content, default=_json_converter, ensure_ascii=ensure_ascii, **kwargs
).encode("utf8")

@content_type("application/jsonl; charset=utf-8")
def json_streaming_lines(data, request=None, response=None,**kwargs):
"""Stream a json response using the jsonl format, where each unique json-object is
seperated by a newline
"""
now = str(datetime.utcnow())
response.append_header('Content-Disposition',
(f'attachment; filename="{now}.jsonl"'))
response.stream = (json(i,indent=None,**kwargs)+b"\n" for i in data)
return

def on_valid(valid_content_type, on_invalid=json):
"""Renders as the specified content type only if no errors are found in the provided data object"""
Expand Down