Skip to main content

Python SDK for Stencila

Project description

Stencila SDK for Python

Bindings for using Stencila from Python

👋 Introduction

This package provides Python classes for types in the Stencila Schema and bindings to core Stencila Rust functions.

The primary intended audience is developers who want to develop their own tools on top of Stencila's core functionality. For example, with this package you could construct Stencila documents programmatically using Python and write them to multiple formats (e.g. Markdown, JATS XML, PDF).

[!IMPORTANT] At present, there are only bindings to functions for format conversion, but future versions will expand this scope to include document management (e.g branching and merging) and execution.

📦 Install

python -m pip install stencila

[!NOTE] If you encounter problems with the above command, you may need to upgrade Pip using pip install --upgrade pip.

This is due to a change in the dependency resolver in Pip 20.3.

⚡ Usage

Types

The types module contains representations of all types in the Stencila Schema.

Object types

Object types (aka product types) in the Stencila Schema are represented as a dataclass. At present the __init__ function requires keywords to be used (this is likely to be improved soon).

For example, to construct an article with a single "Hello world!" paragraph, you can construct Article, Paragraph and Text:

from stencila.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"

Conversion

The convert module has five functions for encoding and decoding Stencila documents and for converting documents between formats. All functions are async.

from_string

Use from_string to decode a string in a certain format to a Stencila Schema type. Usually you will need to supply the format argument (it defaults to JSON). e.g.

import asyncio

from stencila.convert import from_string

doc = asyncio.run(
    from_string(
        '''{
            type: "Article",
            content: [{
                type: "Paragraph",
                content: [{
                    type: "Text",
                    value: "Hello world"
                }]
            }]
        }''',
        format="json5",
    )
)

from_path

Use from_path to decode a file system path (usually a file) to a Stencila Schema type. The format can be supplied but if it is not is inferred from the path. e.g.

import asyncio

from stencila.convert import from_path

doc = asyncio.run(from_path("doc.jats.xml"))

to_string

Use to_string to encode a Stencila Schema type to a string. Usually you will want to supply the format argument (it defaults to JSON).

import asyncio

from stencila.convert import to_string
from stencila.types import Article, Paragraph, Text

doc = Article([Paragraph([Text("Hello world!")])])

markdown = asyncio.run(to_string(doc, format="md"))

to_path

To encode a Stencila Schema type to a filesystem path, use to_path. e.g.

import asyncio

from stencila.convert import to_path
from stencila.types import Article, Paragraph, Text

doc = Article([Paragraph([Text("Hello world!")])])

asyncio.run(to_path(doc, "doc.md"))

from_to

Use from_to when you want to convert a file to another format (i.e. as a more performant shortcut to combining from_path and to_path)

import asyncio

from stencila.convert import from_to

asyncio.run(from_to("doc.md", "doc.html"))

[!NOTE] Some of the usage examples above illustrate manually constructing in-memory Python representations of small documents. This is for illustration only and would be unwieldy for large documents. Instead we imagine developers using the convert.from_string or convert.from_path functions to load documents into memory from other formats, or writing functions to construct documents composed of the Stencila classes.

🛠️ Develop

Bindings

This packages uses PyO3 and Maturin to generate a Python native extension from Stencila Rust functions. It uses the layout recommended for mixed Rust/Python projects: Rust code is in src and Python code and tests is in python.

To build the native extension and use it in a Python shell:

make run

To build the native extension for the current platform (for several versions of Python):

make build

Linting and testing

Please run linting and tests before contributing any code changes.

make lint test

There is also a make fix recipe that will fix any formatting or linting issues.

Testing on different Python versions

You can use asdf to test this package across different versions of Python:

asdf install python 3.9.18
asdf local python 3.9.18
poetry env use 3.9.18
poetry install
make test

[!NOTE] In the future, we may use tox (or similar) to run tests across Python versions. But how to make that work with pyo3 and maturin is yet to be resolved.

Code organization

types module

Most of the types are generated from the Stencila Schema by the Rust schema-gen crate. See there for contributing instructions.

convert module

The convert module is implemented in Rust (src/convert.rs) with a thin Python wrapper (python/stencila/convert.py) to provide documentation and conversion to the types in the types module.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

stencila-2.0.0b2-cp312-none-win_amd64.whl (11.2 MB view details)

Uploaded CPython 3.12 Windows x86-64

stencila-2.0.0b2-cp312-cp312-manylinux_2_34_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.34+ x86-64

stencila-2.0.0b2-cp312-cp312-manylinux_2_31_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.31+ x86-64

stencila-2.0.0b2-cp312-cp312-macosx_11_0_arm64.whl (10.5 MB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

stencila-2.0.0b2-cp312-cp312-macosx_10_12_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.12 macOS 10.12+ x86-64

stencila-2.0.0b2-cp311-none-win_amd64.whl (11.2 MB view details)

Uploaded CPython 3.11 Windows x86-64

stencila-2.0.0b2-cp311-cp311-manylinux_2_34_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.34+ x86-64

stencila-2.0.0b2-cp311-cp311-manylinux_2_31_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.31+ x86-64

stencila-2.0.0b2-cp311-cp311-macosx_11_0_arm64.whl (10.5 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

stencila-2.0.0b2-cp311-cp311-macosx_10_12_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.11 macOS 10.12+ x86-64

stencila-2.0.0b2-cp310-none-win_amd64.whl (11.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

stencila-2.0.0b2-cp310-cp310-manylinux_2_34_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.34+ x86-64

stencila-2.0.0b2-cp310-cp310-manylinux_2_31_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.31+ x86-64

stencila-2.0.0b2-cp310-cp310-macosx_11_0_arm64.whl (10.5 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

stencila-2.0.0b2-cp310-cp310-macosx_10_12_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.10 macOS 10.12+ x86-64

stencila-2.0.0b2-cp39-none-win_amd64.whl (11.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

stencila-2.0.0b2-cp39-cp39-manylinux_2_34_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.34+ x86-64

stencila-2.0.0b2-cp39-cp39-manylinux_2_31_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

stencila-2.0.0b2-cp39-cp39-macosx_11_0_arm64.whl (10.5 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

stencila-2.0.0b2-cp39-cp39-macosx_10_12_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.9 macOS 10.12+ x86-64

stencila-2.0.0b2-cp38-none-win_amd64.whl (11.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

stencila-2.0.0b2-cp38-cp38-manylinux_2_34_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.34+ x86-64

stencila-2.0.0b2-cp38-cp38-manylinux_2_31_x86_64.whl (11.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.31+ x86-64

stencila-2.0.0b2-cp38-cp38-macosx_11_0_arm64.whl (10.5 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

stencila-2.0.0b2-cp38-cp38-macosx_10_12_x86_64.whl (11.5 MB view details)

Uploaded CPython 3.8 macOS 10.12+ x86-64

File details

Details for the file stencila-2.0.0b2-cp312-none-win_amd64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp312-none-win_amd64.whl
Algorithm Hash digest
SHA256 3c86d6d46cc3a7b6f1c3ffb9c0c561f0c06bd81fa441988b5cb0e55cc19d544e
MD5 47793a06edb0c89abcc20d7b09924fb0
BLAKE2b-256 50313665da8ebba2a9cddbeb1b0c18eec3b6a9e36750ef28592574c95148fb06

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d014c511c146ab82b971e37f91912cc0473230af5a8e37e17baf14304c673e35
MD5 9577fd46919b348e2e93fbdf8c31ec26
BLAKE2b-256 fa88fc5f25492e37b619d6299035e95196009eaf80f034f2a2066be4f6bd6c4e

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp312-cp312-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp312-cp312-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 07b41b7eec0f47b3615db0187cc83a3c77bacebd29e15d25b6116686d4dd881f
MD5 379b21d243d96328088e99a29ab6e525
BLAKE2b-256 32d8f46663c8790e505a859c67ad5228da5f9befc53e328bad846559dcd94678

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7101432a8e7894a2c5b25e84f3f803ed5761f040cbee6491ee2d7c072a933060
MD5 fffc647ebd23137e5558026cfae23b6c
BLAKE2b-256 5f07aaf19fe490ca3cfc0e8be05a8d74085410bed8a3c81b1c75287c16ef4da2

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e9fab1fbb059c8f6f43a7f83bbf71c45270c205c1492830ecca30c42a93af944
MD5 482f740aa076741c0a398caea7a6222e
BLAKE2b-256 bea00a81a01118ee5864f45bda9ed6941c253f1a7edd4cec3665821934d6f5b9

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 7b24aebe313972b95e77b9dfef2c6c29edc720b6290ba0a4c49c0f73e5c3008e
MD5 684e2174c63702a8ca14faef70c75c1b
BLAKE2b-256 245b37971b2e6e65ac7d4e3c37f747b953ec9bb679215a87e5c2e6f8b531b9dc

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 29559154984a6f59a6587221354cc8171cd58730c44e98d20fa5c2f517276591
MD5 a039c002a912ce74e161659a62ba8c19
BLAKE2b-256 abc3201a5721140b91be9789866bf344744e4b2ddecd250969f773065a3e3856

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp311-cp311-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp311-cp311-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 617a1adc887dc3e01c682d10ab9c35e47127c7485dfb34b946fbe37fb80ab45a
MD5 b0349471c58bc2bc285e05f2465bf8cb
BLAKE2b-256 65477e2ae4d0eb0bc74ac221324243a3af4af33d05d88fa7bdbbdd873b0f7c33

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 953e5945dc0350037aff049407723d80efa50c146c7afe736554bc99b7798bf6
MD5 fcb6bdb5e2ee43fea9f21e84941cbd99
BLAKE2b-256 2eddbd30936ff922c24a4fd53716085017f4775ca7e8c05158b022640694e544

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9566f94d88aad579f1885254b40d17a985804eef46eda1022e6f0c4245971c05
MD5 3869a74731a11c02d7d5a178a0086412
BLAKE2b-256 3f4c019b459efb62dfdf7ba2812e57d23d8217e38f2aed88dee31c04ca31d322

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 e692798d486a326814f40483b81597eec521cb21dbcf0eab11a70ea35a226d77
MD5 8f08a8d7ce9da07503f42b1a01a39440
BLAKE2b-256 4d8ec874176f6ae2b64355828ca54b3d40b44c531ebb0101db5ae7f82b6f36e3

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4537bf8b3caad83b66526b5f18c54eaf6abfec2a29ce2ea97cdf639b7e09ee19
MD5 68cf7609e159ac9820ecb116d2abb2ec
BLAKE2b-256 5f41bc2e582390a210b590acbc42dae79059d11ca7e960afcee3bc00bc8c017f

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp310-cp310-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp310-cp310-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 ceaf4e1dacc58155a85b273c58838a197069981da2c02125de0d96b81265adfd
MD5 dd4b1993033a7ee08c283428a1c7db43
BLAKE2b-256 1b199fa87239ec7bd8d6554d63b994a1810c3a828e786c0247ddb5ff49076f12

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4b1b1dd2c206b004ce10a176bc141900bd08b74615010f5ec2efe7ebeb197a46
MD5 fae4fe248f5c67672c5dd5c9d4d09541
BLAKE2b-256 6ee0e096bcee3501239473e0f80a8d6532186a3a4bec702a03f69c04c16cab24

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 188a0845f4f08ab09c854e037e42eb1d90550c52779929f6c821979ec092a6ce
MD5 848586317e5af34f9bf49072bf9b468f
BLAKE2b-256 f7347196823800faef7685b1feafb5a1b8c64c9917d003cc027c47afa733189d

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 854e66e31af455a82dd089c3b90ab243577d7822eb537cb3c98ff8cb4d0d86c3
MD5 2ba3941ff72e572ea05640251110ff9a
BLAKE2b-256 c52f8d41448df3aabdc41681536b762a3226b67cafd9bcdbe5acfb103f1d6ea0

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 dc2061e84ed2baf3eadb4d8c87a0a1eb7faa4f2580b840d831e8351a5d0c92f9
MD5 fe487c57693a63601494c7d35d80b5dc
BLAKE2b-256 eec9a6393f924f4596708070e5c406f514b2bd45679c91855cc5a4dad118aa01

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp39-cp39-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp39-cp39-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 2827f378a9a10313ffea29eb4114f373a65335a6657ead33180ca34b757d629c
MD5 447138a0c9043c7c28ad4ae0d9d69665
BLAKE2b-256 f5726e56626030b18bbba48d8d658078bfe18b0d1f334ad7b541394606ee9019

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1dc55828d7a95fedb6c2799536fbe02d2cf29d34c2a0ba0c70274d956e0330d1
MD5 1c54717f9ed66f983e9a426d5bfb7525
BLAKE2b-256 0196f335bc06951ea037ff4f5d140c95d1fcb7e6efcf28fdca92593b9610291d

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fb6a4aef5ecebe70290264813fbacdf8da195309638a3e621814f85a5926f4bb
MD5 f86b440e8f90c3d315277c95a0b922bc
BLAKE2b-256 9afcf66a597f09b7a71879d988a1c4e74f1b4d6a8b769946a067f4cada4209cb

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 6024340d1e40a1ceca6c2f9baa2b911c0ffd5a5fd5f53351ec86d9a24f685d3e
MD5 310bf53fe8c3c8c6efefc7fbf119d2d2
BLAKE2b-256 b2150ee22b858a0d952ea9d6102ee3c728015309cdfb583d3dc2687c22c1fe49

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp38-cp38-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp38-cp38-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 14d6fe7be7dda878d900dcfc7bdde094a22b01ba3ef8a04bd6904a9498f91341
MD5 2ea4cdfece122bbad97f727cdc5c1bfb
BLAKE2b-256 99c0c26390902d2a0cce0886a996aced2a19ac729c875263ea1b20c0f88c8a20

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp38-cp38-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp38-cp38-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 fb8aa8037f757bd58f1b5d32c788b3c106b9f0d7d0c6ba096584ce9039672f01
MD5 794256f41ded71b4adfd31ea3f6566ab
BLAKE2b-256 ff978b024e8b9ba87195e48b8914008683516ec4e12696c77937bd3fd395a6ff

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bcb79ddce7eda5a9321c075c5c248dd2b6fb337b6d31188fc45096e903222710
MD5 feeec78119bef3b743e1f04679bc6f80
BLAKE2b-256 2c80d197869960d0538818cb10ca53ff53c06ccff9eaa1f391a0faca9156244e

See more details on using hashes here.

File details

Details for the file stencila-2.0.0b2-cp38-cp38-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for stencila-2.0.0b2-cp38-cp38-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d55618ab38f7aa16396e7685168a732342c11ec10593fd865e723ca55522d017
MD5 7fc073dda8adaf9c7cb40ab687816885
BLAKE2b-256 2e8a62f03eedc0232d8d4292c454889e5f832e1cb334cb2f070a0fa47b0240c4

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