Skip to main content

Python Frontmatter

Jekyll-style YAML 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:

pip install python-frontmatter

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())

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 BytesIO
>>> f = BytesIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue().decode('utf-8')) # 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.

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.0.0.tar.gz (15.1 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.0.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file python-frontmatter-1.0.0.tar.gz.

File metadata

  • Download URL: python-frontmatter-1.0.0.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for python-frontmatter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e98152e977225ddafea6f01f40b4b0f1de175766322004c826ca99842d19a7cd
MD5 3718ee8fa3b0d4e5b262c9895ef44b81
BLAKE2b-256 dd170614eb78820bd9243ffd1f57c8bd4a86debd644f5a2cef720e07309d307a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_frontmatter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8

File hashes

Hashes for python_frontmatter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 766ae75f1b301ffc5fe3494339147e0fd80bc3deff3d7590a93991978b579b08
MD5 1693d470bc187243e568fdae26febab4
BLAKE2b-256 5f0b95a67d6efbd26c3de5b05e267ed6dd6f359d761526b618e1395a057ae9fa

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