Skip to main content

Stencila Types for Python

stencila_types

Introduction

This package provides Python classes for types in the Stencila Schema, shortcuts for easily constructing these types, and utilities for loading and saving the types to JSON.

⚡ Usage

Object types

Object types (aka product types) in the Stencila Schema are represented as a dataclass. For example, to construct an article with a single "Hello world!" paragraph, you can construct Article, Paragraph and Text:

from stencila_types.types import Article, CreativeWork, Paragraph, Text, Thing

article = Article(content=[Paragraph(content=[Text(value="Hello world!")])])

assert isinstance(article, Article)
assert isinstance(article, CreativeWork)
assert isinstance(article, Thing)

assert isinstance(article.content[0], Paragraph)

assert isinstance(article.content[0].content[0], Text)

Union types

Union types (aka sum types) in the Stencila Schema are represented as typing.Union. For example, the Block union type is defined like so:

Block = Union[
    Call,
    Claim,
    CodeBlock,
    CodeChunk,
    Division,
    Figure,
    For,
    Form,
    Heading,
...

Enumeration types

Enumeration types in the Stencila Schema are represented as StrEnum. For example, the CitationIntent enumeration is defined like so:

class CitationIntent(StrEnum):
    """
    The type or nature of a citation, both factually and rhetorically.
    """

    AgreesWith = "AgreesWith"
    CitesAsAuthority = "CitesAsAuthority"
    CitesAsDataSource = "CitesAsDataSource"
    CitesAsEvidence = "CitesAsEvidence"
    CitesAsMetadataDocument = "CitesAsMetadataDocument"
    CitesAsPotentialSolution = "CitesAsPotentialSolution"
    CitesAsRecommendedReading = "CitesAsRecommendedReading"
    CitesAsRelated = "CitesAsRelated"

Shortcuts

Constructing complex Stencila types can be more easily constructed using the shortcuts module.

from stencila_types import types as T
from stencila_types import shortcuts as S

# As above
art1 = T.Article(content=[T.Paragraph(content=[T.Text(value="Hello world!")])])

# Using shortcuts
art2 = S.art(S.p("Hello world!"))

assert art1 == art2

Basic JSON support

import json
from stencila_types.utilities import from_json, to_json

# Using shortcuts
art1 = S.art(S.p("Hello world!"))

s = to_json(art1)
assert s.startswith('{"type": "Article", "id": null,')
art2 = from_json(s)

assert art1 == art2

Metadata

Release files for stencila_types 2.16.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stencila_types 2.16.0
File Size Uploaded
stencila_types-2.16.0.tar.gz 29.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stencila_types 2.16.0
File Interpreter ABI Platform
stencila_types-2.16.0-py3-none-any.whl Python 3 none any Details

Total release size: 58.7 kB

Release files / stencila_types-2.16.0.tar.gz

Download URL stencila_types-2.16.0.tar.gz
Size 29.0 kB
Tags Source
SHA-256 checksum
How to use checksums
dd3c090dc5dc9eca9a29351a3d4f4bcd0d0431775427ce5958f7c6585cbbcca9
BLAKE2b-256 checksum
How to use checksums
bcf91b2577191a33423670e7bc743d19264a983e22c45345e94ed3d15621c53b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / stencila_types-2.16.0-py3-none-any.whl

Download URL stencila_types-2.16.0-py3-none-any.whl
Size 29.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00ae6d03af8e5b45f0597308cf3040fe2b1bf709aca96e2f67ebeb2d1a67214f
BLAKE2b-256 checksum
How to use checksums
71d9f389a854d3998da3ddc838cda0c5a6a01404d2ac4875b5af2e8115ca8c29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page