No project description provided
Project description
Stencila SDK for Python
Types and functions for using Stencila from within 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 there 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).
📦 Install
python -m pip install stencila
⚡ 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, Paragraph, Text
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 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
article = asyncio.run(
from_string(
'{type: "Article", content: [{type: "Paragraph", content: ["Hello world"]}]}',
format="json5",
)
)
from_path
Use from_path
to decode a file system path, usually to a file, to a schema type. The format of the file can be supplied but if it is not is inferred from the file name. e.g.
import asyncio
from stencila.convert import from_path
article = asyncio.run(
from_path("my-article.jats.xml")
)
to_string
Use to_string
to encode a 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
article = Article(content=[Paragraph(content=[Text(value="Hello world!")])])
markdown = asyncio.run(
to_string(article, format="md")
)
to_path
To encode a 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
article = Article(content=[Paragraph(content=[Text(value="Hello world!")])])
asyncio.run(
to_path(article, "my-article.md")
)
from_to
Use from_to
when you want to convert a file to another format (i.e. as a more performance shortcut to combining from_path
and to_path
)
import asyncio
from stencila.convert import from_to
asyncio.run(
from_to("my-article.md", "my-article.html")
)
🛠️ Develop
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.
Linting and testing
Please run linting checks and tests before contributing any code changes.
make lint test
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
Built Distributions
Hashes for stencila-2.0.0a14-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65e93565e16a46ac2f906d281547f92832bdca40d0c7af6d74ac570a246fabb6 |
|
MD5 | 5a8764f8beef6fe416d506f9a50d06ed |
|
BLAKE2b-256 | 208508cffc1f5c8828ffdfe34d023e12ec6ffc95a4ca6605d13153207c5db127 |
Hashes for stencila-2.0.0a14-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecf23702047de08dcc0e9f73cdb1fc43a0b760456ea218c02af1687d2727ae94 |
|
MD5 | e71a15ef414c4823c0bd3b349c921a30 |
|
BLAKE2b-256 | 3ff2b3f69e85fce1206b4d220edb97b4e0f5d0baf47bddeda6d1f48b08e67f52 |
Hashes for stencila-2.0.0a14-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4874cc6f441db660e2425105aa4a41e5650189a77392ece1a5bf3b4c01b76ca5 |
|
MD5 | 214a6ab9874a533d943e8eff7afb5048 |
|
BLAKE2b-256 | 1863027d38cb8eb2b564993f653373dc72305b9515214358189eafac7c3d1746 |
Hashes for stencila-2.0.0a14-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89522f08a1f60e4d17640c161575ed745b977032e0092445d57c49aca77e5a16 |
|
MD5 | 5a95362d6a163a5153bf3c05d2585b3b |
|
BLAKE2b-256 | 7f1c52877dacb0acb5d52bf3e6f734feb2970001d59552ab52d1f43686a9a8bc |
Hashes for stencila-2.0.0a14-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c14b7a5c3ba591ab3a2f81140369b6f6a0060daf8ba4cc3c11ede21f8576c1f |
|
MD5 | ed2bd193b241a2f0e23cb7ccc3b95f37 |
|
BLAKE2b-256 | 2b655071ca2f982b694254a371643fbd89157d9516882038f190f10668cbe998 |
Hashes for stencila-2.0.0a14-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 303eb53db158338ba36ab0328142a794d41bceb96ab0149d1b4226da5fd87de5 |
|
MD5 | 7f742e89707e39f785be6ff8bf1577b4 |
|
BLAKE2b-256 | 0cd694dcd92a674a71ac809f5045d65dd9f5e4695f744f6f43766c48de7623ce |
Hashes for stencila-2.0.0a14-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 732a6af63809322d6b095e0168cb3abfe196b3e5315ff57a1aade43e1b4efbed |
|
MD5 | cc553358e1d96df8dfd192d11f05c433 |
|
BLAKE2b-256 | a5c3ed5c138fb066991ca44f6a8f60d79869a03880eae813acff61801cd1008f |
Hashes for stencila-2.0.0a14-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4dea744d4ce096cd3ffdb767e9493849dccce8a3f1f845ece6bb4cd816da75b |
|
MD5 | df37474001bdbd9eed0e9dadf3e09db3 |
|
BLAKE2b-256 | d52d7651215eb6e37153b2283092d2342d2d842ead3d5eecd8bcdef6030863d5 |
Hashes for stencila-2.0.0a14-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e2c8f09d80c1218d3af5fa62e938aaf24dbb1e68b031bda937dd950184f5708 |
|
MD5 | a09819c8449f41d62d82b6066e03196b |
|
BLAKE2b-256 | c44031ac850bd9b2b318a44d86aa79e31fbdb841c371c6b855e031f2ba10ccd6 |