Skip to main content

Python interface to generate Typst code.

Project description

python-typst

python-typst is a library for generating executable typst code (See typst repository and typst documentation for more information). It is written primarily in functional programming paradigm with some OOP content. Each module has greater than 90% unit test coverage.

This package provides the interfaces in a way that is as close as possible to typst's native functions. Through python-typst and other data processing packages, you can generate data reports quickly.

Repository on GitHub: python-typst. Homepage on PyPI: python-typst. Contributions are welcome.

Installation

pip install typstpy

Current Support

Is Standard Function Name Original Name Documentation
True _color_hsl color.hsl color.hsl
True _color_linear_rgb color.linear-rgb color.linear-rgb
True _figure_caption figure.caption figure.caption
True bibliography bibliography bibliography
True cite cite cite
True cmyk cmyk cmyk
False color None None
True emph emph emph
True figure figure figure
True footnote footnote footnote
True heading heading heading
True image image image
True link link link
True lorem lorem lorem
True lower lower lower
True luma luma luma
True pagebreak pagebreak pagebreak
True par par par
True ref ref ref
True rgb rgb rgb
True smallcaps smallcaps smallcaps
True strong strong strong
True sub sub sub
True sup super super
True text text text

Examples

from typstpy import *

_color_hsl

>>> color.hsl(Angle.deg(30), Ratio(100), Ratio(50), Ratio(50))
'#color.hsl(30deg, 100%, 50%, 50%)'
>>> color.hsl(Angle.deg(30), 100, 50)
'#color.hsl(30deg, 100, 50)'

_color_linear_rgb

>>> color.linear_rgb(255, 255, 255)
'#color.linear-rgb(255, 255, 255)'
>>> color.linear_rgb(255, 255, 255, 0.5)
'#color.linear-rgb(255, 255, 255, 0.5)'
>>> color.linear_rgb(Ratio(50), Ratio(50), Ratio(50), Ratio(50))
'#color.linear-rgb(50%, 50%, 50%, 50%)'

_figure_caption

>>> figure.caption("This is a caption.")
'This is a caption.'
>>> figure.caption(strong("This is a caption."))
'#strong[This is a caption.]'
>>> figure.caption("This is a caption.", position=Alignment.TOP)
'#figure.caption(position: top, [This is a caption.])'
>>> figure.caption(strong("This is a caption."), position=Alignment.TOP)
'#figure.caption(position: top, strong[This is a caption.])'
>>> figure.caption("This is a caption.", separator="---")
'#figure.caption(separator: [---], [This is a caption.])'
>>> figure.caption("This is a caption.", position=Alignment.TOP, separator="---")
'#figure.caption(position: top, separator: [---], [This is a caption.])'

bibliography

>>> bibliography("references.bib")
'#bibliography("references.bib")'
>>> bibliography("references.bib", title="My Bib")
'#bibliography("references.bib", title: [My Bib])'
>>> bibliography("references.bib", title=None)
'#bibliography("references.bib", title: none)'
>>> bibliography("references.bib", full=True)
'#bibliography("references.bib", full: true)'
>>> bibliography("references.bib", style="annual-reviews")
'#bibliography("references.bib", style: "annual-reviews")'
>>> bibliography("references.bib", title="My Bib", full=True, style="annual-reviews")
'#bibliography("references.bib", title: [My Bib], full: true, style: "annual-reviews")'

cite

>>> label = Label("Essay")
>>> cite(label)
'#cite(<Essay>)'
>>> cite(label, supplement="1")
'#cite(<Essay>, supplement: [1])'
>>> cite(label, form="prose")
'#cite(<Essay>, form: "prose")'
>>> cite(label, style="ieee")
'#cite(<Essay>, style: "ieee")'
>>> cite(label, supplement="1", form="prose", style="ieee")
'#cite(<Essay>, supplement: [1], form: "prose", style: "ieee")'

cmyk

>>> cmyk(Ratio(50), Ratio(50), Ratio(50), Ratio(50))
'#cmyk(50%, 50%, 50%, 50%)'

color

>>> color("black")
'#luma(0)'
>>> color("gray")
'#luma(170)'
>>> color("silver")
'#luma(221)'
>>> color("white")
'#luma(255)'
>>> color("navy")
'#rgb("#001f3f")'
>>> color("blue")
'#rgb("#0074d9")'
>>> color("aqua")
'#rgb("#7fdbff")'
>>> color("teal")
'#rgb("#39cccc")'
>>> color("eastern")
'#rgb("#239dad")'
>>> color("purple")
'#rgb("#b10dc9")'
>>> color("fuchsia")
'#rgb("#f012be")'
>>> color("maroon")
'#rgb("#85144b")'
>>> color("red")
'#rgb("#ff4136")'
>>> color("orange")
'#rgb("#ff851b")'
>>> color("yellow")
'#rgb("#ffdc00")'
>>> color("olive")
'#rgb("#3d9970")'
>>> color("green")
'#rgb("#2ecc40")'
>>> color("lime")
'#rgb("#01ff70")'

emph

>>> emph("Hello, World!")
'#emph([Hello, World!])'
>>> emph(text("Hello, World!", font="Arial", fallback=True))
'#emph(text(font: "Arial")[Hello, World!])'

figure

>>> figure(image("image.png"))
'#figure(image("image.png"))'
>>> figure(image("image.png"), Label("fig:Figure"))
'#figure(image("image.png")) <fig:Figure>'
>>> figure(image("image.png"), placement=Alignment.TOP)
'#figure(image("image.png"), placement: top)'
>>> figure(image("image.png"), caption="This is a caption.")
'#figure(image("image.png"), caption: [This is a caption.])'
>>> figure(image("image.png"), caption=figure.caption("This is a caption.", position=Alignment.TOP, separator="---"))
'#figure(image("image.png"), caption: figure.caption(position: top, separator: [---], [This is a caption.]))'
>>> figure(image("image.png"), kind="figure")
'#figure(image("image.png"), kind: "figure")'
>>> figure(image("image.png"), supplement="Bar")
'#figure(image("image.png"), supplement: [Bar])'
>>> figure(image("image.png"), numbering="1.")
'#figure(image("image.png"), numbering: "1.")'
>>> figure(image("image.png"), gap=Length.em(0.5))
'#figure(image("image.png"), gap: 0.5em)'
>>> figure(image("image.png"), outlined=False)
'#figure(image("image.png"), outlined: false)'

footnote

>>> footnote("Hello, World!")
'#footnote([Hello, World!])'
>>> footnote(text("Hello, World!", font="Arial"))
'#footnote(text(font: "Arial")[Hello, World!])'

heading

>>> heading("Hello, World!")
'= Hello, World!'
>>> heading("Hello, World!", level=2)
'== Hello, World!'
>>> heading("Hello, World!", depth=2)
'== Hello, World!'
>>> heading("Hello, World!", offset=1)
'== Hello, World!'
>>> heading("Hello, World!", level=4, depth=2, offset=1)
'==== Hello, World!'
>>> heading("Hello, World!", numbering="a.")
'#heading(numbering: "a.", level: 1)[Hello, World!]'
>>> heading("Hello, World!", supplement="Chapter")
'#heading(supplement: [Chapter], level: 1)[Hello, World!]'
>>> heading("Hello, World!", outlined=False)
'#heading(outlined: false, level: 1)[Hello, World!]'
>>> heading("Hello, World!", bookmarked=False)
'#heading(bookmarked: false, level: 1)[Hello, World!]'

image

>>> image("image.png")
'#image("image.png")'
>>> image("image.png", format="png")
'#image("image.png", format: "png")'
>>> image("image.png", width=Ratio(50))
'#image("image.png", width: 50%)'
>>> image("image.png", height=Ratio(50))
'#image("image.png", height: 50%)'
>>> image("image.png", alt="An image")
'#image("image.png", alt: "An image")'

link

>>> link("https://typst.app/docs/")
'#link("https://typst.app/docs/")'
>>> link(Label("chap:chapter"))
'#link(<chap:chapter>)'

lorem

>>> lorem(10)
'#lorem(10)'

lower

>>> lower("Hello, World!")
'#lower([Hello, World!])'
>>> lower(text("Hello, World!", font="Arial"))
'#lower(text(font: "Arial")[Hello, World!])'
>>> lower(emph("Hello, World!"))
'#lower(emph([Hello, World!]))'

luma

>>> luma(50)
'#luma(50)'
>>> luma(50, 0.5)
'#luma(50, 0.5)'
>>> luma(Ratio(50), Ratio(50))
'#luma(50%, 50%)'

pagebreak

>>> pagebreak()
'#pagebreak()'
>>> pagebreak(weak=True)
'#pagebreak(weak: true)'
>>> pagebreak(to="even")
'#pagebreak(to: "even")'
>>> pagebreak(to="odd")
'#pagebreak(to: "odd")'
>>> pagebreak(weak=True, to="even")
'#pagebreak(weak: true, to: "even")'
>>> pagebreak(weak=True, to="odd")
'#pagebreak(weak: true, to: "odd")'

par

>>> par("Hello, World!")
'Hello, World!'
>>> par("Hello, World!", leading=Length.em(1.5))
'#par(leading: 1.5em)[Hello, World!]'
>>> par("Hello, World!", justify=True)
'#par(justify: true)[Hello, World!]'
>>> par("Hello, World!", linebreaks="optimized")
'#par(linebreaks: "optimized")[Hello, World!]'
>>> par("Hello, World!", first_line_indent=Length.em(1.5))
'#par(first-line-indent: 1.5em)[Hello, World!]'
>>> par("Hello, World!", hanging_indent=Length.em(1.5))
'#par(hanging-indent: 1.5em)[Hello, World!]'
>>> par("Hello, World!", leading=Length.em(1.5), justify=True, linebreaks="optimized", first_line_indent=Length.em(1.5), hanging_indent=Length.em(1.5))     
'#par(leading: 1.5em, justify: true, linebreaks: "optimized", first-line-indent: 1.5em, hanging-indent: 1.5em)[Hello, World!]'

ref

>>> label = Label("chap:chapter")
>>> ref(label)
'#ref(<chap:chapter>)'
>>> ref(Label("chap:chapter"), supplement="Spam!")
'#ref(<chap:chapter>, supplement: [Spam!])'
>>> ref(Label("chap:chapter"), supplement=None)
'#ref(<chap:chapter>, supplement: none)'

rgb

>>> rgb(255, 255, 255)
'#rgb(255, 255, 255)'
>>> rgb(255, 255, 255, 0.5)
'#rgb(255, 255, 255, 0.5)'
>>> rgb(Ratio(50), Ratio(50), Ratio(50), Ratio(50))
'#rgb(50%, 50%, 50%, 50%)'
>>> rgb("#ffffff")
'#rgb("#ffffff")'

smallcaps

>>> smallcaps("Hello, World!")
'#smallcaps([Hello, World!])'

strong

>>> strong("Hello, World!")
'#strong[Hello, World!]'
>>> strong("Hello, World!", delta=400)
'#strong(delta: 400)[Hello, World!]'
>>> strong(text("Hello, World!", font="Arial"), delta=400)
'#strong(delta: 400)[#text(font: "Arial")[Hello, World!]]'

sub

>>> sub("Hello, World!")
'#sub[Hello, World!]'
>>> sub("Hello, World!", typographic=False)
'#sub(typographic: false)[Hello, World!]'
>>> sub("Hello, World!", baseline=Length.em(0.4))
'#sub(baseline: 0.4em)[Hello, World!]'
>>> sub("Hello, World!", size=Length.em(0.8))
'#sub(size: 0.8em)[Hello, World!]'
>>> sub("Hello, World!", typographic=False, baseline=Length.em(0.4), size=Length.em(0.8))
'#sub(typographic: false, baseline: 0.4em, size: 0.8em)[Hello, World!]'

sup

>>> sup("Hello, World!")
'#super[Hello, World!]'
>>> sup("Hello, World!", typographic=False)
'#super(typographic: false)[Hello, World!]'
>>> sup("Hello, World!", baseline=Length.em(0.4))
'#super(baseline: 0.4em)[Hello, World!]'
>>> sup("Hello, World!", size=Length.em(0.8))
'#super(size: 0.8em)[Hello, World!]'
>>> sup("Hello, World!", typographic=False, baseline=Length.em(0.4), size=Length.em(0.8))
'#super(typographic: false, baseline: 0.4em, size: 0.8em)[Hello, World!]'

text

>>> text("Hello, World!")
'Hello, World!'
>>> text("Hello, World!", font="Arial")
'#text(font: "Arial")[Hello, World!]'
>>> text("Hello, World!", font=("Arial", "Times New Roman"))
'#text(font: ("Arial", "Times New Roman"))[Hello, World!]'
>>> text("Hello, World!", fallback=False)
'#text(fallback: false)[Hello, World!]'
>>> text("Hello, World!", style="italic")
'#text(style: "italic")[Hello, World!]'
>>> text("Hello, World!", weight="bold")
'#text(weight: "bold")[Hello, World!]'
>>> text("Hello, World!", weight=300)
'#text(weight: 300)[Hello, World!]'
>>> text("Hello, World!", stretch=Ratio(50))
'#text(stretch: 50%)[Hello, World!]'
>>> text("Hello, World!", size=Length(12, "pt"))
'#text(size: 12pt)[Hello, World!]'
>>> text("Hello, World!", fill=color("red"))
'#text(fill: rgb("#ff4136"))[Hello, World!]'

Project details


Download files

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

Source Distribution

typstpy-0.1.2.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typstpy-0.1.2-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file typstpy-0.1.2.tar.gz.

File metadata

  • Download URL: typstpy-0.1.2.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Windows/11

File hashes

Hashes for typstpy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 322c9118907c0746f35a08dbd64377555915a13427633c0aee32ff12db120bef
MD5 c5730313ee667c02b300d5639eee226b
BLAKE2b-256 147b84c74ae076b209dcaa9a22a287094fcec1c75933ec162063dd78a4972d77

See more details on using hashes here.

File details

Details for the file typstpy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: typstpy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 21.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Windows/11

File hashes

Hashes for typstpy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 303a973fcc4e099c42a68febd26b4bddbbf39e39bf7689c8129214fe8cd6afac
MD5 c7effe1dbba28fc0b9c4cedd6ce66f62
BLAKE2b-256 8412c38aadbde8fc34d23069a2bae132355de5e5cababf3f4f520472b5b6b5a2

See more details on using hashes here.

Supported by

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