Skip to main content

Parse and manage posts with YAML (or other) frontmatter

Project description

Python Frontmatter

Front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.

This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.

Tests PyPI

Documentation

Install:

# with pip
pip install python-frontmatter

# or uv
uv add python-frontmatter

# local development, with uv
uv sync

Usage:

>>> import frontmatter

Load a post from a filename:

>>> post = frontmatter.load('tests/yaml/hello-world.txt')

Or a file (or file-like object):

>>> with open('tests/yaml/hello-world.txt') as f:
...     post = frontmatter.load(f)

Or load from text:

>>> with open('tests/yaml/hello-world.txt') as f:
...     post = frontmatter.loads(f.read())

If the file has a Byte-Order Mark (BOM), strip it off first. An easy way to do this is by using the utf-8-sig encoding:

>>> with open('tests/yaml/hello-world.txt', encoding="utf-8-sig") as f:
...     post = frontmatter.load(f)

Access content:

>>> print(post.content)
Well, hello there, world.

# this works, too
>>> print(post)
Well, hello there, world.

Use metadata (metadata gets proxied as post keys):

>>> print(post['title'])
Hello, world!

Metadata is a dictionary, with some handy proxies:

>>> sorted(post.keys())
['layout', 'title']

>>> from pprint import pprint
>>> post['excerpt'] = 'tl;dr'
>>> pprint(post.metadata)
{'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}

If you don't need the whole post object, just parse:

>>> with open('tests/yaml/hello-world.txt') as f:
...     metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!

Write back to plain text, too:

>>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.

Or write to a file (or file-like object):

>>> from io import StringIO
>>> f = StringIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue()) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.

For more examples, see files in the tests/ directory. Each sample file has a corresponding .result.json file showing the expected parsed output. See also the examples/ directory, which covers more ways to customize input and output.

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

python_frontmatter-1.3.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

python_frontmatter-1.3.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file python_frontmatter-1.3.0.tar.gz.

File metadata

  • Download URL: python_frontmatter-1.3.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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}

File hashes

Hashes for python_frontmatter-1.3.0.tar.gz
Algorithm Hash digest
SHA256 acc73e477a568dc2a25c9e130c6c68ae8daa8c204c8f7e813db47d6a7280dcf2
MD5 e762a393dcecc5c8cb7def7544ab6b9b
BLAKE2b-256 9de879cbe69864d44f3b48e70ebee0a872a7d5a4e7150c9f8577ed7a5beefff0

See more details on using hashes here.

File details

Details for the file python_frontmatter-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: python_frontmatter-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","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}

File hashes

Hashes for python_frontmatter-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f7dd9260bec99044219159a329f64f039087f9d1a2124c9442556f2fe6f82ec
MD5 e94cba12059203f2aba6ae4d5f8cec73
BLAKE2b-256 a6a317c284b4f4d8ad50f0f9ba70ad8fcc35c777aeafcdbbffdd91bbdc5ab379

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