Skip to main content

Generate static HTML documentation from JSON schemas

Project description

PyPI version

JSON Schema for Humans

Quickly generate a beautiful HTML static page documenting a JSON schema

Documentation (with visual examples)

Features

  • Support for JSON Schema Draft-07
  • Since the result is static, it is easier to host and faster to load
  • Different templates to choose from (See here)
  • Anchor links, allow to send a user to a specific section of the documentation
  • Support for references (even circular!)

Installation

pip install json-schema-for-humans

Usage

Options for generation of the doc are documented using this very library

They can be supplied in various ways:

  • Using a JSON or YAML configuration file with the CLI option --config-file
  • Using the CLI option --config
  • Using a ConfigurationOption from code

More details are available in the appropriate sections below.

From CLI

generate-schema-doc [OPTIONS] SCHEMA_FILE [RESULT_FILE]

SCHEMA_FILE must be a valid JSON Schema (in JSON or YAML format)

The default value for RESULT_FILE is schema_doc.html

CLI options

--config

Supply generation config parameters. The parameters are documented in the JSON schema config_schema.json at the root of the repo or see the generated doc.

Each parameter is in the format --config parameter_name=parameter_value. Example: --config expand_buttons=true. The parameter value must be valid JSON.

For flags, you can also omit the value for true or prefix the parameter name with no_ for false. Example: --config expand_buttons or --config no_expand_buttons.

--config-file

Path to a JSON or YAML configuration file respecting the schema config_schema.json.

Example: --config-file jsfh-conf.yaml where jsfh-conf.yaml is in the current directory and contains the following:

description_is_markdown: false
expand_buttons: true
copy_js: false

From code

There are 3 methods that one could use:

Method Name Schema input Output CSS and JS copied?
generate_from_schema schema_file as str, Path (from pathlib) or a file object Rendered HTML as a str No
generate_from_filename schema_file_name as a str or Path Rendered HTML written to the file at path result_file_name Yes
generate_from_file_object schema_file as an open file object (read mode) Rendered HTML written to the file at result_file, which must be an open file object (in write mode) Yes

Notes:

  • When using file objects, it is assumed that files are opened with encoding "utf-8"
  • CSS and JS files are copied to the current working directory with names "schema_doc.css" and "schema_doc.min.js" respectively
  • Other parameters of these methods are analogous to the CLI parameters documented above.

The GenerationConfiguration object

To reduce the number of parameters to pass from function to function in the code, there is a GenerationConfiguration object that should be used for providing options.

Example:

from json_schema_for_humans.generate import GenerationConfiguration, generate_from_filename


config = GenerationConfiguration(copy_css=False, expand_buttons=True)

generate_from_filename("my_schema.json", "schema_doc.html", config=config)

# Your doc is now in a file named "schema_doc.html". Next to it, "schema_doc.min.js" was copied, but not "schema_doc.css"
# Your doc will contain a "Expand all" and a "Collapse all" button at the top

Pre-load schemas

generate_from_schema has a loaded_schemas parameter that can be used to pre-load schemas. This must be a dict with the key being the real path of the schema file and the value being the result of loading the schema (with json.load or yaml.safe_load, for example).

This should not be necessary in normal scenarios.

What's supported

See the excellent Understanding JSON Schema to understand what are those checks

The following are supported:

  • Types
  • Regular expressions
  • String length
  • Numeric types multiples and range
  • Constant and enumerated values
  • Required properties
  • Pattern properties
  • Default values
  • Array minItems, maxItems, uniqueItems, items (schema that must apply to all of the array items), and contains
  • Combining schema with oneOf, allOf, anyOf, and not
  • Examples
  • Conditional subschemas

These are not supported at the moment (PRs welcome!):

  • String format
  • Property names and size
  • Array items at specific index (for example, first item must be a string and second must be an integer)
  • Property dependencies
  • Media

References

References are supported:

  • To another part of the schema, e.g. { $ref: "#/definitions/something" }
  • To a local file, {"$ref": "references.json"}, {"$ref": "references.json#/definitions/something"}
  • To a URL, {"$ref": "http://example.com/schema.json"}, {"$ref": "http://example.com/schema.json#/definitions/something"}

You can have a description next to a $ref, it will be displayed in priority to the description from the referenced element.

If you have several attributes using the same definition, the definition will only be rendered once. All other usages of the same definition will be replaced with an anchor link to the first render of the definition. This can be turned off using --config no_link_to_reused_ref. See With references in the examples.

Templates

Templates control the style of the generated documentation.

js

This is the default template. It uses Bootstrap along with minimal Javascript to allow for the following:

  • Properties are in expandable dynamic sections. You can include a button to expand or collapse all. (See here)
  • Conditional subschemas (anyOf, oneOf, allOf) are in tabbed sections
  • Anchor links will scroll to, expand, and animate the target section
  • Long descriptions are collapsed by default

When using this template, you need to include the Javascript file (schema_doc.min.js) that is automatically copied next to the output HTML file (schema_doc.html by default).

flat

Note: This template is a work in progress

It is sometimes not possible or desirable to include custom Javascript in documentation. This template addresses this issue by removing interactive elements in favor of simpler HTML.

At the moment, this means the whole documentation is generated without any collapsible sections, which may make it hard to understand the schema structure. Contributions are welcomed to improve it!

Contributing

See CONTRIBUTING.md

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json-schema-for-humans-0.29.3.tar.gz (73.2 kB view details)

Uploaded Source

Built Distribution

json_schema_for_humans-0.29.3-py3-none-any.whl (42.3 kB view details)

Uploaded Python 3

File details

Details for the file json-schema-for-humans-0.29.3.tar.gz.

File metadata

  • Download URL: json-schema-for-humans-0.29.3.tar.gz
  • Upload date:
  • Size: 73.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for json-schema-for-humans-0.29.3.tar.gz
Algorithm Hash digest
SHA256 f17b92be7fbb821ab463f3fdd2e1c6947849ca4d152b5c3fdf627156cd11350d
MD5 b305baf4eaeeabefcc5a6f681523a807
BLAKE2b-256 7e375b859e950fa5ab15db4059135cc8a79f78b247bffb489d8f7b0aa0dd238d

See more details on using hashes here.

File details

Details for the file json_schema_for_humans-0.29.3-py3-none-any.whl.

File metadata

  • Download URL: json_schema_for_humans-0.29.3-py3-none-any.whl
  • Upload date:
  • Size: 42.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for json_schema_for_humans-0.29.3-py3-none-any.whl
Algorithm Hash digest
SHA256 87eba45717d245367b5852a14a4e1234a09e3be9e35e0cf74664e2234dd88d88
MD5 d170218a6ccf69bb3d8e51c7949f6ca7
BLAKE2b-256 b6564618c2a4fb211522e6adf1693d918c951c688adea6011b8c28cff83f5f5a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page